Created
December 11, 2012 14:51
-
-
Save nkcmr/4259085 to your computer and use it in GitHub Desktop.
HSL to HEX triplet algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function HSL2HEX($h,$s,$l){ | |
function hue2rgb($v1, $v2, $vH){ | |
if($vH<0) $vH += 1; | |
if($vH>1) $vH -= 1; | |
if((6*$vH)<1) return $v1+($v2-$v1)*6*$vH; | |
if((2*$vH)<1) return $v2; | |
if((3*$vH)<2) return $v1+($v2-$v1)*((2/3)-$vH)*6; | |
return $v1; | |
} | |
$h /= 360; | |
$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)); | |
} | |
$r = dechex(round($r)); | |
$g = dechex(round($g)); | |
$b = dechex(round($b)); | |
$r = strlen($r)==1 ? '0'.$r : $r; | |
$g = strlen($g)==1 ? '0'.$g : $g; | |
$b = strlen($b)==1 ? '0'.$b : $b; | |
return $r.$g.$b.''; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment