1.. Permission is granted to copy, distribute and/or modify this 2.. document under the terms of the GNU Free Documentation License, 3.. Version 1.1 or any later version published by the Free Software 4.. Foundation, with no Invariant Sections, no Front-Cover Texts 5.. and no Back-Cover Texts. A copy of the license is included at 6.. Documentation/userspace-api/media/fdl-appendix.rst. 7.. 8.. TODO: replace it to GFDL-1.1-or-later WITH no-invariant-sections 9 10.. _colorspaces: 11 12*********** 13Colorspaces 14*********** 15 16'Color' is a very complex concept and depends on physics, chemistry and 17biology. Just because you have three numbers that describe the 'red', 18'green' and 'blue' components of the color of a pixel does not mean that 19you can accurately display that color. A colorspace defines what it 20actually *means* to have an RGB value of e.g. (255, 0, 0). That is, 21which color should be reproduced on the screen in a perfectly calibrated 22environment. 23 24In order to do that we first need to have a good definition of color, 25i.e. some way to uniquely and unambiguously define a color so that 26someone else can reproduce it. Human color vision is trichromatic since 27the human eye has color receptors that are sensitive to three different 28wavelengths of light. Hence the need to use three numbers to describe 29color. Be glad you are not a mantis shrimp as those are sensitive to 12 30different wavelengths, so instead of RGB we would be using the 31ABCDEFGHIJKL colorspace... 32 33Color exists only in the eye and brain and is the result of how strongly 34color receptors are stimulated. This is based on the Spectral Power 35Distribution (SPD) which is a graph showing the intensity (radiant 36power) of the light at wavelengths covering the visible spectrum as it 37enters the eye. The science of colorimetry is about the relationship 38between the SPD and color as perceived by the human brain. 39 40Since the human eye has only three color receptors it is perfectly 41possible that different SPDs will result in the same stimulation of 42those receptors and are perceived as the same color, even though the SPD 43of the light is different. 44 45In the 1920s experiments were devised to determine the relationship 46between SPDs and the perceived color and that resulted in the CIE 1931 47standard that defines spectral weighting functions that model the 48perception of color. Specifically that standard defines functions that 49can take an SPD and calculate the stimulus for each color receptor. 50After some further mathematical transforms these stimuli are known as 51the *CIE XYZ tristimulus* values and these X, Y and Z values describe a 52color as perceived by a human unambiguously. These X, Y and Z values are 53all in the range [0…1]. 54 55The Y value in the CIE XYZ colorspace corresponds to luminance. Often 56the CIE XYZ colorspace is transformed to the normalized CIE xyY 57colorspace: 58 59 x = X / (X + Y + Z) 60 61 y = Y / (X + Y + Z) 62 63The x and y values are the chromaticity coordinates and can be used to 64define a color without the luminance component Y. It is very confusing 65to have such similar names for these colorspaces. Just be aware that if 66colors are specified with lower case 'x' and 'y', then the CIE xyY 67colorspace is used. Upper case 'X' and 'Y' refer to the CIE XYZ 68colorspace. Also, y has nothing to do with luminance. Together x and y 69specify a color, and Y the luminance. That is really all you need to 70remember from a practical point of view. At the end of this section you 71will find reading resources that go into much more detail if you are 72interested. 73 74A monitor or TV will reproduce colors by emitting light at three 75different wavelengths, the combination of which will stimulate the color 76receptors in the eye and thus cause the perception of color. 77Historically these wavelengths were defined by the red, green and blue 78phosphors used in the displays. These *color primaries* are part of what 79defines a colorspace. 80 81Different display devices will have different primaries and some 82primaries are more suitable for some display technologies than others. 83This has resulted in a variety of colorspaces that are used for 84different display technologies or uses. To define a colorspace you need 85to define the three color primaries (these are typically defined as x, y 86chromaticity coordinates from the CIE xyY colorspace) but also the white 87reference: that is the color obtained when all three primaries are at 88maximum power. This determines the relative power or energy of the 89primaries. This is usually chosen to be close to daylight which has been 90defined as the CIE D65 Illuminant. 91 92To recapitulate: the CIE XYZ colorspace uniquely identifies colors. 93Other colorspaces are defined by three chromaticity coordinates defined 94in the CIE xyY colorspace. Based on those a 3x3 matrix can be 95constructed that transforms CIE XYZ colors to colors in the new 96colorspace. 97 98Both the CIE XYZ and the RGB colorspace that are derived from the 99specific chromaticity primaries are linear colorspaces. But neither the 100eye, nor display technology is linear. Doubling the values of all 101components in the linear colorspace will not be perceived as twice the 102intensity of the color. So each colorspace also defines a transfer 103function that takes a linear color component value and transforms it to 104the non-linear component value, which is a closer match to the 105non-linear performance of both the eye and displays. Linear component 106values are denoted RGB, non-linear are denoted as R'G'B'. In general 107colors used in graphics are all R'G'B', except in openGL which uses 108linear RGB. Special care should be taken when dealing with openGL to 109provide linear RGB colors or to use the built-in openGL support to apply 110the inverse transfer function. 111 112The final piece that defines a colorspace is a function that transforms 113non-linear R'G'B' to non-linear Y'CbCr. This function is determined by 114the so-called luma coefficients. There may be multiple possible Y'CbCr 115encodings allowed for the same colorspace. Many encodings of color 116prefer to use luma (Y') and chroma (CbCr) instead of R'G'B'. Since the 117human eye is more sensitive to differences in luminance than in color 118this encoding allows one to reduce the amount of color information 119compared to the luma data. Note that the luma (Y') is unrelated to the Y 120in the CIE XYZ colorspace. Also note that Y'CbCr is often called YCbCr 121or YUV even though these are strictly speaking wrong. 122 123Sometimes people confuse Y'CbCr as being a colorspace. This is not 124correct, it is just an encoding of an R'G'B' color into luma and chroma 125values. The underlying colorspace that is associated with the R'G'B' 126color is also associated with the Y'CbCr color. 127 128The final step is how the RGB, R'G'B' or Y'CbCr values are quantized. 129The CIE XYZ colorspace where X, Y and Z are in the range [0…1] describes 130all colors that humans can perceive, but the transform to another 131colorspace will produce colors that are outside the [0…1] range. Once 132clamped to the [0…1] range those colors can no longer be reproduced in 133that colorspace. This clamping is what reduces the extent or gamut of 134the colorspace. How the range of [0…1] is translated to integer values 135in the range of [0…255] (or higher, depending on the color depth) is 136called the quantization. This is *not* part of the colorspace 137definition. In practice RGB or R'G'B' values are full range, i.e. they 138use the full [0…255] range. Y'CbCr values on the other hand are limited 139range with Y' using [16…235] and Cb and Cr using [16…240]. 140 141Unfortunately, in some cases limited range RGB is also used where the 142components use the range [16…235]. And full range Y'CbCr also exists 143using the [0…255] range. 144 145In order to correctly interpret a color you need to know the 146quantization range, whether it is R'G'B' or Y'CbCr, the used Y'CbCr 147encoding and the colorspace. From that information you can calculate the 148corresponding CIE XYZ color and map that again to whatever colorspace 149your display device uses. 150 151The colorspace definition itself consists of the three chromaticity 152primaries, the white reference chromaticity, a transfer function and the 153luma coefficients needed to transform R'G'B' to Y'CbCr. While some 154colorspace standards correctly define all four, quite often the 155colorspace standard only defines some, and you have to rely on other 156standards for the missing pieces. The fact that colorspaces are often a 157mix of different standards also led to very confusing naming conventions 158where the name of a standard was used to name a colorspace when in fact 159that standard was part of various other colorspaces as well. 160 161If you want to read more about colors and colorspaces, then the 162following resources are useful: :ref:`poynton` is a good practical 163book for video engineers, :ref:`colimg` has a much broader scope and 164describes many more aspects of color (physics, chemistry, biology, 165etc.). The 166`http://www.brucelindbloom.com <http://www.brucelindbloom.com>`__ 167website is an excellent resource, especially with respect to the 168mathematics behind colorspace conversions. The wikipedia 169`CIE 1931 colorspace <http://en.wikipedia.org/wiki/CIE_1931_color_space#CIE_xy_chromaticity_diagram_and_the_CIE_xyY_color_space>`__ 170article is also very useful. 171