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