Posted By

h4rrisonjames on 08/04/09


Tagged

html colour color RGB


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

vali29
umang_nine


RGB 2 HTML Colour


 / Published in: PHP
 

Converts a given RGB colour value such as that provided by the imagecolorat() function into an html compliant colour.

  1. <?php
  2. function rgb2html($r, $g=-1, $b=-1)
  3. {
  4. if (is_array($r) && sizeof($r) == 3)
  5. list($r, $g, $b) = $r;
  6.  
  7. $r = intval($r); $g = intval($g);
  8. $b = intval($b);
  9.  
  10. $r = dechex($r<0?0:($r>255?255:$r));
  11. $g = dechex($g<0?0:($g>255?255:$g));
  12. $b = dechex($b<0?0:($b>255?255:$b));
  13.  
  14. $color = (strlen($r) < 2?'0':'').$r;
  15. $color .= (strlen($g) < 2?'0':'').$g;
  16. $color .= (strlen($b) < 2?'0':'').$b;
  17. return '#'.$color;
  18. }
  19. ?>

Report this snippet  

You need to login to post a comment.