Find the Nearest Matching Color Name
$name | ($r $g $b) (" . rgb2hex($r, $g, $b) . ")\n | "
. colorswatch($r, $g, $b) . "\n";
}
function colorswatch($r, $g, $b) {
return " ";
}
function find_in_file($colorfile) {
global $hex, $r, $g, $b;
$nmatches = 0;
$dist = 255 * sqrt(3.0); # start with longest possible distance
$fp = fopen($colorfile, 'r');
if ($fp) {
while (!feof($fp)) {
$line = fgets($fp);
if (! $line)
continue;
if ($line[0] != '!') {
list($r1, $g1, $b1, $name) = sscanf($line, "%d %d %d %s");
if ($r1 == $r && $g1 == $g && $b1 == $b) {
print(colorline($r1, $g1, $b1, $name));
++$nmatches;
$dist == 0;
}
if ($nmatches == 0) {
# if no exact match yet, see if it's closer than what
# we've seen before.
$newdist = sqrt(pow($r-$r1, 2) + pow($g-$g1, 2) + pow($b-$b1, 2));
if ($newdist == $dist) {
$matches = $matches . colorline($r1, $g1, $b1, $name);
} else if ($newdist < $dist) {
$dist = $newdist;
$matches = colorline($r1, $g1, $b1, $name);
}
}
}
}
fclose($fp);
if ($nmatches == 0) {
print(" | ");
print("No exact match found for ($r $g $b) (");
print(rgb2hex($r, $g, $b) . "\n | ");
print(colorswatch($r, $g, $b));
print(" |
Closest matches:");
print($matches);
}
}
}
if (isset($_GET['hex'])) {
$hex = $_GET['hex'];
# hex is a code like fff or 11aa77. Split it into r, g and b:
$len = strlen($hex);
if ($len == 3) {
$r = hexdec($hex[0]); $r *= 17;
$g = hexdec($hex[1]); $g *= 17;
$b = hexdec($hex[2]); $b *= 17;
}
else if ($len == 6) {
$r = hexdec(substr($hex, 0, 2));
$g = hexdec(substr($hex, 2, 2));
$b = hexdec(substr($hex, 4, 2));
}
}
else if (isset($_GET['r']) && isset($_GET['g']) && isset($_GET['b'])) {
$hex = "";
$r = $_GET['r'];
$g = $_GET['g'];
$b = $_GET['b'];
}
else {
$hex = "";
$r = 0;
$g = 0;
$b = 0;
}
?>
|