spectrum Where's purple? Or, how to plot colours properly on a computer screen

 forward Forward to What colour is the Sun?

 Colour Quiz Home Colour Quiz Homepage


The mathematics on this page was translated from TEX by TTH, version 2.01.

The page contains mathematical symbols and Greek letters, and some letters from the extended Latin ISO-8859-1 character set. If these do not show correctly, then


What is wrong with this?

spectrum

The above is supposed to show, as accurately as possible on a computer screen, the colours of the visible spectrum as a function of wavelength in nanometers. What is wrong with it? The problem is that computer monitors cannot show monochromatic colours as saturated as the eye is able to perceive them. Disco purple, at the shortest monochromatic wavelengths, probably shows up on the screen as wilting violet, while crimson red, at the longest monochromatic wavelengths, appears pinkish.

How do you plot a spectrum of monochromatic colours as accurately as possible on a computer screen? Set saturation to 1, and run hue from -0.046 (i.e. 0.954, about right for crimson red) to 0.753 (about right for disco purple), right? Actually it's more complicated if you want to do it right. The following four steps describe what to do.

Step 1: Define the spectrum

As you undoubtedly know, the eye perceives colour through three types of cone on the retina. As a result, it is possible to define any colour, any mixture of visible wavelengths, using 3 coordinates.

By convention of the Commission Internationale de l'Éclairage (CIE), colours are defined by their `tristimulus' coordinates X, Y, Z. The coordinates are linear measures of light power, so that the coordinates of any sum of colours is the sum of the coordinates of the colours. The middle coordinate, Y, called the `luminance', is the light power weighted by the CIE 1924 Photopic Luminosity Function, a certain spectral sensitivity function characteristic of human vision. The luminance Y can be regarded as a measure of perceived brightness, with the proviso that it varies linearly with power, whereas the eye responds nonlinearly with power.

The colour of a colour, without reference to its total power, is conventionally described by its `chromaticity' coordinates x, y, defined by

x X
X + Y + Z
 ,     y Y
X + Y + Z
 .
(1)
The 1931 CIE Chromaticity Diagram 1931 CIE Chromaticity Diagram shows colour plotted over the x-y plane of chromaticities. Note that notwithstanding equation (1), it is the luminance Y, not the sum X + Y + Z, that is a measure of perceived total brightness. Thus one sometimes sees colours specified by their chromaticity and luminance x, y, Y.

A table of the tristimulus coordinates X, Y, Z for monochromatic light every 1 nm from 360 nm to 830 nm is available from the UCSD Color and Vision database. There are two sets of files, the original 1931 set, based on experiments that involved people matching colours in a split 2° field, about the size of the cone-rich fovea at the center of the retina, and a supplementary 1964 set, for a 10° field, which gives greater weight to rods. The 2° tristimuli are appropriate for normal colour vision at daylight levels of illumination, while the 10° tristimuli are more appropriate for dark-adapted vision. The spectrum at the top of this page used the 2° field tristimuli.

Step 2: Define the characteristics of the monitor

The characteristics of a colour monitor are determined by the the colours of its guns, its red, green, and blue primaries. Measured on a linear scale of light power, the monitor's primaries are denoted R, G, and B. It is important to distinguish these linear measures of colour from the nonlinear measures R, G, B that are specified to the computer's colour tables. More on the latter in Step 4 below.

The colour of each primary is specified by its tristimulus coordinates. For example, the colour of the R gun is specified by XR , YR , ZR . Given these coordinates, the tristimulus coordinates X, Y, Z of any combination colour R, G, B are given by the linear transformation





X
Y
Z




=



XR
XG
XB
YR
YG
YB
ZR
ZG
ZB








R
G
B




 .
(2)
For example, the monitor's colour white, W, is defined by all guns firing on full, RW = GW = BW = 1, so that XW = XR + XG + XB , and similarly for YW and ZW .

Conversely, the R, G, B coordinates of any colour X, Y, Z are given by the inverse of the matrix transformation (2).





R
G
B




=



RX
RY
RZ
GX
GY
GZ
BX
BY
BZ








X
Y
Z




=



XR
XG
XB
YR
YG
YB
ZR
ZG
ZB
-1







X
Y
Z




 .
(3)

