Posts Tagged ‘Color’

Color Space Correctness in Alpha Blending

Monday, July 8th, 2024

Take a look at these three grayscale images:

Solid grayscale image with value 128Grayscale checkerboard with values 64 and 192Solid grayscale image with value 146

The left image has a solid luminance of 128. The middle image is a checkerboard alternating between luminances of 64 and 192. The right image has a solid luminance of 146. Squint your eyes. Does the luminance in the middle look closer to the luminance on the left, or on the right?

As a checkerboard, the center image is 50% 64 and 50% 192, so mathematically, it should average out to 128, but instead, there’s a substantial difference in luminance, and 146 is much closer. The reason for this discrepancy: All values in these images are sRGB values. When colors mix in the real world, the absolute amount of light is what’s mixed, but sRGB is a non-linear mapping, so attempting to mix sRGB values directly yields nonphysical results. You can read more about this in Eric Brasseur’s blog post, “Gamma error in picture scaling”.

Eric’s post is all about image scaling, where this definitely matters. But this is far from the only place this matters. Near and dear to our hearts from the previous post, blending in OpenGL also has to mix colors in a way that could be affected by use of the wrong color space for computations. Indeed, the blend formula says nothing about the relatively complicated conversions needed for sRGB. So what happens?

(more…)