Computing RGB-XYZ conversion matrix

We will get matrix M which converts RGB to XYZ:

X )

( R
Y = M G

Note that RGB are the linear values without gamma correction. The coordinates of the white point and the primary color points in the XYZ space are necessary as the conditions to determine the matrix:

White point:   (Xw, Yw, Zw)
R point:   (xr, yr, zr)
G point:   (xg, yg, zg)
B point:   (xb, yb, zb)

The values are given in the sRGB or Adobe RGB specifications. These are all that are given.

In the above, (xr, yr, zr) are the proportions of (Xr, Yr, Zr) values for R point (R, G, B) = (1, 0, 0), i.e.,

xr = Xr / (Xr+Yr+Zr),   yr = Yr / (Xr+Yr+Zr),   zr = Zr / (Xr+Yr+Zr) = 1−xryr (1)

Similar relation holds for G and B points as well. If the white point is given as (xw, yw), use these relations for conversion: Xw = xw / ywYw = 1Zw = zw / yw .

Now, from the fact that matrix M converts R point (R, G, B) = (1, 0, 0) to (Xr, Yr, Zr), the following relation holds:

Xr )

( 1
Yr = M 0

Since similar relations hold for G and B points, matrix M can be written as:

( Xr  Xg  Xb )
M =
Yr Yg Yb (2)

Zr Zg Zb

By the way, from equation (1) we see that Xr can be written as xr multiplied by Xr+Yr+Zr, and so on. Therefore, if we put the sum of three components as

Xr+Yr+Zr = Sr,   Xg+Yg+Zg = Sg,   Xb+Yb+Zb = Sb,

then we get

Xr=Srxr,   Yr=Sryr,   Zr=Srzr,   Xg=Sgxg,   Yg=Sgyg,   Zg=Sgzg,   Xb=Sbxb,   Yb=Sbyb,   Zb=Sbzb.

Graphically, Sr etc. is the scale factor for converting a vector (xr, yr, zr) etc. into a vector (Xr, Yr, Zr) etc. (see Fig. 1)
Now we can express the matrix M in a simple form:

( Srxr  Sgxg  Sbxb )
M =
Sryr Sgyg Sbyb (3)

Srxr Sgxg Sbxb


( xr  xg  xb ) ( Sr 0 0
M =
yr yg yb 0  Sg 0

zr zg zb 0 0  Sb

Thus, the problem has been reduced to determining the three vlues, i.e., Sr, Sg, Sb.
These can be obtained by the fact that the white point  (R, G, B) = (1, 1, 1)  is converted to  (Xw, Yw, Zw)  in the XYZ space. That is,

Xw )

( 1
( xr  xg  xb ) ( Sr 0 0 ) ( 1
( xr  xg  xb ) ( Sr )
Yw = M 1  = 
yr yg yb 0  Sg 0 1  =  yr  yg  yb Sg
zr zg zb 0 0
 Sb 1

zr zg zb Sb

By solving this equation for  Sr, Sg, Sb, we get

Sr )

( xr  xg  xb ) −1
( Xw )
Sg  =  yr  yg yb
Yw (4)
zr zg zb

Now we have  Sr, Sg, Sb. The conversion matrix M can be obtained by substituting these values into equation (3).

(XYZ coordinates for RGB-XYZ conversion matrix)
Fig. 1.  Coordinates for a gumut (sRGB) in the XYZ space

Appendix ― Chromaticity coordinates and white point from a given matrix

For a given conversion matrix M as in equation (2), we can easily obtain three chromaticity coordinates and the white point. First, we should calculate Sr, Sg, Sb as the sum of matrix components in the column direction. That is,

Sr = Xr+Yr+Zr,   Sg = Xg+Yg+Zg,   Sb = Xb+Yb+Zb  

Thus, the chromaticity coordinates can be calculated as follows:

xr=Xr/Sr,   yr=Yr/Sr,   zr=Zr/Sr,   xg=Xg/Sg,   yg=Yg/Sg,   zg=Zg/Sg,   xb=Xb/Sb,   yb=Yb/Sb,   zb=Zb/Sb

Moreover, the white point can be obtained by the following equation:

Xw )

( Xr  Xg  Xb ) ( 1 )
Yw  =  Yr  Yg  Yb 1
Zr Zg Zb 1

T. Fujiwara,  2011/12, updated 2021/10