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