0001 .. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
0002
0003 .. _colorspaces:
0004
0005 ***********
0006 Colorspaces
0007 ***********
0008
0009 'Color' is a very complex concept and depends on physics, chemistry and
0010 biology. Just because you have three numbers that describe the 'red',
0011 'green' and 'blue' components of the color of a pixel does not mean that
0012 you can accurately display that color. A colorspace defines what it
0013 actually *means* to have an RGB value of e.g. (255, 0, 0). That is,
0014 which color should be reproduced on the screen in a perfectly calibrated
0015 environment.
0016
0017 In order to do that we first need to have a good definition of color,
0018 i.e. some way to uniquely and unambiguously define a color so that
0019 someone else can reproduce it. Human color vision is trichromatic since
0020 the human eye has color receptors that are sensitive to three different
0021 wavelengths of light. Hence the need to use three numbers to describe
0022 color. Be glad you are not a mantis shrimp as those are sensitive to 12
0023 different wavelengths, so instead of RGB we would be using the
0024 ABCDEFGHIJKL colorspace...
0025
0026 Color exists only in the eye and brain and is the result of how strongly
0027 color receptors are stimulated. This is based on the Spectral Power
0028 Distribution (SPD) which is a graph showing the intensity (radiant
0029 power) of the light at wavelengths covering the visible spectrum as it
0030 enters the eye. The science of colorimetry is about the relationship
0031 between the SPD and color as perceived by the human brain.
0032
0033 Since the human eye has only three color receptors it is perfectly
0034 possible that different SPDs will result in the same stimulation of
0035 those receptors and are perceived as the same color, even though the SPD
0036 of the light is different.
0037
0038 In the 1920s experiments were devised to determine the relationship
0039 between SPDs and the perceived color and that resulted in the CIE 1931
0040 standard that defines spectral weighting functions that model the
0041 perception of color. Specifically that standard defines functions that
0042 can take an SPD and calculate the stimulus for each color receptor.
0043 After some further mathematical transforms these stimuli are known as
0044 the *CIE XYZ tristimulus* values and these X, Y and Z values describe a
0045 color as perceived by a human unambiguously. These X, Y and Z values are
0046 all in the range [0…1].
0047
0048 The Y value in the CIE XYZ colorspace corresponds to luminance. Often
0049 the CIE XYZ colorspace is transformed to the normalized CIE xyY
0050 colorspace:
0051
0052 x = X / (X + Y + Z)
0053
0054 y = Y / (X + Y + Z)
0055
0056 The x and y values are the chromaticity coordinates and can be used to
0057 define a color without the luminance component Y. It is very confusing
0058 to have such similar names for these colorspaces. Just be aware that if
0059 colors are specified with lower case 'x' and 'y', then the CIE xyY
0060 colorspace is used. Upper case 'X' and 'Y' refer to the CIE XYZ
0061 colorspace. Also, y has nothing to do with luminance. Together x and y
0062 specify a color, and Y the luminance. That is really all you need to
0063 remember from a practical point of view. At the end of this section you
0064 will find reading resources that go into much more detail if you are
0065 interested.
0066
0067 A monitor or TV will reproduce colors by emitting light at three
0068 different wavelengths, the combination of which will stimulate the color
0069 receptors in the eye and thus cause the perception of color.
0070 Historically these wavelengths were defined by the red, green and blue
0071 phosphors used in the displays. These *color primaries* are part of what
0072 defines a colorspace.
0073
0074 Different display devices will have different primaries and some
0075 primaries are more suitable for some display technologies than others.
0076 This has resulted in a variety of colorspaces that are used for
0077 different display technologies or uses. To define a colorspace you need
0078 to define the three color primaries (these are typically defined as x, y
0079 chromaticity coordinates from the CIE xyY colorspace) but also the white
0080 reference: that is the color obtained when all three primaries are at
0081 maximum power. This determines the relative power or energy of the
0082 primaries. This is usually chosen to be close to daylight which has been
0083 defined as the CIE D65 Illuminant.
0084
0085 To recapitulate: the CIE XYZ colorspace uniquely identifies colors.
0086 Other colorspaces are defined by three chromaticity coordinates defined
0087 in the CIE xyY colorspace. Based on those a 3x3 matrix can be
0088 constructed that transforms CIE XYZ colors to colors in the new
0089 colorspace.
0090
0091 Both the CIE XYZ and the RGB colorspace that are derived from the
0092 specific chromaticity primaries are linear colorspaces. But neither the
0093 eye, nor display technology is linear. Doubling the values of all
0094 components in the linear colorspace will not be perceived as twice the
0095 intensity of the color. So each colorspace also defines a transfer
0096 function that takes a linear color component value and transforms it to
0097 the non-linear component value, which is a closer match to the
0098 non-linear performance of both the eye and displays. Linear component
0099 values are denoted RGB, non-linear are denoted as R'G'B'. In general
0100 colors used in graphics are all R'G'B', except in openGL which uses
0101 linear RGB. Special care should be taken when dealing with openGL to
0102 provide linear RGB colors or to use the built-in openGL support to apply
0103 the inverse transfer function.
0104
0105 The final piece that defines a colorspace is a function that transforms
0106 non-linear R'G'B' to non-linear Y'CbCr. This function is determined by
0107 the so-called luma coefficients. There may be multiple possible Y'CbCr
0108 encodings allowed for the same colorspace. Many encodings of color
0109 prefer to use luma (Y') and chroma (CbCr) instead of R'G'B'. Since the
0110 human eye is more sensitive to differences in luminance than in color
0111 this encoding allows one to reduce the amount of color information
0112 compared to the luma data. Note that the luma (Y') is unrelated to the Y
0113 in the CIE XYZ colorspace. Also note that Y'CbCr is often called YCbCr
0114 or YUV even though these are strictly speaking wrong.
0115
0116 Sometimes people confuse Y'CbCr as being a colorspace. This is not
0117 correct, it is just an encoding of an R'G'B' color into luma and chroma
0118 values. The underlying colorspace that is associated with the R'G'B'
0119 color is also associated with the Y'CbCr color.
0120
0121 The final step is how the RGB, R'G'B' or Y'CbCr values are quantized.
0122 The CIE XYZ colorspace where X, Y and Z are in the range [0…1] describes
0123 all colors that humans can perceive, but the transform to another
0124 colorspace will produce colors that are outside the [0…1] range. Once
0125 clamped to the [0…1] range those colors can no longer be reproduced in
0126 that colorspace. This clamping is what reduces the extent or gamut of
0127 the colorspace. How the range of [0…1] is translated to integer values
0128 in the range of [0…255] (or higher, depending on the color depth) is
0129 called the quantization. This is *not* part of the colorspace
0130 definition. In practice RGB or R'G'B' values are full range, i.e. they
0131 use the full [0…255] range. Y'CbCr values on the other hand are limited
0132 range with Y' using [16…235] and Cb and Cr using [16…240].
0133
0134 Unfortunately, in some cases limited range RGB is also used where the
0135 components use the range [16…235]. And full range Y'CbCr also exists
0136 using the [0…255] range.
0137
0138 In order to correctly interpret a color you need to know the
0139 quantization range, whether it is R'G'B' or Y'CbCr, the used Y'CbCr
0140 encoding and the colorspace. From that information you can calculate the
0141 corresponding CIE XYZ color and map that again to whatever colorspace
0142 your display device uses.
0143
0144 The colorspace definition itself consists of the three chromaticity
0145 primaries, the white reference chromaticity, a transfer function and the
0146 luma coefficients needed to transform R'G'B' to Y'CbCr. While some
0147 colorspace standards correctly define all four, quite often the
0148 colorspace standard only defines some, and you have to rely on other
0149 standards for the missing pieces. The fact that colorspaces are often a
0150 mix of different standards also led to very confusing naming conventions
0151 where the name of a standard was used to name a colorspace when in fact
0152 that standard was part of various other colorspaces as well.
0153
0154 If you want to read more about colors and colorspaces, then the
0155 following resources are useful: :ref:`poynton` is a good practical
0156 book for video engineers, :ref:`colimg` has a much broader scope and
0157 describes many more aspects of color (physics, chemistry, biology,
0158 etc.). The
0159 `http://www.brucelindbloom.com <http://www.brucelindbloom.com>`__
0160 website is an excellent resource, especially with respect to the
0161 mathematics behind colorspace conversions. The wikipedia
0162 `CIE 1931 colorspace <http://en.wikipedia.org/wiki/CIE_1931_color_space#CIE_xy_chromaticity_diagram_and_the_CIE_xyY_color_space>`__
0163 article is also very useful.