PHP Convert RGB from/to HTML hex color
# character in the beginning can be omitted. Function returns array of three integers in range (0..255) or false when it fails to recognize color format.
{ if ($color[0] == '#') $color = substr($color, 1); if (strlen($color) == 6) list($r, $g, $b) = array($color[0].$color[1], $color[2].$color[3], $color[4].$color[5]); elseif (strlen($color) == 3) list($r, $g, $b) = array($color[0], $color[1], $color[2]); else return false; $r = hexdec($r); $g = hexdec($g); $b = hexdec($b); return array($r, $g, $b); } ?>
Second function, rgb2html converts its arguments (r, g, b) to hexadecimal html-color string #RRGGBB Arguments are converted to integers and trimmed to 0..255 range. It is possible to call it with array argument rgb2html($array_of_three_ints) or specifying each component value separetly rgb2html($r, $g, $b).
