Question

Okay, I have written a function that should convert HSL color value to RGB. I have rewritten it in PHP according to this script: http://www.easyrgb.com/index.php?X=MATH&H=19#text19

This is what I have:

function HSL2RGB($h, $s, $l){
    function hue2rgb($v1, $v2, $vH){
        $sH = $vH;
        if($vH<0) $sH += 1;
        if($vH>1) $sH -= 1;
        if((6*$sH)<1) return $v1+($v2-$v1)*6*$sH;
        if((2*$sH)<1) return $v2;
        if((3*$sH)<2) return $v1+($v2-$v1)*((2/3)-$sH)*6;
        return $v1;
    }

    $h *= (5/18);
    $s /= 100;
    $l /= 100;
    $r=$g=$b=NULL;
    if($s==0){
        $r=$l*255;
        $g=$l*255;
        $b=$l*255;
    }else{
        if($l<0.5)
            $var_2 = $l*(1+$s);
        else
            $var_2 = ($l+$s)-($s*$l);

        $var_1 = 2*$l-$var_2;
        $r = 255*hue2rgb($var_1, $var_2, $h+(1/3));
        $g = 255*hue2rgb($var_1, $var_2, $h);
        $b = 255*hue2rgb($var_1, $var_2, $h-(1/3));
    }
    return array('r'=>$r,'g'=>$g,'b'=>$b);
}

var_dump(HSL2RGB(196.4, 100, 59.8));

The output of this script:

array(3) {
  ["r"]=>
  float(49.98)
  ["g"]=>
  float(49.98)
  ["b"]=>
  float(49.98)
}

The correct output is R: 50, G:199, B:255. I adapted it perfectly from the reference script from easyrgb.com. I just can't figure out why it isn't working. Any help would be awesome. Thanks.

Was it helpful?

Solution

According to the PHP manual, variable names are case-sensitive in PHP.

Change $sh to $sH in your code.

There may be an error in your PHP code. Add these lines at the beginning of your code:

error_reporting(E_ALL);
ini_set('display_errors', 1);

Why are you using $sH variable? The algorithm you point to is only using vH. Try replacing all $sH with $vH.

In your original code, changing $h *= (5/18) to $h /= 360; will fix your function.

There is one such fonction in the comments here: http://php.net/manual/en/function.imagecolorallocate.php

function hslToRgb ($h, $s, $l) {
    if ($h>240 || $h<0) return array(0,0,0);
    if ($s>240 || $s<0) return array(0,0,0);
    if ($l>240 || $l<0) return array(0,0,0);    
    if ($h<=40) {
        $R=255;
        $G=(int)($h/40*256);
        $B=0;
    }
    elseif ($h>40 && $h<=80) {
        $R=(1-($h-40)/40)*256;
        $G=255;
        $B=0;
    }
    elseif ($h>80 && $h<=120) {
        $R=0;
        $G=255;
        $B=($h-80)/40*256;
    }
    elseif ($h>120 && $h<=160) {
        $R=0;
        $G=(1-($h-120)/40)*256;
        $B=255;
    }
    elseif ($h>160 && $h<=200) {
        $R=($h-160)/40*256;
        $G=0;
        $B=255;
    }
    elseif ($h>200) {
        $R=255;
        $G=0;
        $B=(1-($h-200)/40)*256;
    }
    $R=$R+(240-$s)/240*(128-$R);
    $G=$G+(240-$s)/240*(128-$G);
    $B=$B+(240-$s)/240*(128-$B);
    if ($l<120) {
        $R=($R/120)*$l;
        $G=($G/120)*$l;
        $B=($B/120)*$l;
    }
    else {
        $R=$l*((256-$R)/120)+2*$R-256;
        $G=$l*((256-$G)/120)+2*$G-256;
        $B=$l*((256-$B)/120)+2*$B-256;
    }
    if ($R<0) $R=0;
    if ($R>255) $R=255;
    if ($G<0) $G=0;
    if ($G>255) $G=255;
    if ($B<0) $B=0;
    if ($B>255) $B=255;

    return array((int)$R,(int)$G,(int)$B);
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top