Created
November 7, 2015 11:41
-
-
Save 08euccs014/e3f20b0a90a075b2250d to your computer and use it in GitHub Desktop.
PHP function for converting HSV colours to RGB colours
This file contains hidden or 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 | |
| /* | |
| ** 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(floor($dH)) { | |
| case 0: | |
| $dR = $dC; $dG = $dX; $dB = 0.0; break; | |
| case 1: | |
| $dR = $dX; $dG = $dC; $dB = 0.0; break; | |
| case 2: | |
| $dR = 0.0; $dG = $dC; $dB = $dX; break; | |
| case 3: | |
| $dR = 0.0; $dG = $dX; $dB = $dC; break; | |
| case 4: | |
| $dR = $dX; $dG = 0.0; $dB = $dC; break; | |
| case 5: | |
| $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