# RGB/XYZ Conversion Matrix Accuracy

The second volume of the Foundations of Game Engine Development series (FGED2) contains a derivation of the 3×3 matrices that transform between the standard RGB color space and the CIE XYZ color space. The resulting matrix entries that I published are a little different from the numbers published in both Real-Time Rendering, 3rd edition (RTR3) and Physically Based Rendering, 2nd edition (PBR2). This post is a quick note about how I arrived at my numbers and why they are correct.

On page 14 of FGED2, the following matrix is given for converting XYZ colors to sRGB colors.

And the following matrix is the inverse given for converting back from sRGB to XYZ.

However, on page 215 of RTR3, the same matrix MsRGB has the following entries. (It appears that this matrix was altogether removed from the 4th edition of RTR.)

And the entries of its inverse are the following.

The digits colored red are the ones that differ from the digits published in FGED2. On page 273 of PBR2, the matrix MsRGB is the same as the one in RTR3 except that the center entry has the slightly different value 1.875991. The inverse matrix in PBR2 is identical to the one in RTR3. The fact that RTR3 and PBR2 agree so well suggests that they got their numbers from the same source, but I have not been able to identify that source. (The two sources cited in RTR3 don’t actually provide these matrices.)

So why the difference? FGED2 disagrees with both RTR3 and PBR2 in the final digit of every matrix entry, and the disagreement extends to as many as four decimal places in some entries. To demonstrate that the numbers given by FGED2 are correct, I have carried out their calculation in exact rational arithmetic below. I suspect that the numbers given by RTR3 and PBR2 were calculated in some limited-precision fashion that accumulated rounding errors at each step.

The sRGB specification defines the red, green, and blue primaries as having the exact chromaticity coordinates (xR, yR) = (0.64, 0.33), (xG, yG) = (0.30, 0.60), and (xB, yB) = (0.15, 0.06). The D65 white point used by sRGB has the exact chromaticity coordinates (xW, yW) = (0.3127, 0.3290). Using the relationship z = 1 − xy, we also have zR = 0.03, zG = 0.10, zB = 0.79, and zW = 0.3583. When we plug all of these numbers into Equation (5.9) in FGED2 as exact fractions, we get the following equation.

Solving for the luminances Y produces the exact values shown below.

Using these values in Equation (5.11) gives us the following matrix.

The exact inverse of this matrix is the following.

These matrices were calculated with Mathematica. When we carry out the division for each entry and round to six decimal places, we get the values that are published in FGED2. Because no rounding occurred before these final divisions, we know with certainty that the values are accurate through as many decimal places as we decide to keep.