I don't suppose anyone knows of a function (PHP, preferably) that can take a hex color code and give an approximate color name for that hex value. I don't need a solution with 100s of colors. Even if it just amounted to the colors white, black, red, green blue, brown orange and yellow, I'd be pretty well in shape.
If you don't know of an existing resource, does anyone know of a good way to approach this problem?
Thanks in advance for the help.
Converting hex to RGB We need to take two hex values for one RGB value, convert those two hex values to decimal values, and then perform the same step with the other values. We will get 3 values that correspond to RGB values.
There is a program called pynche which can change RGB to colour name in English for Python. You can try to use the method ColorDB. nearest() in ColorDB.py which can do what you want.
HEX Color Values Where rr (red), gg (green) and bb (blue) are hexadecimal values between 00 and ff (same as decimal 0-255). For example, #ff0000 is displayed as red, because red is set to its highest value (ff), and the other two (green and blue) are set to 00.
You have a list of colors here in wikipedia.
A naive implementation would just calculate the distance between the color whose name you want to retrieve and every one of those and output the one that's closer.
If you see the colors as vectors in R3, you could calculate the distance as L1 norm:
function distancel1(array $color1, array $color2) {
return abs($color1[0] - $color2[0]) +
abs($color1[1] - $color2[1]) +
abs($color1[2] - $color2[2]);
}
or the L2 norm:
function distancel2(array $color1, array $color2) {
return sqrt(pow($color1[0] - $color2[0], 2) +
pow($color1[1] - $color2[1], 2) +
pow($color1[2] - $color2[2], 2));
}
For converting the hexadecimal notation, see here.
Example script:
<?php
$colors = array(
"black" => array(0, 0, 0),
"green" => array(0, 128, 0),
"silver" => array(192, 192, 192),
"lime" => array(0, 255, 0),
"gray" => array(128, 0, 128),
"olive" => array(128, 128, 0),
"white" => array(255, 255, 255),
"yellow" => array(255, 255, 0),
"maroon" => array(128, 0, 0),
"navy" => array(0, 0, 128),
"red" => array(255, 0, 0),
"blue" => array(0, 0, 255),
"purple" => array(128, 0, 128),
"teal" => array(0, 128, 128),
"fuchsia" => array(255, 0, 255),
"aqua" => array(0, 255, 255),
);
$value = "#819001";
function html2rgb($color)
{
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[0],
$color[1].$color[1], $color[2].$color[2]);
else
return false;
$r = hexdec($r); $g = hexdec($g); $b = hexdec($b);
return array($r, $g, $b);
}
function distancel2(array $color1, array $color2) {
return sqrt(pow($color1[0] - $color2[0], 2) +
pow($color1[1] - $color2[1], 2) +
pow($color1[2] - $color2[2], 2));
}
$distances = array();
$val = html2rgb($value);
foreach ($colors as $name => $c) {
$distances[$name] = distancel2($c, $val);
}
$mincolor = "";
$minval = pow(2, 30); /*big value*/
foreach ($distances as $k => $v) {
if ($v < $minval) {
$minval = $v;
$mincolor = $k;
}
}
echo "Closest color: $mincolor\n";
Using the wiki link used in Artefacto's answer I grabbed all the color names and formed an array containing 1,175 color names. Unfortunately Stackoverflow only allows 30,000 characters in answers so here's the link.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With