Posted By

Sverri on 08/25/10


Tagged

hex RGB


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

seloh77


hex2rgb


 / Published in: PHP
 

  1. if ( ! function_exists("hex2rgb"))
  2. {
  3. function hex2rgb($h, $a=false)
  4. {
  5. if ($h[0] == '#') $h = substr($h,1);
  6.  
  7. if (strlen($h) == 3)
  8. {
  9. $h = str_split($h,1);
  10.  
  11. foreach ($h as &$c) $c .= $c;
  12. }
  13. elseif (strlen($h) == 6)
  14. {
  15. $h = str_split($h,2);
  16. }
  17. else return null;
  18.  
  19. foreach ($h as &$t)
  20. {
  21. $t = base_convert($t,16,10);
  22. }
  23. return $a ? array_combine(array('red','green','blue'),$h)
  24.  
  25. : implode(',',$h);
  26. }
  27. }

Report this snippet  

You need to login to post a comment.