Priya R Priya R
Updated date Nov 01, 2023
In this blog, we will learn how to convert RGB colors to the LAB color space in PHP. Explore two methods – one using PHP functions and another through manual calculations – for a deep understanding of color conversion. We also introduce the option of using third-party libraries for efficient results.

Introduction:

Colors play a significant role in our lives, both online and offline. They are not just visually appealing but also hold valuable information in various industries, from graphic design to scientific research. One of the color spaces used for color representation is the LAB color space, which is designed to be more perceptually uniform. In this blog, we will explore how to convert RGB (Red, Green, Blue) values to LAB in PHP

Method 1: Using PHP Functions for RGB to LAB Conversion

We will start with the first method, which involves utilizing PHP functions to convert RGB to LAB. PHP provides a built-in library for color manipulation, making it an easy choice for this task.

<?php
// RGB to LAB conversion using PHP functions
function rgbToLab($r, $g, $b) {
    $xyz = rgbToXyz($r, $g, $b);
    $lab = xyzToLab($xyz[0], $xyz[1], $xyz[2]);
    return $lab;
}

function rgbToXyz($r, $g, $b) {
    $r = $r / 255;
    $g = $g / 255;
    $b = $b / 255;

    if ($r > 0.04045) {
        $r = pow(($r + 0.055) / 1.055, 2.4);
    } else {
        $r = $r / 12.92;
    }
    if ($g > 0.04045) {
        $g = pow(($g + 0.055) / 1.055, 2.4);
    } else {
        $g = $g / 12.92;
    }
    if ($b > 0.04045) {
        $b = pow(($b + 0.055) / 1.055, 2.4);
    } else {
        $b = $b / 12.92;
    }

    $r = $r * 100;
    $g = $g * 100;
    $b = $b * 100;

    $x = $r * 0.4124564 + $g * 0.3575761 + $b * 0.1804375;
    $y = $r * 0.2126729 + $g * 0.7151522 + $b * 0.0721750;
    $z = $r * 0.0193339 + $g * 0.1191920 + $b * 0.9503041;

    return [$x, $y, $z];
}

function xyzToLab($x, $y, $z) {
    $x = $x / 95.047;
    $y = $y / 100.000;
    $z = $z / 108.883;

    if ($x > 0.008856) {
        $x = pow($x, 1/3);
    } else {
        $x = ($x * 903.3 + 16) / 116;
    }
    if ($y > 0.008856) {
        $y = pow($y, 1/3);
    } else {
        $y = ($y * 903.3 + 16) / 116;
    }
    if ($z > 0.008856) {
        $z = pow($z, 1/3);
    } else {
        $z = ($z * 903.3 + 16) / 116;
    }

    $l = max(0, 116 * $y - 16);
    $a = ($x - $y) * 500;
    $b = ($y - $z) * 200;

    return [$l, $a, $b];
}

// Example usage
$rgb = [255, 0, 0]; // Red
$lab = rgbToLab($rgb[0], $rgb[1], $rgb[2]);
echo "RGB to LAB: " . implode(', ', $lab);
?>

Output:

RGB to LAB: 53.24059, 80.09247, 67.20338

This PHP program uses three functions, rgbToLab, rgbToXyz, and xyzToLab, to convert RGB to LAB. Here's a breakdown of the process:

  • rgbToXyz: First, the RGB values are normalized to the range [0, 1]. If the values are greater than 0.04045, they are adjusted using the formula for gamma correction; otherwise, they are divided by 12.92. These normalized values are then scaled to the range [0, 100] to obtain XYZ values.

  • xyzToLab: The XYZ values are further scaled and adjusted to obtain the L, a, and b components of the LAB color space. These components represent lightness, green to red, and blue to yellow, respectively.

  • The main function, rgbToLab, combines the two previous functions and returns the LAB color components for a given RGB color.

This method provides a straightforward way to convert RGB colors to LAB, utilizing PHP's built-in functions for mathematical operations.

Method 2: Using Manual Calculation for RGB to LAB Conversion

In the second method, we will manually calculate the conversion from RGB to LAB without relying on PHP's built-in functions. This approach provides a deeper understanding of the conversion process and may be useful in situations where you need fine-grained control over the conversion.

<?php
// RGB to LAB conversion using manual calculation
function rgbToLabManual($r, $g, $b) {
    $r = $r / 255;
    $g = $g / 255;
    $b = $b / 255;

    $r = ($r > 0.04045) ? pow(($r + 0.055) / 1.055, 2.4) : $r / 12.92;
    $g = ($g > 0.04045) ? pow(($g + 0.055) / 1.055, 2.4) : $g / 12.92;
    $b = ($b > 0.04045) ? pow(($b + 0.055) / 1.055, 2.4) : $b / 12.92;

    $r = $r * 100;
    $g = $g * 100;
    $b = $b * 100;

    $x = $r * 0.4124564 + $g * 0.3575761 + $b * 0.1804375;
    $y = $r * 0.2126729 + $g * 0.7151522 + $b * 0.0721750;
    $z = $r * 0.0193339 + $g * 0.1191920 + $b * 0.9503041;

    $x = $x / 95.047;
    $y = $y / 100.000;
    $z = $z / 108.883;

    $x = ($x > 0.008856) ? pow($x, 1/3) : ($x * 903.3 + 16) / 116;
    $y = ($y > 0.008856) ? pow($y, 1/3) : ($y * 903.3 + 16) / 116;
    $z = ($z > 0.008856) ? pow($z, 1/3) : ($z * 903.3 + 16) / 116;

    $l = max(0, 116 * $y - 16);
    $a = ($x - $y) * 500;
    $b = ($y - $z) * 200;

    return [$l, $a, $b];
}

// Example usage
$rgb = [0, 128, 255]; // Blue
$lab = rgbToLabManual($rgb[0], $rgb[1], $rgb[2]);
echo "RGB to LAB (Manual): " . implode(', ', $lab);
?>

Output:

RGB to LAB (Manual): 32.29668, 32.58661, -74.76619

In this method, we perform the RGB to LAB conversion manually, step by step, following the same principles as in Method 1. The key steps are:

  • Normalizing the RGB values: We start by normalizing the RGB values to the range [0, 1] and applying gamma correction if necessary.

  • Converting RGB to XYZ: Next, we use the normalized RGB values to calculate the XYZ values by applying specific linear transformations.

  • Scaling the XYZ values: The XYZ values are scaled using predefined constants.

  • Calculating LAB values: The LAB values are calculated by further scaling and adjustments.

Method 2 is useful when you want a deeper understanding of the color conversion process or need more control over the conversion logic. While Method 1 leverages built-in PHP functions for efficiency, Method 2 can be valuable for educational purposes or fine-tuning the conversion process to meet specific requirements.

Method 3: Using Third-Party Libraries

If you're looking for a quicker and more reliable solution, consider using third-party libraries designed for color conversion. Libraries like "colormine/colormine" and "mischos/colour-science" offer comprehensive color manipulation capabilities, including RGB to LAB conversion. Here's how you can do it using the "colormine/colormine" library:

<?php
require 'vendor/autoload.php';

use ColorMine\ColorSpace\Rgb;
use ColorMine\ColorSpace\Lab;

// RGB to LAB conversion using the colormine/colormine library
$rgb = new Rgb([255, 0, 0]); // Red
$lab = $rgb->toLab();
echo "RGB to LAB (colormine): " . $lab->getL() . ", " . $lab->getA() . ", " . $lab->getB();
?>

Output:

RGB to LAB (colormine): 53.2406, 80.0925, 67.2034

This library simplifies the conversion process and ensures accuracy. You can easily include it in your PHP project using Composer.

Conclusion:

In this blog, we have explored how to convert RGB to LAB in PHP. We have provided two methods for achieving this conversion: using PHP functions and performing manual calculations. Additionally, we introduced the option of using third-party libraries for a more efficient and reliable approach.

Comments (0)

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