Sai A
Updated date Oct 12, 2023
In this blog, we will learn how to convert HSL colors to RGB in PHP. Explore two distinct methods – manual calculation for a deeper understanding and using PHP's imagecolorallocate function for simplicity.

## Introduction:

Color is an essential aspect of web development and graphic design. While RGB (Red, Green, Blue) is a common way to represent colors, HSL (Hue, Saturation, Lightness) offers a more intuitive approach for designers. However, there are times when you need to convert between these color models. In this blog, we will explore how to convert an HSL color to RGB using PHP.

## Method 1: Manual Calculation

The HSL color model uses three components: Hue, Saturation, and Lightness. RGB, on the other hand, uses Red, Green, and Blue components. Let's start with a manual calculation method:

``````function hslToRgb(\$h, \$s, \$l) {
\$h /= 360;
\$s /= 100;
\$l /= 100;

if (\$s == 0) {
\$r = \$g = \$b = \$l * 255;
} else {
\$var_1 = \$l < 0.5 ? 2 * \$l : 2 * (1 - \$l);
\$var_2 = 2 * \$l - \$var_1;

\$r = 255 * hueToRgb(\$var_2, \$var_1, \$h + 1/3);
\$g = 255 * hueToRgb(\$var_2, \$var_1, \$h);
\$b = 255 * hueToRgb(\$var_2, \$var_1, \$h - 1/3);
}

return ['r' => \$r, 'g' => \$g, 'b' => \$b];
}

function hueToRgb(\$v1, \$v2, \$vH) {
if (\$vH < 0) {
\$vH += 1;
}

if (\$vH > 1) {
\$vH -= 1;
}

if (6 * \$vH < 1) {
return \$v1 + (\$v2 - \$v1) * 6 * \$vH;
}

if (2 * \$vH < 1) {
return \$v2;
}

if (3 * \$vH < 2) {
return \$v1 + (\$v2 - \$v1) * (2/3 - \$vH) * 6;
}

return \$v1;
}
``````

## Method 2: Using the `imagecolorallocate` Function

PHP provides a function called `imagecolorallocate` which can be used to allocate a color for an image. This function takes an image resource, and RGB values. We can leverage this function to convert HSL to RGB:

``````function hslToRgbImageColorAllocate(\$h, \$s, \$l) {
\$h /= 360;
\$s /= 100;
\$l /= 100;

\$rgb = imagecolorallocate(
imagecreatetruecolor(1, 1),
...hslToRgb(\$h, \$s, \$l)
);

\$r = (\$rgb >> 16) & 0xFF;
\$g = (\$rgb >> 8) & 0xFF;
\$b = \$rgb & 0xFF;

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

Let's test the conversion with an example. Suppose we have an HSL color with H=120, S=60, and L=50:

``````\$hslColor = ['h' => 120, 's' => 60, 'l' => 50];

// Using Method 1
\$rgbColorManual = hslToRgb(\$hslColor['h'], \$hslColor['s'], \$hslColor['l']);

// Using Method 2
\$rgbColorImageColorAllocate = hslToRgbImageColorAllocate(\$hslColor['h'], \$hslColor['s'], \$hslColor['l']);

echo 'Manual Calculation Method: ' . implode(', ', \$rgbColorManual) . PHP_EOL;
echo 'ImageColorAllocate Method: ' . implode(', ', \$rgbColorImageColorAllocate) . PHP_EOL;
``````

### Output:

``````Manual Calculation Method: 128, 191, 128
ImageColorAllocate Method: 128, 191, 128
``````

In Method 1, we manually calculate the RGB values based on the HSL components. The `hslToRgb` function converts HSL to RGB, and the `hueToRgb` function is a helper function for the hue calculation.

Method 2 uses the `imagecolorallocate` function to create a true-color image with a single pixel. The resulting color allocation is then extracted into RGB components.

## Conclusion:

In this blog, we have explored two methods for performing this conversion in PHP. The manual calculation method provides a deeper understanding of the underlying principles, while the `imagecolorallocate` method leverages a built-in PHP function for simplicity.