Advertisement
Cullub

colorHslAndRgb

Dec 29th, 2015
670
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. /**
  4.  * Input: hex color
  5.  * Output: hsl(in ranges from 0-1)
  6.  *
  7.  * Takes the hex, converts it to RGB, and sends
  8.  * it to RGBToHsl.  Returns the output.
  9.  *
  10. */
  11. function hexToHsl($hex) {
  12.     $r = "";
  13.     $g = "";
  14.     $b = "";
  15.  
  16.     $hex = str_replace('#', '', $hex);
  17.    
  18.     if (strlen($hex) == 3) {
  19.         $r = substr($hex, 0, 1);
  20.         $r = $r . $r;
  21.         $g = substr($hex, 1, 1);
  22.         $g = $g . $g;
  23.         $b = substr($hex, 2, 1);
  24.         $b = $b . $b;
  25.     } elseif (strlen($hex) == 6) {
  26.         $r = substr($hex, 0, 2);
  27.         $g = substr($hex, 2, 2);
  28.         $b = substr($hex, 4, 2);
  29.     } else {
  30.         return false;
  31.     }
  32.  
  33.     $r = hexdec($r);
  34.     $g = hexdec($g);
  35.     $b = hexdec($b);
  36.  
  37.     $hsl =  rgbToHsl($r,$g,$b);
  38.     return $hsl;
  39. }
  40.  
  41. /**
  42.  *
  43.  *Credits:
  44.  * http://stackoverflow.com/questions/4793729/rgb-to-hsl-and-back-calculation-problems
  45.  * http://www.niwa.nu/2013/05/math-behind-colorspace-conversions-rgb-hsl/
  46.  *
  47.  * Called by hexToHsl by default.
  48.  *
  49.  * Converts an RGB color value to HSL. Conversion formula
  50.  * adapted from http://www.niwa.nu/2013/05/math-behind-colorspace-conversions-rgb-hsl/.
  51.  * Assumes r, g, and b are contained in the range [0 - 255] and
  52.  * returns h, s, and l in the format Degrees, Percent, Percent.
  53.  *
  54.  * @param   Number  r       The red color value
  55.  * @param   Number  g       The green color value
  56.  * @param   Number  b       The blue color value
  57.  * @return  Array           The HSL representation
  58. */
  59. function rgbToHsl($r, $g, $b){  
  60.     //For the calculation, rgb needs to be in the range from 0 to 1. To convert, divide by 255 (ff).
  61.     $r /= 255;
  62.     $g /= 255;
  63.     $b /= 255;
  64.  
  65.     $myMax = max($r, $g, $b);
  66.     $myMin = min($r, $g, $b);
  67.  
  68.     $maxAdd = ($myMax + $myMin);
  69.     $maxSub = ($myMax - $myMin);
  70.  
  71.     //luminence is (max + min)/2
  72.     $h = 0;
  73.     $s = 0;
  74.     $l = ($maxAdd / 2.0);
  75.  
  76.     //if all the numbers are equal, there is no saturation (greyscale).
  77.     if($myMin != $myMax){
  78.         if ($l < 0.5) {
  79.             $s = ($maxSub / $maxAdd);
  80.         } else {
  81.             $s = (2.0 - $myMax - $myMin); //note order of opperations - can't use $maxSub here
  82.             $s = ($maxSub / $s);
  83.         }
  84.  
  85.         //find hue
  86.         switch($myMax){
  87.             case $r:
  88.                 $h = ($g - $b);
  89.                 $h = ($h / $maxSub);
  90.                 break;
  91.             case $g:
  92.                 $h = ($b - $r);
  93.                 $h = ($h / $maxSub);
  94.                 $h = ($h + 2.0);
  95.                 break;
  96.             case $b:
  97.                 $h = ($r - $g);
  98.                 $h = ($h / $maxSub);
  99.                 $h = ($h + 4.0);
  100.                 break;
  101.         }
  102.     }
  103.  
  104.     $hsl = hslToDegPercPerc($h, $s, $l);
  105.     return $hsl;
  106. }
  107.  
  108. /**
  109.  * Input: HSL in ranges 0-1.
  110.  * Output: HSL in format Deg, Perc, Perc.
  111.  *
  112.  * Note: rgbToHsl calls this function by default.
  113.  *
  114.  * Multiplies $h by 60, and $s and $l by 100.
  115.  */
  116. function hslToDegPercPerc($h, $s, $l) {
  117.     //convert h to degrees
  118.     $h *= 60;
  119.    
  120.     if ($h < 0) {
  121.         $h += 360;
  122.     }
  123.    
  124.     //convert s and l to percentage
  125.     $s *= 100;
  126.     $l *= 100;
  127.    
  128.     $hsl['h'] = $h;
  129.     $hsl['s'] = $s;
  130.     $hsl['l'] = $l;
  131.     return $hsl;
  132. }
  133.  
  134. /**
  135.  * Input: HSL in format Deg, Perc, Perc
  136.  * Output: An array containing HSL in ranges 0-1
  137.  *
  138.  * Divides $h by 60, and $s and $l by 100.
  139.  *
  140.  * hslToRgb calls this by default.
  141. */
  142. function degPercPercToHsl($h, $s, $l) {
  143.     //convert h, s, and l back to the 0-1 range
  144.    
  145.     //convert the hue's 360 degrees in a circle to 1
  146.     $h /= 360;
  147.    
  148.     //convert the saturation and lightness to the 0-1
  149.     //range by multiplying by 100
  150.     $s /= 100;
  151.     $l /= 100;
  152.    
  153.     $hsl['h'] =  $h;
  154.     $hsl['s'] = $s;
  155.     $hsl['l'] = $l;
  156.    
  157.     return $hsl;
  158. }
  159.  
  160. /**
  161.  * Converts an HSL color value to RGB. Conversion formula
  162.  * adapted from http://www.niwa.nu/2013/05/math-behind-colorspace-conversions-rgb-hsl/.
  163.  * Assumes h, s, and l are in the format Degrees,
  164.  * Percent, Percent, and returns r, g, and b in
  165.  * the range [0 - 255].
  166.  *
  167.  * Called by hslToHex by default.
  168.  *
  169.  * Calls:
  170.  *   degPercPercToHsl
  171.  *   hueToRgb
  172.  *
  173.  * @param   Number  h       The hue value
  174.  * @param   Number  s       The saturation level
  175.  * @param   Number  l       The luminence
  176.  * @return  Array           The RGB representation
  177.  */
  178. function hslToRgb($h, $s, $l){
  179.     $hsl = degPercPercToHsl($h, $s, $l);
  180.     $h = $hsl['h'];
  181.     $s = $hsl['s'];
  182.     $l = $hsl['l'];
  183.  
  184.     //If there's no saturation, the color is a greyscale,
  185.     //so all three RGB values can be set to the lightness.
  186.     //(Hue doesn't matter, because it's grey, not color)
  187.     if ($s == 0) {
  188.         $r = $l * 255;
  189.         $g = $l * 255;
  190.         $b = $l * 255;
  191.     }
  192.     else {
  193.         //calculate some temperary variables to make the
  194.         //calculation eaisier.
  195.         if ($l < 0.5) {
  196.             $temp2 = $l * (1 + $s);
  197.         } else {
  198.             $temp2 = ($l + $s) - ($s * $l);
  199.         }
  200.         $temp1 = 2 * $l - $temp2;
  201.        
  202.         //run the calculated vars through hueToRgb to
  203.         //calculate the RGB value.  Note that for the Red
  204.         //value, we add a third (120 degrees), to adjust
  205.         //the hue to the correct section of the circle for
  206.         //red.  Simalarly, for blue, we subtract 1/3.
  207.         $r = 255 * hueToRgb($temp1, $temp2, $h + (1 / 3));
  208.         $g = 255 * hueToRgb($temp1, $temp2, $h);
  209.         $b = 255 * hueToRgb($temp1, $temp2, $h - (1 / 3));
  210.     }
  211.        
  212.     $rgb['r'] = $r;
  213.     $rgb['g'] = $g;
  214.     $rgb['b'] = $b;
  215.  
  216.     return $rgb;
  217. }
  218.  
  219. /**
  220.  * Converts an HSL hue to it's RGB value.  
  221.  *
  222.  * Input: $temp1 and $temp2 - temperary vars based on
  223.  * whether the lumanence is less than 0.5, and
  224.  * calculated using the saturation and luminence
  225.  * values.
  226.  *  $hue - the hue (to be converted to an RGB
  227.  * value)  For red, add 1/3 to the hue, green
  228.  * leave it alone, and blue you subtract 1/3
  229.  * from the hue.
  230.  *
  231.  * Output: One RGB value.
  232.  *
  233.  * Thanks to Easy RGB for this function (Hue_2_RGB).
  234.  * http://www.easyrgb.com/index.php?X=MATH&$h=19#text19
  235.  *
  236. */
  237. function hueToRgb($temp1, $temp2, $hue) {
  238.     if ($hue < 0) {
  239.         $hue += 1;
  240.     }
  241.     if ($hue > 1) {
  242.         $hue -= 1;
  243.     }
  244.    
  245.     if ((6 * $hue) < 1 ) {
  246.         return ($temp1 + ($temp2 - $temp1) * 6 * $hue);
  247.     } elseif ((2 * $hue) < 1 ) {
  248.         return $temp2;
  249.     } elseif ((3 * $hue) < 2 ) {
  250.         return ($temp1 + ($temp2 - $temp1) * ((2 / 3) - $hue) * 6);
  251.     }
  252.     return $temp1;
  253. }
  254.  
  255. /**
  256.  * Converts HSL to Hex by converting it to
  257.  * RGB, then converting that to hex.
  258.  *
  259.  * string hslToHex($h, $s, $l[, $prependPound = true]
  260.  *
  261.  * $h is the Degrees value of the Hue
  262.  * $s is the Percentage value of the Saturation
  263.  * $l is the Percentage value of the Lightness
  264.  * $prependPound is a bool, whether you want a pound
  265.  *  sign prepended. (optional - default=true)
  266.  *
  267.  * Calls:
  268.  *   hslToRgb
  269.  *
  270.  * Output: Hex in the format: #00ff88 (with
  271.  * pound sign).  Rounded to the nearest whole
  272.  * number.
  273. */
  274. function hslToHex($h, $s, $l, $prependPound = true) {
  275.     //convert hsl to rgb
  276.     $rgb = hslToRgb($h,$s,$l);
  277.  
  278.     //convert rgb to hex
  279.     $hexR = $rgb['r'];
  280.     $hexG = $rgb['g'];
  281.     $hexB = $rgb['b'];
  282.    
  283.     //round to the nearest whole number
  284.     $hexR = round($hexR);
  285.     $hexG = round($hexG);
  286.     $hexB = round($hexB);
  287.    
  288.     //convert to hex
  289.     $hexR = dechex($hexR);
  290.     $hexG = dechex($hexG);
  291.     $hexB = dechex($hexB);
  292.    
  293.     //check for a non-two string length
  294.     //if it's 1, we can just prepend a
  295.     //0, but if it is anything else non-2,
  296.     //it must return false, as we don't
  297.     //know what format it is in.
  298.     if (strlen($hexR) != 2) {
  299.         if (strlen($hexR) == 1) {
  300.             //probably in format #0f4, etc.
  301.             $hexR = "0" . $hexR;
  302.         } else {
  303.             //unknown format
  304.             return false;
  305.         }
  306.     }
  307.     if (strlen($hexG) != 2) {
  308.         if (strlen($hexG) == 1) {
  309.             $hexG = "0" . $hexG;
  310.         } else {
  311.             return false;
  312.         }
  313.     }
  314.     if (strlen($hexB) != 2) {
  315.         if (strlen($hexB) == 1) {
  316.             $hexB = "0" . $hexB;
  317.         } else {
  318.             return false;
  319.         }
  320.     }
  321.    
  322.     //if prependPound is set, will prepend a
  323.     //# sign to the beginning of the hex code.
  324.     //(default = true)
  325.     $hex = "";
  326.     if ($prependPound) {
  327.         $hex = "#";
  328.     }
  329.    
  330.     $hex = $hex . $hexR . $hexG . $hexB;
  331.    
  332.     return $hex;
  333. }
  334.  
  335. ?>
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement