RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.38% (6.14%*) • Home Loan Fixed: 5.44% (6.26%*) • Fixed: 5.44% (6.26%*) • Variable: 5.38% (6.14%*) • Investment IO: 5.69% (6.52%*) • Investment PI: 5.49% (5.98%*)

Convert a HEX Color Value or RGB Array to CIE XYZ

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

1
<?php 
2
/*
3
 Convert a HEX Color Value or RGB Array to CIE XYZ
4
 http://www.beliefmedia.com/convert-hex-rgb-cie-xyz
5
*/
6
 
7
function beliefmedia_hex_xyz($color) {
8
 
9
  if ( (!is_array($color)) && (preg_match('/#?[a-fA-F0-9]{1,8}/', $color)) ) {
10
    $color = str_replace('0x','', $color);
11
    $color = str_replace('#','', $color);
12
    $rgb['r'] = hexdec(substr($color, 0, 2)); $rgb['g'] = hexdec(substr($color, 2, 2)); $rgb['b'] = hexdec(substr($color, 4, 2));
13
     } else {
14
    $rgb = $color;
15
  }
16
 
17
  /* Normalises RGB values to 1 */
18
  $rgb = array_map(function($item) {
19
    return $item / 255;
20
  }, $rgb);
21
 
22
  $rgb = array_map(function($item) {
23
      if ($item > 0.04045) {
24
        $item = pow((($item + 0.055) / 1.055), 2.4);
25
          } else {
26
        $item = $item / 12.92;
27
      }
28
    return ($item * 100);
29
  }, $rgb);
30
 
31
  $xyz = array(
32
    'x' => ($rgb['r'] * 0.4124) + ($rgb['g'] * 0.3576) + ($rgb['b'] * 0.1805),
33
    'y' => ($rgb['r'] * 0.2126) + ($rgb['g'] * 0.7152) + ($rgb['b'] * 0.0722),
34
    'z' => ($rgb['r'] * 0.0193) + ($rgb['g'] * 0.1192) + ($rgb['b'] * 0.9505)
35
  );
36
 
37
 return $xyz;
38
}
39
 
40
/* Usage */
41
$rgbarray = array('r'=>'61', 'g'=>'65', 'b'=>'228');
42
echo 'pre' . print_r(beliefmedia_hex_xyz($rgbarray), true) . '/pre';
43
 
44
$item = '#ff0000';
45
echo 'pre' . print_r(beliefmedia_hex_xyz($item), true) . '/pre';

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: color tag.

Download


Title: Convert HEX Color Value or RGB Array to CIE XYZ
Description: Convert a HEX Color Value or RGB Array to CIE XYZ.
  Download • Version 0.2, 781.0B, zip, Category: PHP Code & Snippets

■ ■ ■

 
Download our complimentary 650-page guide on marketing for mortgage brokers. We'll show you exactly how we generate billions in volume for our clients.
Finance Guide, Cropped Top and Bottom
  Timezone: 1 · [ CHANGE ]

RELATED READING

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment