Skip to content

Instantly share code, notes, and snippets.

@joshrp
Created March 19, 2013 22:55
Show Gist options
  • Select an option

  • Save joshrp/5200913 to your computer and use it in GitHub Desktop.

Select an option

Save joshrp/5200913 to your computer and use it in GitHub Desktop.
<?php
/*
** Converts HSV to RGB values
** –––––––––––––––––––––––––––––––––––––––––––––––––––––
** Reference: http://en.wikipedia.org/wiki/HSL_and_HSV
** Purpose: Useful for generating colours with
** same hue-value for web designs.
** Input: Hue (H) Integer 0-360
** Saturation (S) Integer 0-100
** Lightness (V) Integer 0-100
** Output: String "R,G,B"
** Suitable for CSS function RGB().
*/
function fGetRGB($iH, $iS, $iV) {
if($iH < 0) $iH = 0; // Hue:
if($iH > 360) $iH = 360; // 0-360
if($iS < 0) $iS = 0; // Saturation:
if($iS > 100) $iS = 100; // 0-100
if($iV < 0) $iV = 0; // Lightness:
if($iV > 100) $iV = 100; // 0-100
$dS = $iS/100.0; // Saturation: 0.0-1.0
$dV = $iV/100.0; // Lightness: 0.0-1.0
$dC = $dV*$dS; // Chroma: 0.0-1.0
$dH = $iH/60.0; // H-Prime: 0.0-6.0
$dT = $dH; // Temp variable
while($dT >= 2.0) $dT -= 2.0; // php modulus does not work with float
$dX = $dC*(1-abs($dT-1)); // as used in the Wikipedia link
switch($dH) {
case($dH >= 0.0 && $dH < 1.0):
$dR = $dC; $dG = $dX; $dB = 0.0; break;
case($dH >= 1.0 && $dH < 2.0):
$dR = $dX; $dG = $dC; $dB = 0.0; break;
case($dH >= 2.0 && $dH < 3.0):
$dR = 0.0; $dG = $dC; $dB = $dX; break;
case($dH >= 3.0 && $dH < 4.0):
$dR = 0.0; $dG = $dX; $dB = $dC; break;
case($dH >= 4.0 && $dH < 5.0):
$dR = $dX; $dG = 0.0; $dB = $dC; break;
case($dH >= 5.0 && $dH < 6.0):
$dR = $dC; $dG = 0.0; $dB = $dX; break;
default:
$dR = 0.0; $dG = 0.0; $dB = 0.0; break;
}
$dM = $dV - $dC;
$dR += $dM; $dG += $dM; $dB += $dM;
$dR *= 255; $dG *= 255; $dB *= 255;
return round($dR).",".round($dG).",".round($dB);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment