johnmahugu

php - hex to rgb

Jun 14th, 2015
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.62 KB | None | 0 0
  1.  
  2.  
  3. function Hex2RGB($color){
  4.     $color = str_replace('#', '', $color);
  5.     if (strlen($color) != 6){ return array(0,0,0); }
  6.     $rgb = array();
  7.     for ($x=0;$x<3;$x++){
  8.         $rgb[$x] = hexdec(substr($color,(2*$x),2));
  9.     }
  10.     return $rgb;
  11. }
  12.  
  13.  
  14. // Example usage:
  15. print_r(Hex2RGB('#B3DAF5'));
  16.  
  17. /*
  18. Returns an array (R,G,B):
  19.  
  20. Array
  21. (
  22.     [0] => 179
  23.     [1] => 218
  24.     [2] => 245
  25. )
  26. */
  27.  
  28. // Another cool way to define RGB colors with
  29. // Hex values: (like #B3DAF5)
  30.  
  31. $rgb = array(0xB3, 0xDA, 0xF5);
  32.  
  33. print_r($rgb);
  34.  
  35. /* output:
  36.  
  37. Array
  38. (
  39.     [0] => 179
  40.     [1] => 218
  41.     [2] => 245
  42. )
  43.  
  44. */
Advertisement
Add Comment
Please, Sign In to add comment