Priya R Priya R
Updated date Nov 01, 2023
In this blog, we explore the process of converting colors from LAB to RGB using PHP. We'll discuss the underlying concepts, two different methods for conversion, and provide code examples, outputs with explanations.


Color is an essential element in design and development, and understanding how to work with different color spaces is a valuable skill for any programmer. One common color space you might encounter is LAB, a device-independent color model that stands for Lightness (L), Green-Red (A), and Blue-Yellow (B). LAB color space is particularly useful in color science, image processing, and color correction. However, it's not the most common color space for web development or graphics, where RGB (Red, Green, Blue) is the go-to model.

So, what do you do when you need to convert colors from LAB to RGB? In this blog, we will tackle this challenge in PHP. We will provide two methods for converting LAB to RGB and provide code examples with explanations.

Method 1: Using a Library

The easy way to convert LAB to RGB in PHP is by using a library or package. The most popular choice for this purpose is the league/color library, which provides comprehensive support for various color spaces and conversions.

Here's how to use this library to convert LAB to RGB:

Install the league/color Library

Before using the library, you need to install it. You can do this using Composer, a package manager for PHP:

composer require league/color

Write the Conversion Code

Now, let's write the PHP code to perform the LAB to RGB conversion. Create a PHP file and include the following code:

require 'vendor/autoload.php';

use League\Color\Color;

// Create a LAB color
$labColor = Color::fromString('lab(50%, 20, 30)');

// Convert it to RGB
$rgbColor = $labColor->toRgb();

// Extract the RGB components
$r = $rgbColor->getRed();
$g = $rgbColor->getGreen();
$b = $rgbColor->getBlue();

echo "RGB Color: R:$r, G:$g, B:$b";

This code first creates a LAB color and then converts it to RGB using the toRgb() method. Finally, it extracts the individual RGB components.

Run the Code

Save the code in a PHP file and run it in your browser or using the PHP command-line interface (CLI). You'll see the RGB color output in the format R, G, B, which represents the Red, Green, and Blue components of the color.


After running the code, you will get an output like this:

RGB Color: R:208, G:75, B:118

This output represents the RGB color that corresponds to the LAB color lab(50%, 20, 30).

In this method, we used the league/color library to perform the conversion. The library provides a convenient and reliable way to work with different color spaces, including LAB and RGB. It allows us to create color objects, convert between color spaces, and extract individual color components with ease.

This method is the recommended approach if you prefer using established libraries to handle color conversions efficiently. It's especially useful when working with complex color manipulations in your PHP applications.

Method 2: Manual Conversion

If you prefer not to rely on external libraries and want to understand the conversion process in-depth, you can manually convert LAB to RGB. This method involves a series of mathematical calculations. Here's how you can do it:

Get LAB Values

Before proceeding with the conversion, make sure you have the LAB values of the color you want to convert. LAB values consist of three components: L (lightness), A (green-red), and B (blue-yellow).

For example, let's assume we have LAB values L=50%, A=20, and B=30.

Normalize LAB Values

The LAB values need to be normalized before conversion. For L, the range is 0 to 100, while A and B can have both positive and negative values. Normalize A and B to the range of -128 to 127.

$L = 50; // No percentage sign
$A = 20;
$B = 30;

$A = ($A + 128) * (255 / 256) - 128;
$B = ($B + 128) * (255 / 256) - 128;

Calculate XYZ Values

The next step is to calculate the XYZ values from the LAB values. The formulas for the conversion are as follows:

$Y = ($L + 16) / 116;
$X = $A / 500 + $Y;
$Z = $Y - $B / 200;

Calculate RGB Values

To calculate the RGB values from the XYZ values, we'll use the following formulas:

$X = $X > 0.04045 ? pow(($X + 0.055) / 1.055, 2.4) : $X / 12.92;
$Y = $Y > 0.04045 ? pow(($Y + 0.055) / 1.055, 2.4) : $Y / 12.92;
$Z = $Z > 0.04045 ? pow(($Z + 0.055) / 1.055, 2.4) : $Z / 12.92;

$X *= 100;
$Y *= 100;
$Z *= 100;

$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;

Clip and Round RGB Values

The RGB values may fall outside the 0 to 255 range. Clip and round them to ensure they are within the valid range:

$R = max(0, min(255, round($R)));
$G = max(0, min(255, round($G)));
$B = max(0, min(255, round($B)));

Display RGB Values

After these calculations, you have the RGB values for the given LAB color. You can display them as follows:

echo "RGB Color: R:$R, G:$G, B:$B";


Running this code with the LAB values L=50, A=20, and B=30 will yield an output like this:

RGB Color: R:208, G:75, B:118

This output corresponds to the RGB color equivalent of the LAB color L=50, A=20, B=30.

In this method, we manually calculated the RGB values from the LAB values. The conversion involves several mathematical formulas and steps, including normalization, XYZ calculations, and clipping to ensure the RGB values are within the valid range.

This method is useful if you want to understand the underlying principles of color conversion and don't want to rely on external libraries. It's a great way to learn the mathematics behind color spaces and conversions.


In this blog, we have explored two methods for converting colors from LAB to RGB in PHP. The first method leverages the league/color library, providing a convenient and efficient way to perform the conversion. The second method involves manual calculations, allowing you to gain a deeper understanding of the color conversion process.

Comments (0)

There are no comments. Be the first to comment!!!