Usually, the colour properties of a monitor are specified not in terms of the tristimuli of its primaries, as above, but rather in terms of the chromaticities xR , yR , xG , yG , xB , yB , of the primaries, together with the chromaticity xW , yW of the white point. Conventionally, the luminance of white is normalized to one, YW = 1. The 6 primary chromaticities, the 2 white chromaticities, and the white luminance together constitute 9 quantities, precisely sufficient to specify the 9 tristimulus coordinates of the R, G, B primaries.

Exercise: Figure out how to construct the XYZ  RGB matrices in equations (2) and (3), given the primary chromaticities xR , yR , etc., together with the white chromaticity xW , yW , and unit white luminance YW = 1.
Solution: Look at the TxrFromChrom routine from Johan Lammens' ``chromaticity'' Mathematica® package in Transformations between different color spaces.

What primary and white chromaticities should one assume? Item 45 of the Color FAQ recommends using the CIE D65 `daylight' white point

xW = 0.3127  ,     yW = 0.3290  ,
(4)
and primary chromaticities from Rec. 709 (ITU-R Recommendation BT. 709, 1990, ``Basic Parameter Values for the HDTV Standard for the Studio and for International Programme Exchange'', formerly CCIR Rec. 709),


xR
xG
xB
yR
yG
yB


=

0.64
0.30
0.15
0.33
0.60
0.06


 .
(5)
The spectrum at the top of this page, and the chromaticity diagram just below, use these recommended values.

Step 3: Transform from XYZ to RGB

chromaticity diagram Given the tristimuli coordinates X, Y, Z of any colour, such as those for monochromatic colours specified in the CIE 2° tables, and the characteristics of the colour monitor as described in Step 2, the corresponding R, G, B coordinates follow from equation (3).

Often one or two (never three) of the R, G, B coordinates will turn out negative. This means that the colour lies outside the `gamut' of colours that the monitor can reproduce. For example, every monochromatic colour is out of gamut. On the chromaticity diagram, the gamut forms a triangle with the monitor's primaries at its vertices, as in the diagram on the right, constructed using a corrected version of John Walker's cietoppm utility. The blackbody trajectory on this chromaticity diagram is discussed on the colour temperature What colour is the Sun? page. Compare this diagram to the chromaticity diagram with respect to the fundamental primaries on the fundamental primaries Primary Colours page.

The standard way to fix out-of-gamut colours is to add white - equal parts of R, G, and B - just enough to make all components positive, so bringing the colour to the border of the gamut. That is, add -min(RGB, 0) to each of R, G, and B. It is this adding of white that causes disco purple and crimson red to appear palid in the spectrum at the top of this page.

At this point you are free to multiply the R, G, and B values of your entire picture by some overall factor, to achieve the desired overall level of brightness. If any of the R, G, or B values exceed the permitted maximum (typically 1, or 255), then obviously you will have to scale those values back to the maximum.

Step 4: Do a gamma correction

The colour guns on monitors respond nonlinearly to their inputs. As Charles Poynton emphasizes in Gamma FAQ, this is a feature, not a bug. The nonlinearity roughly matches the nonlinear response of the human eye, and allows a broader dynamic range of perceived brightnesses to be coded with fewer bits.

The transformation from linear coordinates R, G, and B to nonlinear coordinates R, G, and B, or vice versa, is called `gamma-correction'. It is the latter nonlinear coordinates R, G, B that are specified in software, such as in HTML, PostScript, xv, and so on.

Item 45 of the Color FAQ recommends again following Rec. 709, which specifies a gamma-correction (see Item 6 of the Gamma FAQ)

R =

4.5 R
(R 0.018)
1.099 R0.45 - 0.099
(R > 0.018)
(6)
and similarly for each of G and B. Equation (6) assumes that R, G, B are scaled so that they run from a minimum of 0 to a maximum of 1. The correction is a power law except for the linear ramp near black. The reciprocal of the index of the power law is the `gamma' of the transformation
g = 1
0.45
2.2  .
(7)
The spectrum at the top of this page, and the chromaticity diagram above, use the recommended gamma-correction (6).

Sources


 forward Forward to What colour is the Sun?

 Colour Quiz Home Colour Quiz Homepage

Updated 18 Nov 1999