Computing RGBXYZ conversion matrix
We will get matrix M which converts RGB to XYZ:
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: (X_{w},
Y_{w},
Z_{w})
R point: (x_{r}, y_{r},
z_{r})
G point: (x_{g}, y_{g},
z_{g})
B point: (x_{b}, y_{b},
z_{b})
The values are given in the sRGB or Adobe RGB specifications.
These are all that are given.
In the above,
(x_{r}, y_{r}, z_{r})
are the proportions of (X_{r},
Y_{r}, Z_{r}) values
for R point (R, G, B) = (1, 0, 0), i.e.,
x_{r}
= X_{r} /
(X_{r}+Y_{r}+Z_{r}),
y_{r} = Y_{r} /
(X_{r}+Y_{r}+Z_{r}),
z_{r} = Z_{r} /
(X_{r}+Y_{r}+Z_{r}) = 1−x_{r}−y_{r} 
(1)

Similar relation holds for G and B points as well.
If the white point is given as (x_{w}, y_{w}),
use these relations for conversion:
X_{w} = x_{w} / y_{w},
Y_{w} = 1,
Z_{w} = z_{w} / y_{w} .
Now, from the fact that matrix M converts R point
(R, G, B) = (1, 0, 0)
to (X_{r}, Y_{r}, Z_{r}),
the following relation holds:

X_{r} 



1


Y_{r} 
= M 
0

Z_{r} 

0

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


X_{r} 
X_{g} 
X_{b} 


M =

Y_{r} 
Y_{g} 
Y_{b} 
(2)


Z_{r} 
Z_{g} 
Z_{b} 

By the way, from equation (1) we see that
X_{r}
can be written as
x_{r} multiplied by
X_{r}+Y_{r}+Z_{r},
and so on.
Therefore, if we put the sum of three components as
X_{r}+Y_{r}+Z_{r} = S_{r},
X_{g}+Y_{g}+Z_{g} = S_{g},
X_{b}+Y_{b}+Z_{b} = S_{b},
then we get
X_{r}=S_{r}x_{r},
Y_{r}=S_{r}y_{r},
Z_{r}=S_{r}z_{r},
X_{g}=S_{g}x_{g},
Y_{g}=S_{g}y_{g},
Z_{g}=S_{g}z_{g},
X_{b}=S_{b}x_{b},
Y_{b}=S_{b}y_{b},
Z_{b}=S_{b}z_{b}.
Graphically, S_{r} etc. is the scale factor for converting a vector
(x_{r}, y_{r}, z_{r}) etc.
into a vector (X_{r}, Y_{r}, Z_{r}) etc.
(see Fig. 1)
Now we can express the matrix M in a simple form:


S_{r}x_{r} 
S_{g}x_{g} 
S_{b}x_{b} 


M =

S_{r}y_{r} 
S_{g}y_{g} 
S_{b}y_{b} 
(3)


S_{r}x_{r} 
S_{g}x_{g} 
S_{b}x_{b} 

or


x_{r} 
x_{g} 
x_{b} 


S_{r} 
0 
0


M =

y_{r} 
y_{g} 
y_{b} 
0 
S_{g} 
0 

z_{r} 
z_{g} 
z_{b} 
0 
0 
S_{b} 
Thus, the problem has been reduced to determining the three vlues, i.e.,
S_{r},
S_{g},
S_{b}.
These can be obtained by the fact that the white point
(R, G, B) = (1, 1, 1)
is converted to
(X_{w}, Y_{w}, Z_{w})
in the XYZ space.
That is,

X_{w} 



1




x_{r} 
x_{g} 
x_{b} 


S_{r} 
0 
0 


1




x_{r} 
x_{g} 
x_{b} 


S_{r} 

Y_{w} 
= M 
1 
=

y_{r} 
y_{g} 
y_{b} 
0 
S_{g} 
0 
1 
= 
y_{r} 
y_{g} 
y_{b} 
S_{g} 
Z_{w} 

1 

z_{r} 
z_{g} 
z_{b} 
0 
0

S_{b} 
1


z_{r} 
z_{g} 
z_{b} 
S_{b} 
By solving this equation for
S_{r}, S_{g}, S_{b}, we get

S_{r} 



x_{r} 
x_{g} 
x_{b} 

^{−1}


X_{w} 


S_{g} 
= 
y_{r} 
y_{g} 
y_{b} 

Y_{w} 
(4)

S_{b} 

z_{r} 
z_{g} 
z_{b} 

Z_{w} 

Now we have
S_{r}, S_{g}, S_{b}.
The conversion matrix
M can be obtained by substituting these values into equation (3).
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 S_{r}, S_{g}, S_{b}
as the sum of matrix components in the column direction.
That is,
S_{r} = X_{r}+Y_{r}+Z_{r},
S_{g} = X_{g}+Y_{g}+Z_{g},
S_{b} = X_{b}+Y_{b}+Z_{b}
Thus, the chromaticity coordinates can be calculated as follows:
x_{r}=X_{r}/S_{r},
y_{r}=Y_{r}/S_{r},
z_{r}=Z_{r}/S_{r},
x_{g}=X_{g}/S_{g},
y_{g}=Y_{g}/S_{g},
z_{g}=Z_{g}/S_{g},
x_{b}=X_{b}/S_{b},
y_{b}=Y_{b}/S_{b},
z_{b}=Z_{b}/S_{b}
Moreover, the white point can be obtained by the following equation:

X_{w} 



X_{r} 
X_{g} 
X_{b} 


1 

Y_{w} 
= 
Y_{r} 
Y_{g} 
Y_{b} 
1 
Z_{w} 

Z_{r} 
Z_{g} 
Z_{b} 
1 
Back
T. Fujiwara, 2011/12, updated 2021/10