Skip to content

Instantly share code, notes, and snippets.

@kkirby
Last active March 31, 2020 22:57
Show Gist options
  • Save kkirby/10c1bd47ac10f90b521eb38f47b94f12 to your computer and use it in GitHub Desktop.
Save kkirby/10c1bd47ac10f90b521eb38f47b94f12 to your computer and use it in GitHub Desktop.
PHP version of HSL to RGB
<?php
// Original Credit goes to:
// Paul S. @ StackOverflow https://stackoverflow.com/a/17243070
// This has been converted from JavaScript.
//
// I was unable to find a pre-made solution in PHP that worked correct.
// Specifically: HSL(360,87,87)
function HSV_TO_RGB($h, $s, $v) {
$i = floor($h * 6);
$f = $h * 6 - $i;
$p = $v * (1 - $s);
$q = $v * (1 - $f * $s);
$t = $v * (1 - (1 - $f) * $s);
switch ($i % 6) {
case 0: $r = $v; $g = $t; $b = $p; break;
case 1: $r = $q; $g = $v; $b = $p; break;
case 2: $r = $p; $g = $v; $b = $t; break;
case 3: $r = $p; $g = $q; $b = $v; break;
case 4: $r = $t; $g = $p; $b = $v; break;
case 5: $r = $v; $g = $p; $b = $q; break;
}
return [
'r' => round($r * 255),
'g' => round($g * 255),
'b' => round($b * 255)
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment