Convert a HEX Color Value or RGB Array to CIE XYZ

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 brightnesses 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.

The pre tags in the example code above need to be closed (they were removed for page formatting purposes).

Download the Function

Download the PHP function here .

Shortt URL for this post:

Leave a Reply

Your email address will not be published. Required fields are marked *