19.4. Color Space Conversions
A variety of color space conversions can be implemented in OpenGL shaders. In this section, we look at converting CIE colors to RGB, and vice versa. Conversions between other color spaces can be done similarly.
The CIE system was defined in 1931 by the Committee Internationale de L'Éclairage (CIE). It defines a device-independent color space based on the characteristics of human color perception. The CIE set up a hypothetical set of primaries, XYZ, that correspond to the way the eye's retina behaves. After experiments based on color matching by human observers, the CIE defined the primaries so that all visible light maps into a positive mixture of X, Y, and Z and so that Y correlates approximately to the apparent lightness of a color. The CIE system precisely defines any color. With this as a standard reference, colors can be transformed from the native (device-dependent) color space of one device to the native color space of another device.
A matrix formulation is convenient for performing such conversions. The HDTV standard as defined in ITU-R Recommendation BT.709 (1990) has the following CIE XYZ primaries and uses the D65 (natural sunlight) white point:
Listing 19.1 shows the OpenGL shader code that transforms CIE color values to HDTV standard RGB values by using the D65 white point, and Listing 19.2 shows the reverse transformation. The matrices look like they are transposed compared to the colorimetry literature because in the OpenGL Shading Language, matrix values are provided in column major order.
Listing 19.1. OpenGL shader code to transform CIE values to RGB
Listing 19.2. OpenGL shader code to transform RGB values to CIE