Categorized | News

Hex color array to RGB array

Posted on 02 December 2003 by Demian Turner

I’ve been doing some graphs and charts for a site I’m working on and used an array of hex color values to highlight the different bars of the graph.

Sadly, the pie-chart routines take the color values for pie-slices in RGB instead, so I knocked up this little loop to switch the colors to RGB on the fly.

// set up the colour array
$hex_colors = array('#0066B2',
 '#6CAAD9', '#B1D3EC', '#285577', 
'#4C7899','#2C83E7', '#7C94A5', 
'#B8C7D2','#D7E8F5', '#607C90');		
while (list($key,$val) = each($hex_colors)){
	$red = hexdec(substr($val, 1 , 2));
	$green = hexdec(substr($val, 3 , 2));
	$blue = hexdec(substr($val, 5 , 2));
	$rgb_colors[$key] = "$red,$green,$blue";

Of course, this could be stuck in a function and passed the array of hex_colors too.

Bookmark and Share

Leave a Reply



Demian Turner's currently-reading book recommendations, reviews, favorite quotes, book clubs, book trivia, book lists