Hex2RGB (PHP)


SUBMITTED BY: scripts4you

DATE: Oct. 21, 2015, 8:50 a.m.

FORMAT: Text only

SIZE: 639 Bytes

HITS: 1729

  1. function Hex2RGB($color){
  2. $color = str_replace('#', '', $color);
  3. if (strlen($color) != 6){ return array(0,0,0); }
  4. $rgb = array();
  5. for ($x=0;$x<3;$x++){
  6. $rgb[$x] = hexdec(substr($color,(2*$x),2));
  7. }
  8. return $rgb;
  9. }
  10. // Example usage:
  11. print_r(Hex2RGB('#B3DAF5'));
  12. /*
  13. Returns an array (R,G,B):
  14. Array
  15. (
  16. [0] => 179
  17. [1] => 218
  18. [2] => 245
  19. )
  20. */
  21. // Another cool way to define RGB colors with
  22. // Hex values: (like #B3DAF5)
  23. $rgb = array(0xB3, 0xDA, 0xF5);
  24. print_r($rgb);
  25. /* output:
  26. Array
  27. (
  28. [0] => 179
  29. [1] => 218
  30. [2] => 245
  31. )
  32. */

comments powered by Disqus