RGB to XYZ

Given an RGB color whose components are in the nominal range [0.0, 1.0] and whose gamma is γ:

Equation 1

where, if the RGB system is not sRGB:

Equation 2

and if it is sRGB:

Equation 3

Implementation Notes:

  1. The transformation matrix [M] is calculated from the RGB reference primaries as discussed here.
  2. The gamma values for many common RGB color spaces may be found here.
  3. Your input RGB values may need to be scaled before using the above. For example, if your values are in the range [0, 255], you must first divide each by 255.0.
  4. The output XYZ values are in the nominal range [0.0, 1.0].
  5. The XYZ values will be relative to the same reference white as the RGB system. If you want XYZ relative to a different reference white, you must apply a chromatic adaptation transform to the XYZ color to convert it from the reference white of the RGB system to the desired reference white.
  6. Sometimes the more complicated special case of sRGB shown above is replaced by a "simplified" version using a straight gamma function with γ = 2.2.