HSL to RGB color conversion

后端 未结 21 2703
忘了有多久
忘了有多久 2020-11-22 01:59

I am looking for a JavaScript / PHP algorithm to convert between HSL color to RGB.

It seems to me that HSL is not very widely used so I am not having much luck search

21条回答
  •  礼貌的吻别
    2020-11-22 02:27

    PHP - shortest but precise

    Here I rewrite my JS answer (math details are there) to PHP - you can run it here

    function hsl2rgb($h,$s,$l) 
    {
      $a = $s * min($l, 1-$l);
      $k = function($n,$h) { return ($n+$h/30)%12;};
      $f = function($n) use ($h,$s,$l,$a,$k) { 
          return $l - $a * max( min($k($n,$h)-3, 9-$k($n,$h), 1),-1);
      };
      return [ $f(0), $f(8), $f(4) ];
    }   
    

提交回复
热议问题