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.20% (5.24%*) • Home Loan Fixed: 5.48% (6.24%*) • Fixed: 5.48% (6.24%*) • Variable: 5.20% (5.24%*) • Investment IO: 5.78% (6.81%*) • Investment PI: 5.49% (6.32%*)

Convert an XYZ Color to CIELAB Color Space with PHP

Convert an XYZ Color to CIELAB Color Space with PHP

In 1931, CIE (Commission Internationale de l'Eclairage) defined the CIE XYZ color space, representing all possible colors based on human perception. Like RGB, CIE XYZ has three orthogonal dimensions; however, X, Y and Z do not correspond to real colors - they are mathematically convenient with Y carrying the luminance information. CIE is usually represented by a 2D "chromaticity diagram" obtained from the CIE XYZ model.

Image: The most commonly used system in practical application, aside from the CIE color triangle, is the nearly perception-true L*, a*, b* system that was developed by Judd and Hunter and standardized in 1976. In this system, the L* value indicates the position on the light-dark axis, the a* value indicates the position on the red/green axis, and the b* value indicates the position on the blue/yellow axis).
Convert an XYZ Color to CIELAB Color Space with PHP
The L*, a*, b* coordinates are correlated directly to the standard color values, X, Y, and Z. Source: Zeiss .

The CIE XYZ model and chromaticity diagram are not perceptually uniform. A more uniform version of CIE was defined in 1976, officially known as CIE L*a*b*. L* stands for luminance, a* is the red-green axis, and b* is the blue-yellow axis (referred to as CIELAB). The asterisks were added to differentiate CIE from the Hunter Lab model which is more commonly referred to as Hunter CIE to avoid additional confusion.

Although CIE L*a*b* has a large color gamut and is considered the most accurate color model, it is more commonly used solely as a reference or intermediary for color space conversion. In practice, the RGB and CMYK color spaces are more widely used for display and printing, and HSB and HSL are used for color selection. However, all color spaces, including RGB, CMYK, HSB, HSL and YUV, are subsets of the entire human color gamut as measured and defined by CIE.

The PHP function on this page will convert CIE XYZ to CIELAB.

The PHP Function

1
<?php 
2
/*
3
 Convert an XYZ Color to CIELAB Color Space with PHP
4
 http://www.beliefmedia.com/convert-xyz-cielab
5
*/
6
 
7
function beliefmedia_xyz_labcie($xyz) {
8
 
9
  $xyz['x'] /= 95.047;
10
  $xyz['y'] /= 100;
11
  $xyz['z'] /= 108.883;
12
 
13
  $xyz = array_map(function($item){
14
    if ($item > 0.008856) {
15
      return pow($item, 1/3);
16
        } else {
17
      return (7.787 * $item) + (16 / 116);
18
    }
19
  }, $xyz);
20
 
21
  $cielab = array(
22
    'l' => (116 * $xyz['y']) - 16,
23
    'a' => 500 * ($xyz['x'] - $xyz['y']),
24
    'b' => 200 * ($xyz['y'] - $xyz['z'])
25
  );
26
 
27
 return $cielab;
28
}
29
 
30
/* Usage (close pre tags) */
31
$xyz = array('x'=>'41.24','y'=>'21.26','z'=>'1.93');
32
echo 'pre' . print_r(beliefmedia_xyz_labcie($xyz), true) . '/pre';
33
?>

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 an XYZ Color to CIELAB Color Space with PHP
Description: Convert an XYZ Color to CIELAB Color Space with PHP.
  Download • Version 0.2, 598.0B, zip, Category: PHP Code & Snippets

Download our 650-page guide on Finance Marketing. We'll show you exactly how we generate Billions in volume for our clients.

  E. Australia Standard Time [ UTC+10, Default ] [ CHECK TO CHANGE ]

  Want to have a chat?
 

RELATED READING

Like this article?

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

Leave a comment