5
\$\begingroup\$

I have looked at two different vertex shaders that calculate luminance and both use a "magic" vector that I'm not sure the meaning of the the actual values. For instance:

const mediump vec3 Perception = vec3(0.299, 0.587, 0.114);

void main(void)
{
    mediump vec3 color = texture2D(Sampler, TextureCoord).xyz;
    mediump float luminance = dot(Perception, color);
    gl_FragColor = (luminance > Threshold) ? vec4(color, 1) : vec4(0);
}

This was taken from the iPhone 3D programming book.

This perception vector is the magic vector I'm trying to figure out. I added the rgb/xyz values and the add to 1.

I saw another example that used vec3( 0.2125, 0.7154, 0.0721), which also adds to 1.

Can someone explain how one comes up with these values and what they really mean?

\$\endgroup\$

1 Answer 1

11
\$\begingroup\$

When you want to obtain the relative luminance from a RGB color, you are mapping the three values to a single grayscale one. This is achieved by multiplying each of the components by a constant and adding them together, so the final result is in the 0-1 range. That's why the two vectors you found so far are added to 1, because applying the operation to pure white (vec3(1, 1, 1)) will return 1.

The logic behind having a higher green and red components is because the photoreceptor cones in your eye's retina are more sensitive to these components than blue; a little variation of green color (the most sensitive one) can dramatically change the perception of the total luminance perceived by your eye.

And I would stick with the vec3(0.2126, 0.7152, 0.0722) values, since they are the ones used for sRGB color spaces. The first example you posted corresponds to the YCbCr color encoding, and it can cause incorrect results in your calculations.

\$\endgroup\$
1
  • \$\begingroup\$ Awesome answer. I actually studied the eye being more sensitive to green and red this semester but didn't put the two together. \$\endgroup\$
    – Joey Green
    Dec 21, 2011 at 18:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .