Convert Hex Colour codes to their RGB values
Login or Register to Bookmark this snippet
Takes a string, examples: #ff0000, #ff0, fcc800, ffc, white, black, red and converts it to the RGB values. Returns an array.
Usage:
$arr = html2rgb('#ffc800');
$red = $arr[0];
$grn = $arr[1];
$blu = $arr[2];
<?php
function html2rgb($hex_string) {
//trim # from string
//echo dba($hex_string);
switch ($string) {
case 'WHITE':
return array(255,
255,
255);
break;
case 'BLACK':
break;
case 'RED':
break;
case 'BLUE':
break;
case 'GREEN':
break;
case 'YELLOW':
break;
default:
//check for short value
//short
//double up
$r .= $r;
$g .= $g;
$b .= $b;
} else {
//long
}
//due to some stange anomally, a value of FF returns 0 instead of 255. We shall need to fix this.
if ($r == 'FF') {
$rval = 255;
} else {
}
if ($g == 'FF') {
$gval = 255;
} else {
}
if ($b == 'FF') {
$bval = 255;
} else {
}
return array($rval,
$gval,
$bval);
break;
}
} //end function
?>
Added by JC on 13th January, 2008
There are no comments about this snippet.
You must be registered and logged in to post a comment.
Login here to post a comment