Home Blog Convert a HEX Color Value or RGB Array to CIE XYZ

# Convert a HEX Color Value or RGB Array to CIE XYZ

0

The CIE XYZ color model (created in 1931) is a ‘device-independent’ colour space in which each primary colour (X,Y,Z) is always constant, unlike RGB model where color variances occur with every individual device that applies the model (monitors, scanners, cameras, etc.). The CIE XYZ color space encompasses all color sensations that an average person can experience. As such, the model serves as a standard reference against which many other color spaces are defined.

The PHP function on this page will convert a #HEX value, or an RGB array, into XYZ.

### Meaning of X, Y and Z

Source: Wikipedia .

When judging the relative luminance (brightness) of different colors in well-lit situations, humans tend to perceive light within the green parts of the spectrum as brighter than red or blue light of equal power. The luminosity function that describes the perceived brightness of different wavelengths is thus roughly analogous to the spectral sensitivity of M cones.

The CIE model capitalises on this fact by defining Y as luminance. Z is quasi-equal to blue stimulation, or the S cone response, and X is a mix (a linear combination) of cone response curves chosen to be nonnegative. The XYZ tristimulus values are thus analogous to, but different to, the LMS cone responses of the human eye. Defining Y as luminance has the useful result that for any given Y value, the XZ plane will contain all possible chromaticities at that luminance.

There’s a good video here on YouTube that provides a visual representation of the XYZ color space.

### The PHP Function

The function works with an array of RGB values (with keys of r, g, and b). While the function accepts a 6-figure HEX value, that reference is first converted to RGB for the purpose of the subsequent conversion.

### Considerations

• The `pre` tags in the example code above need to be closed (they were removed for page formatting purposes). The download includes working code.
• We have a number of color posts scheduled (and published) that’ll work with color data in various ways; they will all be available via the color tag.