$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; } ?>

Find the Nearest Matching Color Name

This page can take a three or six digit hexadecimal color specifier (e.g. #fff or #a210f0), or three decimal numbers for red, green, and blue, and find the nearest named colors, either in the Unix RGB.txt or in the much smaller set of CSS colors.

Hex code (3 or 6 digits): #

or Decimal values (0-255):

Red: Green: Blue:

\n"); if (isset($hex) && $hex != "") { print("

Hex code: #" . $hex . "\n"); } print ("

Matches in rgb.txt:

\n\n"); if (file_exists("rgb.txt")) { find_in_file ("rgb.txt"); } elseif (file_exists("/etc/X11/rgb.txt")) { find_in_file ("/etc/X11/rgb.txt"); } else print(""); print("
No rgb.txt file
\n"); print("

Matches in CSS color list:

\n"); find_in_file ("csscolors.txt"); } ?>

The source code.


Linux links ... Shallow Sky home ... ...Akkana