How do I transform between CIE XYZ and a particular set of RGB primaries?
RGB values in a particular set of primaries can be transformed to and from CIE XYZ by a three-by-three matrix transform. These transforms involve tristimulus values, that is, sets of three linear-light components that conform to the CIE colour matching functions. CIE XYZ is a special case of tristimulus values. In XYZ, any colour is represented by a positive set of values. Details can be found in SMPTE RP 177-1993 [11]. To transform from CIE XYZ into Rec. 709 RGB (with its D65 white point), use this transform: This matrix has some negative coefficients: XYZ colours that are out of gamut for a particular RGB transform to RGB where one or more RGB components is negative or greater than unity. Here’s the inverse transform. Because white is normalized to unity, the middle row sums to unity: To recover primary chromaticities from such a matrix, compute little x and y for each RGB column vector. To recover the white point, transform RGB=[1, 1, 1] to XYZ, then compute x and y.