function HSL2RGB($h, $s, $l) { $r = $g = $b = 0; if ($s == 0) { $r = $g = $b = $l * 255.0; } else { if ($l < 0.5) { $q = $l * ($s + 1.0); } else { $q = $l + $s - $l * $s; } $p = $l * 2.0 - $q; $h = $h / 360.0; $r = hue2rgb($p, $q, $h + 1.0 / 3.0) * 255.0; $g = hue2rgb($p, $q, $h) * 255.0; $b = hue2rgb($p, $q, $h - 1.0 / 3.0) * 255.0; } return array((int) $r, (int) $g, (int) $b); }
/** * Convert an HSL triplet to an RGB triplet. * * @param array $hsl The HSL triplet. * @return array The RGB triplet. * * @package esoTalk */ function hsl2rgb($hsl) { $h = $hsl[0]; $s = $hsl[1]; $l = $hsl[2]; $m2 = $l <= 0.5 ? $l * ($s + 1) : $l + $s - $l * $s; $m1 = $l * 2 - $m2; return array(hue2rgb($m1, $m2, $h + 0.33333), hue2rgb($m1, $m2, $h), hue2rgb($m1, $m2, $h - 0.33333)); }
function hsl($h = null, $s = null, $l = null, $a = null) { $h = !$h ? 0 : $h; $s = !$s ? 100 : $s; $l = !$l ? 50 : $l; $h = $h >= 360 || $h < 0 ? 0 : $h; $h /= 360; $s = $s > 100 ? 100 : $s; $s = $s < 0 ? 0 : $s; $s /= 100; $l = $l > 100 ? 100 : $l; $l = $l < 0 ? 0 : $l; $l /= 100; $r; $g; $b; if ($s == 0) { $r = $g = $b = $l; } else { function hue2rgb($p, $q, $t) { if ($t < 0) { $t += 1; } if ($t > 1) { $t -= 1; } if ($t < 1 / 6) { return $p + ($q - $p) * 6 * $t; } if ($t < 1 / 2) { return $q; } if ($t < 2 / 3) { return $p + ($q - $p) * (2 / 3 - $t) * 6; } return $p; } $q = $l < 0.5 ? $l * (1 + $s) : $l + $s - $l * $s; $p = 2 * $l - $q; $r = hue2rgb($p, $q, $h + 1 / 3); $g = hue2rgb($p, $q, $h); $b = hue2rgb($p, $q, $h - 1 / 3); } if ($a !== null) { if (0 > $a) { $a = 0; } elseif ($a > 1) { $a = 1; } return array($r, $g, $b, $a); } return array($r, $g, $b, 1); }