0

Does anyone know how to convert a Hex color to HSL in PHP? I've searched but the functions that I’ve found don’t convert precisely the color.

Marc Duluc
  • 31
  • 4
  • Does this answer your question? [PHP function to convert HSL to RGB or Hex](https://stackoverflow.com/questions/20423641/php-function-to-convert-hsl-to-rgb-or-hex) – abc Nov 19 '19 at 10:37
  • Thanks for the reply. Unfortunately no, I want to convert Hex to HSL not HSL to Hex. – Marc Duluc Nov 19 '19 at 10:53

2 Answers2

0
function hexToHsl($hex) {
    $hex = array($hex[0].$hex[1], $hex[2].$hex[3], $hex[4].$hex[5]);
    $rgb = array_map(function($part) {
        return hexdec($part) / 255;
    }, $hex);

    $max = max($rgb);
    $min = min($rgb);

    $l = ($max + $min) / 2;

    if ($max == $min) {
        $h = $s = 0;
    } else {
        $diff = $max - $min;
        $s = $l > 0.5 ? $diff / (2 - $max - $min) : $diff / ($max + $min);

        switch($max) {
            case $rgb[0]:
                $h = ($rgb[1] - $rgb[2]) / $diff + ($rgb[1] < $rgb[2] ? 6 : 0);
                break;
            case $rgb[1]:
                $h = ($rgb[2] - $rgb[0]) / $diff + 2;
                break;
            case $rgb[2]:
                $h = ($rgb[0] - $rgb[1]) / $diff + 4;
                break;
        }

        $h /= 6;
    }

    return array($h, $s, $l);
}
Piyush Shukla
  • 240
  • 1
  • 11
  • you might want to show the original [source](https://gist.github.com/bedeabza/10463089) of this script... – lovelace Nov 19 '19 at 09:57
  • I've already tried this one. It's not working properly for some colors. For example for #fbffe0 the HSL should be (68, 100%, 94%) which it isn't the case with the function. – Marc Duluc Nov 19 '19 at 10:27
0

Rewritten (and adjusted a bit) from javascript from https://css-tricks.com/converting-color-spaces-in-javascript/

<?php

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}%)";
}

Example:

<?php

echo hexToHsl('fbffe0'); // outputs 'hsl(68, 100%, 94%)'
Rikudou_Sennin
  • 1,202
  • 10
  • 24
  • Thanks for your reply. While it's working for # fbffe0, it's not working properly for some colors. For example for #f5efe1 the HSL should be (42, 50%, 92%) whereas the function returns hsl (0, 100%, 92%). The yellow shade is turned into pink shade. – Marc Duluc Nov 19 '19 at 16:08