RGB 2 HTML Colour


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.