Converting between CIE XYZ and RGB
Going from RGB to CIE XYZ is the bridge between device color and device-independent color. It takes two steps: undoing the gamma encoding and multiplying by a 3×3 matrix. Shown here for sRGB, the most widely used RGB space.
Why "RGB" is not enough
The triple (200, 80, 40) does not yet define a color. You also need the display's primaries, its white point and its transfer curve. A specific RGB space defines all three. sRGB, set in 1996 for monitors and the web, has:
| x | y | |
|---|---|---|
| red | 0.64 | 0.33 |
| green | 0.30 | 0.60 |
| blue | 0.15 | 0.06 |
| white D65 | 0.3127 | 0.3290 |
The matrix below is derived from these chromaticity coordinates. Adobe RGB or Display P3 have different primaries and therefore different matrices.
Step 1: linearization
sRGB values are encoded nonlinearly (see gamma correction). The matrix works on values proportional to light, so the encoding has to be undone first. For each component c in 0..1:
Step 2: the matrix
The middle row is the luminance formula: green contributes over 71% of brightness, blue just over 7%. White (1, 1, 1) gives X = 0.9505, Y = 1, Z = 1.0888, exactly the D65 white point.
The way back: XYZ to RGB
Worked example
RGB (200, 80, 40), #c85028.
Note the linearization: an encoded R of 0.78 is only 0.58 of the light, and G = 0.31 is a mere 0.08. Skipping this step is the most common mistake in color conversion, for this color it gives L* = 69.7 instead of 49.7.
From this XYZ you can go on to CIELAB and CIELUV. For this color: Lab (49.71, 45.77, 46.31).
Out-of-gamut colors
XYZ covers every visible color; sRGB covers only part of them. The reverse conversion can therefore produce negative values or values above 1. Example: X = 0.20, Y = 0.50, Z = 0.10 (a saturated green):
Negative red means this green cannot be displayed in sRGB. The simplest fix is clipping to 0..1 (here roughly RGB (0, 224, 33)), but that changes hue and lightness. Color management software uses better gamut mapping methods that preserve hue at the cost of saturation.
The matrix depends on the white point. When XYZ has to be relative to D50 (for example in ICC profiles), an extra chromatic adaptation step is needed, usually the Bradford method. Skipping it gives a slight bluish or yellowish shift.