Let’s say I have a background layer (
B) filled with
RGB 255 115 0, and a foreground layer (
F) filled with
RGB 64 64 64. Both layers are in
Normalblending mode, the opacity on
Fis 80%. The resulting color is
RGB 135 78 57.
I would like to calculate the color with a script, so I searched about the formula used to interpolate between the two colors, and found this (linear interpolation)
C = F*alpha + B*(1-alpha)
So, I normalized the values to be in the range
0..1, by dividing the three color channels by 255 and the opacity by 100. Then I applied the formula above and multipied the channels of the resulting color by 255, rounding to the nearest integer, to obtain the RGB values of the resulting color
However, the values calculated by me are
102 74 51, which are different from those obtained by creating the color in GIMP.
Am I making wrong assumptions, or is the actual formula used by GIMP different?
Thanks to @xenoid, I made my own experiments and I can confirm: if I switch to
Legacythe values are the same as what I have computed.
Searching about linear light I stumbled upon this: Image Precision, and found some slight differences. Let’s assume that
Fis the top layer filled with
RGB 64 64 64at 80% opacity and
Bis the bottom layer filled with
RGB 255 115 46.
Cwill be the color computed by the formula given above.
Precision 8-bit Linear LightI came up with these results:
C: 102 74 60 Legacy: 102 75 61 Default: 102 75 61
Precision 8-bit Perceptual Gamma (sRGB):
C: 102 74 60 Legacy: 102 74 60 Default: 135 78 61
It seems that working with
Perceptual Gammaand using
Legacymode gives an exact match with the values computed with the formula.
When you use the 0..255 range you are likely in a gamma-corrected image. To compute the resulting color, Gimp converts the 0..255 value to “linear light” applies your composition formula, and reapplies the gamma correction. So the composition is really applied with darker colors.
However, since Gimp 2.10 you can work in gamma-corrected or “linear” light. This has an effect on blend modes so you have the choice between “default” and “legacy” modes (this is the small selector on the right of the blend mode selector). In practice, if you work in linear light, the legacy modes don’t perform any gamma correction, and using “Normal (legacy)” will produce the output you computed:
Top: “default” mode, bottom: “legacy” mode.