PDA

View Full Version : HEX 2 Colo-u-r

mr_ego
10-27-2003, 08:23 AM
If I have a hexidecimal color:

EG:
#FFFFFF
OR:
#ACED00

How do i get this to represent this in alphabetical characters

EG:
White
OR:
Light Green

Is there a simple mathemtical way to calculate the HEX and turn it into a string?

-Bash

mordred
10-27-2003, 10:20 PM
What you have is a string containing a color value in hexadecimal notation. The pair of hex digits stands for (in order) red, green and blue. The range of color value is from 0 (none) to 255 (full). You can transform the string value into hex numbers by using this function out of PEAR::Image_Color:

function _splitColor ( \$color )
{
\$color = str_replace('#', '', \$color);
\$c[] = hexdec( substr( \$color, 0, 2 ) );
\$c[] = hexdec( substr( \$color, 2, 2 ) );
\$c[] = hexdec( substr( \$color, 4, 2 ) );
return \$c;
}

The whole package can be found and browsed at http://cvs.php.net/co.php/pear/Image_Color/Color.php?r=1.11 In this package there's also an array of color names and their corresponding hex values, although the keys are the color names. You'd need to sort this array first by a custom function, and then you could loop through it with a foreach loop and get the closest match for the tested color. Then the key of this match is the color name.