thara Posted July 13, 2020 Share Posted July 13, 2020 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. Quote Link to comment Share on other sites More sharing options...
requinix Posted July 13, 2020 Share Posted July 13, 2020 $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. Quote Link to comment Share on other sites More sharing options...
Barand Posted July 13, 2020 Share Posted July 13, 2020 Your method has a fatal problem with greys (ie when R = G = B) Quote Link to comment Share on other sites More sharing options...
requinix Posted July 13, 2020 Share Posted July 13, 2020 24 minutes ago, Barand said: Your method has a fatal problem with greys (ie when R = G = B) You mean when $delta === 0? Quote Link to comment Share on other sites More sharing options...
Barand Posted July 13, 2020 Share Posted July 13, 2020 Yes, if all 3 components are equal then $delta is zero, giving a division by zero error. Quote Link to comment Share on other sites More sharing options...
requinix Posted July 13, 2020 Share Posted July 13, 2020 In the $hue calculations? The first branch handles the $delta=0 case. Quote Link to comment Share on other sites More sharing options...
Barand Posted July 13, 2020 Share Posted July 13, 2020 $templatePrimaryColor = '555555'; echo '<pre>',print_r(hexToHsl($templatePrimaryColor)).'</pre>'; -> Warning: Division by zero in C:\inetpub\wwwroot\test\winuser\noname3.php on line 14 Quote Link to comment Share on other sites More sharing options...
Barand Posted July 13, 2020 Share Posted July 13, 2020 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) { 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.