RGB/XYZ Matrices
Given the chromaticity coordinates of an RGB system (xr, yr), (xg, yg) and (xb, yb) and its reference white (XW, YW, ZW), here is the method to compute the 3 × 3 matrix for converting RGB to XYZ:
Implementation Notes:
1.In order to properly use this matrix, the RGB values must be linear and in the nominal range [0.0, 1.0]. In many cases, RGB values may first need conversion (for example, dividing by 255 and then raising them to a power).
2.Be careful that reference whites are used consistently. For example, sRGB is defined relative to a D65 reference white and ICC profiles are defined relative to a D50 reference white. Mismatched reference whites must be accounted for elsewhere, typically by using a chromatic adaptation algorithm.
3.The inverse matrix (i.e. the matrix converting XYZ to RGB) is computed by inverting matrix [M] above.