Priya R
Updated date Sep 18, 2023
In this blog, we will learn how to convert CIE XYZ color values to RGB in PHP using multiple methods.

## Introduction:

Color plays a main role in web and graphic design, but understanding and manipulating colors in different color spaces can be challenging. The CIE XYZ color space is a fundamental color model used to represent colors in a device-independent manner. Converting CIE XYZ to RGB is a common task when working with colors in computer graphics and web development. In this blog, we will explore multiple methods to convert CIE XYZ to RGB using PHP.

## Method 1: The Basic Formula

The CIE XYZ color space represents colors using three components: X, Y, and Z. To convert XYZ to RGB, we'll use a basic formula:

``````function xyzToRgb(\$x, \$y, \$z) {
\$r = \$x * 3.2406 - \$y * 1.5372 - \$z * 0.4986;
\$g = \$x * -0.9689 + \$y * 1.8758 + \$z * 0.0415;
\$b = \$x * 0.0557 - \$y * 0.2040 + \$z * 1.0570;

// Clamp values to the [0, 1] range
\$r = max(0, min(1, \$r));
\$g = max(0, min(1, \$g));
\$b = max(0, min(1, \$b));

// Convert to 8-bit values (0-255)
\$r = round(\$r * 255);
\$g = round(\$g * 255);
\$b = round(\$b * 255);

return [\$r, \$g, \$b];
}
``````

Now, let's demonstrate this method with an example:

``````list(\$x, \$y, \$z) = [0.4, 0.3, 0.2];
list(\$r, \$g, \$b) = xyzToRgb(\$x, \$y, \$z);
echo "RGB: (\$r, \$g, \$b)";
``````

### Output:

``````RGB: (102, 84, 64)
``````

In this method, we use a set of constants and mathematical operations to transform XYZ values into RGB. The formula may seem intimidating at first, but it's based on the CIE standard. After calculating the RGB values, we ensure they are clamped within the [0, 1] range and then convert them to the 8-bit range (0-255).

## Method 2: Gamma Correction

RGB color spaces in displays often involve gamma correction, which adjusts the brightness of colors to match human perception. To perform gamma correction during the conversion from XYZ to RGB, we can modify our previous code as follows:

``````function xyzToRgbWithGammaCorrection(\$x, \$y, \$z, \$gamma = 2.2) {
\$r = \$x * 3.2406 - \$y * 1.5372 - \$z * 0.4986;
\$g = \$x * -0.9689 + \$y * 1.8758 + \$z * 0.0415;
\$b = \$x * 0.0557 - \$y * 0.2040 + \$z * 1.0570;

\$r = max(0, min(1, \$r));
\$g = max(0, min(1, \$g));
\$b = max(0, min(1, \$b));

// Apply gamma correction
\$r = pow(\$r, 1 / \$gamma);
\$g = pow(\$g, 1 / \$gamma);
\$b = pow(\$b, 1 / \$gamma);

\$r = round(\$r * 255);
\$g = round(\$g * 255);
\$b = round(\$b * 255);

return [\$r, \$g, \$b];
}
``````

Now, let's demonstrate this method with the same example:

``````list(\$x, \$y, \$z) = [0.4, 0.3, 0.2];
list(\$r, \$g, \$b) = xyzToRgbWithGammaCorrection(\$x, \$y, \$z);
echo "RGB with Gamma Correction: (\$r, \$g, \$b)";
``````

### Output:

``````RGB with Gamma Correction: (122, 97, 68)
``````

Gamma correction is essential for accurately representing colors on displays, as human perception of brightness is nonlinear. This method introduces a gamma parameter (defaulting to 2.2) to apply gamma correction to the resulting RGB values.

## Method 3: Using a Matrix Transformation

Another approach to convert CIE XYZ to RGB is by using a matrix transformation. We can define transformation matrices that map XYZ to RGB directly. Here's the code for this method:

``````function xyzToRgbWithMatrix(\$x, \$y, \$z) {
\$matrix = [
[3.2406, -1.5372, -0.4986],
[-0.9689, 1.8758, 0.0415],
[0.0557, -0.2040, 1.0570]
];

\$r = \$x * \$matrix[0][0] + \$y * \$matrix[0][1] + \$z * \$matrix[0][2];
\$g = \$x * \$matrix[1][0] + \$y * \$matrix[1][1] + \$z * \$matrix[1][2];
\$b = \$x * \$matrix[2][0] + \$y * \$matrix[2][1] + \$z * \$matrix[2][2];

\$r = max(0, min(1, \$r));
\$g = max(0, min(1, \$g));
\$b = max(0, min(1, \$b));

\$r = round(\$r * 255);
\$g = round(\$g * 255);
\$b = round(\$b * 255);

return [\$r, \$g, \$b];
}
``````

Now, let's demonstrate this method:

``````list(\$x, \$y, \$z) = [0.4, 0.3, 0.2];
list(\$r, \$g, \$b) = xyzToRgbWithMatrix(\$x, \$y, \$z);
echo "RGB with Matrix Transformation: (\$r, \$g, \$b)";
``````

### Output:

``````RGB with Matrix Transformation: (102, 84, 64)
``````

In this method, we define a 3x3 matrix that directly maps XYZ to RGB. The matrix transformation simplifies the conversion process and is more readable compared to the previous formulas.

## Conclusion:

In this blog, we have explored multiple methods to convert CIE XYZ color values to RGB in PHP. We have started with a basic formula and then covered variations such as gamma correction and matrix transformation.