1059b1c5bSMauro Carvalho Chehab.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later 254f38fcaSMauro Carvalho Chehab 354f38fcaSMauro Carvalho Chehab.. _colorspaces: 454f38fcaSMauro Carvalho Chehab 554f38fcaSMauro Carvalho Chehab*********** 654f38fcaSMauro Carvalho ChehabColorspaces 754f38fcaSMauro Carvalho Chehab*********** 854f38fcaSMauro Carvalho Chehab 954f38fcaSMauro Carvalho Chehab'Color' is a very complex concept and depends on physics, chemistry and 1054f38fcaSMauro Carvalho Chehabbiology. Just because you have three numbers that describe the 'red', 1154f38fcaSMauro Carvalho Chehab'green' and 'blue' components of the color of a pixel does not mean that 1254f38fcaSMauro Carvalho Chehabyou can accurately display that color. A colorspace defines what it 1354f38fcaSMauro Carvalho Chehabactually *means* to have an RGB value of e.g. (255, 0, 0). That is, 1454f38fcaSMauro Carvalho Chehabwhich color should be reproduced on the screen in a perfectly calibrated 1554f38fcaSMauro Carvalho Chehabenvironment. 1654f38fcaSMauro Carvalho Chehab 1754f38fcaSMauro Carvalho ChehabIn order to do that we first need to have a good definition of color, 1854f38fcaSMauro Carvalho Chehabi.e. some way to uniquely and unambiguously define a color so that 1954f38fcaSMauro Carvalho Chehabsomeone else can reproduce it. Human color vision is trichromatic since 2054f38fcaSMauro Carvalho Chehabthe human eye has color receptors that are sensitive to three different 2154f38fcaSMauro Carvalho Chehabwavelengths of light. Hence the need to use three numbers to describe 2254f38fcaSMauro Carvalho Chehabcolor. Be glad you are not a mantis shrimp as those are sensitive to 12 2354f38fcaSMauro Carvalho Chehabdifferent wavelengths, so instead of RGB we would be using the 2454f38fcaSMauro Carvalho ChehabABCDEFGHIJKL colorspace... 2554f38fcaSMauro Carvalho Chehab 2654f38fcaSMauro Carvalho ChehabColor exists only in the eye and brain and is the result of how strongly 2754f38fcaSMauro Carvalho Chehabcolor receptors are stimulated. This is based on the Spectral Power 2854f38fcaSMauro Carvalho ChehabDistribution (SPD) which is a graph showing the intensity (radiant 2954f38fcaSMauro Carvalho Chehabpower) of the light at wavelengths covering the visible spectrum as it 3054f38fcaSMauro Carvalho Chehabenters the eye. The science of colorimetry is about the relationship 3154f38fcaSMauro Carvalho Chehabbetween the SPD and color as perceived by the human brain. 3254f38fcaSMauro Carvalho Chehab 3354f38fcaSMauro Carvalho ChehabSince the human eye has only three color receptors it is perfectly 3454f38fcaSMauro Carvalho Chehabpossible that different SPDs will result in the same stimulation of 3554f38fcaSMauro Carvalho Chehabthose receptors and are perceived as the same color, even though the SPD 3654f38fcaSMauro Carvalho Chehabof the light is different. 3754f38fcaSMauro Carvalho Chehab 3854f38fcaSMauro Carvalho ChehabIn the 1920s experiments were devised to determine the relationship 3954f38fcaSMauro Carvalho Chehabbetween SPDs and the perceived color and that resulted in the CIE 1931 4054f38fcaSMauro Carvalho Chehabstandard that defines spectral weighting functions that model the 4154f38fcaSMauro Carvalho Chehabperception of color. Specifically that standard defines functions that 4254f38fcaSMauro Carvalho Chehabcan take an SPD and calculate the stimulus for each color receptor. 4354f38fcaSMauro Carvalho ChehabAfter some further mathematical transforms these stimuli are known as 4454f38fcaSMauro Carvalho Chehabthe *CIE XYZ tristimulus* values and these X, Y and Z values describe a 4554f38fcaSMauro Carvalho Chehabcolor as perceived by a human unambiguously. These X, Y and Z values are 4654f38fcaSMauro Carvalho Chehaball in the range [0…1]. 4754f38fcaSMauro Carvalho Chehab 4854f38fcaSMauro Carvalho ChehabThe Y value in the CIE XYZ colorspace corresponds to luminance. Often 4954f38fcaSMauro Carvalho Chehabthe CIE XYZ colorspace is transformed to the normalized CIE xyY 5054f38fcaSMauro Carvalho Chehabcolorspace: 5154f38fcaSMauro Carvalho Chehab 5254f38fcaSMauro Carvalho Chehab x = X / (X + Y + Z) 5354f38fcaSMauro Carvalho Chehab 5454f38fcaSMauro Carvalho Chehab y = Y / (X + Y + Z) 5554f38fcaSMauro Carvalho Chehab 5654f38fcaSMauro Carvalho ChehabThe x and y values are the chromaticity coordinates and can be used to 5754f38fcaSMauro Carvalho Chehabdefine a color without the luminance component Y. It is very confusing 5854f38fcaSMauro Carvalho Chehabto have such similar names for these colorspaces. Just be aware that if 5954f38fcaSMauro Carvalho Chehabcolors are specified with lower case 'x' and 'y', then the CIE xyY 6054f38fcaSMauro Carvalho Chehabcolorspace is used. Upper case 'X' and 'Y' refer to the CIE XYZ 6154f38fcaSMauro Carvalho Chehabcolorspace. Also, y has nothing to do with luminance. Together x and y 6254f38fcaSMauro Carvalho Chehabspecify a color, and Y the luminance. That is really all you need to 6354f38fcaSMauro Carvalho Chehabremember from a practical point of view. At the end of this section you 6454f38fcaSMauro Carvalho Chehabwill find reading resources that go into much more detail if you are 6554f38fcaSMauro Carvalho Chehabinterested. 6654f38fcaSMauro Carvalho Chehab 6754f38fcaSMauro Carvalho ChehabA monitor or TV will reproduce colors by emitting light at three 6854f38fcaSMauro Carvalho Chehabdifferent wavelengths, the combination of which will stimulate the color 6954f38fcaSMauro Carvalho Chehabreceptors in the eye and thus cause the perception of color. 7054f38fcaSMauro Carvalho ChehabHistorically these wavelengths were defined by the red, green and blue 7154f38fcaSMauro Carvalho Chehabphosphors used in the displays. These *color primaries* are part of what 7254f38fcaSMauro Carvalho Chehabdefines a colorspace. 7354f38fcaSMauro Carvalho Chehab 7454f38fcaSMauro Carvalho ChehabDifferent display devices will have different primaries and some 7554f38fcaSMauro Carvalho Chehabprimaries are more suitable for some display technologies than others. 7654f38fcaSMauro Carvalho ChehabThis has resulted in a variety of colorspaces that are used for 7754f38fcaSMauro Carvalho Chehabdifferent display technologies or uses. To define a colorspace you need 7854f38fcaSMauro Carvalho Chehabto define the three color primaries (these are typically defined as x, y 7954f38fcaSMauro Carvalho Chehabchromaticity coordinates from the CIE xyY colorspace) but also the white 8054f38fcaSMauro Carvalho Chehabreference: that is the color obtained when all three primaries are at 8154f38fcaSMauro Carvalho Chehabmaximum power. This determines the relative power or energy of the 8254f38fcaSMauro Carvalho Chehabprimaries. This is usually chosen to be close to daylight which has been 8354f38fcaSMauro Carvalho Chehabdefined as the CIE D65 Illuminant. 8454f38fcaSMauro Carvalho Chehab 8554f38fcaSMauro Carvalho ChehabTo recapitulate: the CIE XYZ colorspace uniquely identifies colors. 8654f38fcaSMauro Carvalho ChehabOther colorspaces are defined by three chromaticity coordinates defined 8754f38fcaSMauro Carvalho Chehabin the CIE xyY colorspace. Based on those a 3x3 matrix can be 8854f38fcaSMauro Carvalho Chehabconstructed that transforms CIE XYZ colors to colors in the new 8954f38fcaSMauro Carvalho Chehabcolorspace. 9054f38fcaSMauro Carvalho Chehab 9154f38fcaSMauro Carvalho ChehabBoth the CIE XYZ and the RGB colorspace that are derived from the 9254f38fcaSMauro Carvalho Chehabspecific chromaticity primaries are linear colorspaces. But neither the 9354f38fcaSMauro Carvalho Chehabeye, nor display technology is linear. Doubling the values of all 9454f38fcaSMauro Carvalho Chehabcomponents in the linear colorspace will not be perceived as twice the 9554f38fcaSMauro Carvalho Chehabintensity of the color. So each colorspace also defines a transfer 9654f38fcaSMauro Carvalho Chehabfunction that takes a linear color component value and transforms it to 9754f38fcaSMauro Carvalho Chehabthe non-linear component value, which is a closer match to the 9854f38fcaSMauro Carvalho Chehabnon-linear performance of both the eye and displays. Linear component 9954f38fcaSMauro Carvalho Chehabvalues are denoted RGB, non-linear are denoted as R'G'B'. In general 10054f38fcaSMauro Carvalho Chehabcolors used in graphics are all R'G'B', except in openGL which uses 10154f38fcaSMauro Carvalho Chehablinear RGB. Special care should be taken when dealing with openGL to 10254f38fcaSMauro Carvalho Chehabprovide linear RGB colors or to use the built-in openGL support to apply 10354f38fcaSMauro Carvalho Chehabthe inverse transfer function. 10454f38fcaSMauro Carvalho Chehab 10554f38fcaSMauro Carvalho ChehabThe final piece that defines a colorspace is a function that transforms 10654f38fcaSMauro Carvalho Chehabnon-linear R'G'B' to non-linear Y'CbCr. This function is determined by 10754f38fcaSMauro Carvalho Chehabthe so-called luma coefficients. There may be multiple possible Y'CbCr 10854f38fcaSMauro Carvalho Chehabencodings allowed for the same colorspace. Many encodings of color 10954f38fcaSMauro Carvalho Chehabprefer to use luma (Y') and chroma (CbCr) instead of R'G'B'. Since the 11054f38fcaSMauro Carvalho Chehabhuman eye is more sensitive to differences in luminance than in color 11154f38fcaSMauro Carvalho Chehabthis encoding allows one to reduce the amount of color information 11254f38fcaSMauro Carvalho Chehabcompared to the luma data. Note that the luma (Y') is unrelated to the Y 11354f38fcaSMauro Carvalho Chehabin the CIE XYZ colorspace. Also note that Y'CbCr is often called YCbCr 11454f38fcaSMauro Carvalho Chehabor YUV even though these are strictly speaking wrong. 11554f38fcaSMauro Carvalho Chehab 11654f38fcaSMauro Carvalho ChehabSometimes people confuse Y'CbCr as being a colorspace. This is not 11754f38fcaSMauro Carvalho Chehabcorrect, it is just an encoding of an R'G'B' color into luma and chroma 11854f38fcaSMauro Carvalho Chehabvalues. The underlying colorspace that is associated with the R'G'B' 11954f38fcaSMauro Carvalho Chehabcolor is also associated with the Y'CbCr color. 12054f38fcaSMauro Carvalho Chehab 12154f38fcaSMauro Carvalho ChehabThe final step is how the RGB, R'G'B' or Y'CbCr values are quantized. 12254f38fcaSMauro Carvalho ChehabThe CIE XYZ colorspace where X, Y and Z are in the range [0…1] describes 12354f38fcaSMauro Carvalho Chehaball colors that humans can perceive, but the transform to another 12454f38fcaSMauro Carvalho Chehabcolorspace will produce colors that are outside the [0…1] range. Once 12554f38fcaSMauro Carvalho Chehabclamped to the [0…1] range those colors can no longer be reproduced in 12654f38fcaSMauro Carvalho Chehabthat colorspace. This clamping is what reduces the extent or gamut of 12754f38fcaSMauro Carvalho Chehabthe colorspace. How the range of [0…1] is translated to integer values 12854f38fcaSMauro Carvalho Chehabin the range of [0…255] (or higher, depending on the color depth) is 12954f38fcaSMauro Carvalho Chehabcalled the quantization. This is *not* part of the colorspace 13054f38fcaSMauro Carvalho Chehabdefinition. In practice RGB or R'G'B' values are full range, i.e. they 13154f38fcaSMauro Carvalho Chehabuse the full [0…255] range. Y'CbCr values on the other hand are limited 13254f38fcaSMauro Carvalho Chehabrange with Y' using [16…235] and Cb and Cr using [16…240]. 13354f38fcaSMauro Carvalho Chehab 13454f38fcaSMauro Carvalho ChehabUnfortunately, in some cases limited range RGB is also used where the 13554f38fcaSMauro Carvalho Chehabcomponents use the range [16…235]. And full range Y'CbCr also exists 13654f38fcaSMauro Carvalho Chehabusing the [0…255] range. 13754f38fcaSMauro Carvalho Chehab 13854f38fcaSMauro Carvalho ChehabIn order to correctly interpret a color you need to know the 13954f38fcaSMauro Carvalho Chehabquantization range, whether it is R'G'B' or Y'CbCr, the used Y'CbCr 14054f38fcaSMauro Carvalho Chehabencoding and the colorspace. From that information you can calculate the 14154f38fcaSMauro Carvalho Chehabcorresponding CIE XYZ color and map that again to whatever colorspace 14254f38fcaSMauro Carvalho Chehabyour display device uses. 14354f38fcaSMauro Carvalho Chehab 14454f38fcaSMauro Carvalho ChehabThe colorspace definition itself consists of the three chromaticity 14554f38fcaSMauro Carvalho Chehabprimaries, the white reference chromaticity, a transfer function and the 14654f38fcaSMauro Carvalho Chehabluma coefficients needed to transform R'G'B' to Y'CbCr. While some 14754f38fcaSMauro Carvalho Chehabcolorspace standards correctly define all four, quite often the 14854f38fcaSMauro Carvalho Chehabcolorspace standard only defines some, and you have to rely on other 14954f38fcaSMauro Carvalho Chehabstandards for the missing pieces. The fact that colorspaces are often a 15054f38fcaSMauro Carvalho Chehabmix of different standards also led to very confusing naming conventions 15154f38fcaSMauro Carvalho Chehabwhere the name of a standard was used to name a colorspace when in fact 15254f38fcaSMauro Carvalho Chehabthat standard was part of various other colorspaces as well. 15354f38fcaSMauro Carvalho Chehab 15454f38fcaSMauro Carvalho ChehabIf you want to read more about colors and colorspaces, then the 15554f38fcaSMauro Carvalho Chehabfollowing resources are useful: :ref:`poynton` is a good practical 15654f38fcaSMauro Carvalho Chehabbook for video engineers, :ref:`colimg` has a much broader scope and 15754f38fcaSMauro Carvalho Chehabdescribes many more aspects of color (physics, chemistry, biology, 15854f38fcaSMauro Carvalho Chehabetc.). The 15954f38fcaSMauro Carvalho Chehab`http://www.brucelindbloom.com <http://www.brucelindbloom.com>`__ 16054f38fcaSMauro Carvalho Chehabwebsite is an excellent resource, especially with respect to the 16154f38fcaSMauro Carvalho Chehabmathematics behind colorspace conversions. The wikipedia 16254f38fcaSMauro Carvalho Chehab`CIE 1931 colorspace <http://en.wikipedia.org/wiki/CIE_1931_color_space#CIE_xy_chromaticity_diagram_and_the_CIE_xyY_color_space>`__ 16354f38fcaSMauro Carvalho Chehabarticle is also very useful. 164