Thanks for clarifying! You're working with an "eight-mix" approach (a weighted mixture of up to eight color components by parts), and you're trying to approximate how the 19th-century print-shop manual (like The Color Printer by Earhardt) mixes pigment colors—not just blend RGB values linearly.
Goal: Implement a better mixing method in Lua that more closely models pigment mixing for weights given as "x parts color A and y parts color B," rather than a simple linear blend in RGB.
Understanding the 19th-Century Color Mixing Method
Books like Earhardt's The Color Printer essentially work by weighted mixing of pigment colors specified in parts and proportions. The key is that pigment mixing is subtractive rather than additive—reflectance or absorbance combine according to rules different from straightforward RGB addition.
Specifically, the color mixing behavior can be thought of using Kubelka-Munk theory or simplified approaches:
- Each pigment is defined by its reflectance spectrum or a suitable proxy.
- When mixed, the combined pigment's absorption and scattering combine approximately additively with respect to concentration (weight fractions).
- The combined reflectance can then be converted back to a color space (like RGB or Lab) for display.
Earhardt and similar manuals worked with "parts" to give correct proportional mixtures by weight or volume.
Simplified Approach for Your Use Case
Since you have RGB for each pigment and "parts" values, and you're interested in an approach resembling the original mixing process, here is a practical approximation that retains the key subtractive spirit better than linear RGB blend:
-
Convert RGB pigment colors to approximate reflectance values.
Since pigments subtract light, higher RGB means higher reflectance, but their relationship isn't linear. You can approximate reflectance as:
R_reflectance = RGB_component / 255
(Though this is rough; ideally you apply gamma correction first.)
-
Convert Reflectance to Absorbance (Optical Density)
Since pigments absorb light, their "absorbance" A = -log10(R). The idea is that absorbances add proportionally with weights.
-
Mix absorbances weighted by parts:
For pigments A and B with absorbances A_a and A_b and weights w_a and w_b:
A_mix = (w_a * A_a + w_b * A_b) / (w_a + w_b)
-
Convert mixture absorbance back to reflectance:
R_mix = 10^(-A_mix)
-
Convert reflectance back to RGB scale:
RGB_mix = R_mix * 255
-
Repeat for each RGB channel.
Why This Works Better
- Absorbance (optical density) is roughly additive in mixtures by concentration.
- Mixing absorbances weighted by pigment parts mirrors how real pigments combine absorption.
- Converting back to reflectance and RGB gives an output closer to the real subtractive pigment mixtures.
Lua Implementation
-- Utility: Convert RGB component (0-255) to reflectance [0,1]
local function rgb_to_reflectance(c)
-- Approximate linear reflectance (gamma correction could be added)
return c / 255
end
-- Convert reflectance to absorbance (optical density)
local function reflectance_to_absorbance(R)
-- Clamp R to > 0 to avoid log(0)
local epsilon = 1e-6
R = math.max(R, epsilon)
return -math.log10(R)
end
-- Convert absorbance back to reflectance
local function absorbance_to_reflectance(A)
return 10^(-A)
end
-- Mix two pigments given RGB components and weights (parts)
-- rgbA and rgbB are tables: {r,g,b} with values 0-255
-- wA and wB are weights (parts)
local function mix_pigments(rgbA, wA, rgbB, wB)
local totalWeight = wA + wB
local result = {0,0,0}
for i = 1, 3 do
local R_a = rgb_to_reflectance(rgbA[i])
local R_b = rgb_to_reflectance(rgbB[i])
local A_a = reflectance_to_absorbance(R_a)
local A_b = reflectance_to_absorbance(R_b)
-- Weighted average absorbance
local A_mix = (wA * A_a + wB * A_b) / totalWeight
local R_mix = absorbance_to_reflectance(A_mix)
local c_mix = R_mix * 255
-- Clamp result to 0-255
if c_mix < 0 then c_mix = 0 end
if c_mix > 255 then c_mix = 255 end
result[i] = c_mix
end
return result
end
-- Example usage:
local zincWhite = {238, 238, 238} -- nominal zinc white RGB
local colorA = {200, 50, 50} -- example pigment color
local partsWhite = 3
local partsColor = 5
local mixedRGB = mix_pigments(colorA, partsColor, zincWhite, partsWhite)
print(string.format("Mixed RGB: R=%d, G=%d, B=%d",
mixedRGB[1], mixedRGB[2], mixedRGB[3]))
Extending to More Than Two Pigments (Up to Eight)
You can extend this approach for N pigments with weights ( w_1, w_2, ..., w_N ), and pigments ( C_1, C_2, ..., C_N ):
local function mix_multiple_pigments(pigments, weights)
local totalWeight = 0
for _, w in ipairs(weights) do totalWeight = totalWeight + w end
local result = {0, 0, 0}
for i = 1, 3 do
local absorbanceSum = 0
for idx, pigment in ipairs(pigments) do
local R = rgb_to_reflectance(pigment[i])
local A = reflectance_to_absorbance(R)
absorbanceSum = absorbanceSum + A * weights[idx]
end
local A_mix = absorbanceSum / totalWeight
local R_mix = absorbance_to_reflectance(A_mix)
local c_mix = math.min(math.max(R_mix * 255, 0), 255)
result[i] = c_mix
end
return result
end
Notes & Caveats
- This is a simplified physical model that captures pigment subtractive mixing better than direct RGB linear blending.
- It does not account for:
- Real pigment spectral curves beyond RGB.
- Scattering and opacity differences (like zinc white’s low opacity).
- Metamerism or human perceptual nonlinearities.
- Adding gamma correction in conversions (sRGB gamma companding) will further improve realism.
- This matches the spirit of "parts" weighting and pigment mixture from Earhardt's time much better than linear addition.
If you'd like, I can help you add gamma correction or estimate opacity effects for zinc white as well! But this code is a strong step moving away from naive linear additive RGB blending toward a model more aligned with paint mixing.