# PHP function to convert Hex to HSL (Not HSL to Hex)

## Recommended Posts

I am trying to convert colors from Hex to HSL (Not HSL to Hex). I am using a PHP function for this purpose. But It's not working properly for some colors. For example for #e04c4c the HSL should be (0, 70%, 59%) which it isn't the case with the function.

```function hexToHsl(\$hex) {
\$red = hexdec(substr(\$hex, 0, 2)) / 255;
\$green = hexdec(substr(\$hex, 2, 2)) / 255;
\$blue = hexdec(substr(\$hex, 4, 2)) / 255;

\$cmin = min(\$red, \$green, \$blue);
\$cmax = max(\$red, \$green, \$blue);
\$delta = \$cmax - \$cmin;

if (\$delta === 0) {
\$hue = 0;
} elseif (\$cmax === \$red) {
\$hue = ((\$green - \$blue) / \$delta) % 6;
} elseif (\$cmax === \$green) {
\$hue = (\$blue - \$red) / \$delta + 2;
} else {
\$hue = (\$red - \$green) / \$delta + 4;
}

\$hue = round(\$hue * 60);
if (\$hue < 0) {
\$hue += 360;
}

\$lightness = ((\$cmax + \$cmin) / 2) * 100;
\$saturation = \$delta === 0 ? 0 : (\$delta / (1 - abs(2 * \$lightness - 1))) * 100;
if (\$saturation < 0) {
\$saturation += 100;
}

\$lightness = round(\$lightness);
\$saturation = round(\$saturation);

//return "hsl(\${hue}, \${saturation}%, \${lightness}%)";
return array(\$hue, \$saturation, \$lightness);
}```

This is how I used it:

```\$templatePrimaryColor = '#e04c4c';

\$templatePrimaryColor = str_replace("#", "",\$templatePrimaryColor);

list(\$h,\$s,\$l) = hexToHsl(\$templatePrimaryColor);

\$primaryColor = "hsl(\${h}, \${s}%, \${l}%)";```

This is the output:

```echo '<pre>',print_r(hexToHsl(\$templatePrimaryColor)).'</pre>';

Array
(
[0] => 0
[1] => 99
[2] => 59
)
1```

Does anyone know what is the problem there?

Thank you.

##### Share on other sites
```    \$lightness = ((\$cmax + \$cmin) / 2) * 100;
\$saturation = \$delta === 0 ? 0 : (\$delta / (1 - abs(2 * \$lightness - 1))) * 100;```

You're multiplying the lightness by 100 too early. This throws off the saturation calculation.

Leave it as \$cmax+\$cmin/2 and multiply when you do the round() later.

##### Share on other sites

Your method has a fatal problem with greys (ie when R = G = B)

##### Share on other sites
24 minutes ago, Barand said:

Your method has a fatal problem with greys (ie when R = G = B)

You mean when \$delta === 0?

##### Share on other sites

Yes, if all 3 components are equal then \$delta is zero, giving a division by zero error.

##### Share on other sites

In the \$hue calculations? The first branch handles the \$delta=0 case.

##### Share on other sites
```\$templatePrimaryColor = '555555';
echo '<pre>',print_r(hexToHsl(\$templatePrimaryColor)).'</pre>';```

->

`Warning:  Division by zero in C:\inetpub\wwwroot\test\winuser\noname3.php on line 14`

##### Share on other sites
43 minutes ago, requinix said:

The first branch handles the \$delta=0 case.

Not quite.

var_dump(\$delta) gives float(0), therefore the first branch needs to be either

`if (\$delta === 0.0) {`

or

`if (\$delta == 0) {`

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×

×

• #### Activity

• Chat
×
• Create New...