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.