Tuesday, November 2, 2010

PHP: hex color to RGB

I wrote this small script to convert a hex color to RGB value.

function hex2rgb($h){
    $h = trim($h,'#');
    if(strlen($h)==3) $h=$h[0].$h[0].$h[1].$h[1].$h[2].$h[2];
    if(strlen($h)!=6) return false;
    $d = hexdec($h);
    return array("R"=>0xFF&($d>>0x10),"G"=>0xFF&($d>>0x8),"B"=>0xFF&$d);

I know this isn't helpful to the majority of my readers, but nonetheless here it is.

No comments:

Post a Comment