-
Notifications
You must be signed in to change notification settings - Fork 116
/
colormatch.php
129 lines (115 loc) · 3.5 KB
/
colormatch.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<html>
<head>
<title>Find the Nearest Matching Color Name</title>
<?php
function colorline($r, $g, $b, $name) {
return "<tr><td>$name <td>($r $g $b) <td>" . colorswatch($r, $g, $b) . "\n";
}
function colorswatch($r, $g, $b) {
return "<span style='background: rgb($r, $g, $b);'> </span>";
}
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[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("<tr>");
print("<td colspan=2>No exact match found for ($r $g $b)\n<td>");
print(colorswatch($r, $g, $b));
print("<tr><th colspan=3>Closest matches:");
print($matches);
}
}
}
$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 {
$r = $_GET['r'];
$g = $_GET['g'];
$b = $_GET['b'];
}
$len = strlen($hex);
?>
</head>
<body>
<h1>Find the Nearest Matching Color Name</h1>
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
<a href="http://www-swiss.ai.mit.edu/~jaffer/Color/rgb.txt">RGB.txt</a>
or in the much smaller set of
<a href="http://www.w3.org/TR/CSS21/syndata.html#color-units">CSS colors</a>.
<p>
<form method="GET" action="index.php">
Hex code (3 or 6 digits): #<input type=text name="hex" size=6>
<input type="submit" value="Match by hex code">
</form>
<p>
<form method="GET" action="index.php">
<p>
or Decimal values:
<p>
Red: <input type=text name="r" size=4>
Green: <input type=text name="g" size=4>
Blue: <input type=text name="b" size=4>
<input type="submit" value="Match by RGB values">
</form>
<?php
if ($hex != "" || ($r != "" && $g != "" && $b != "")) {
print ("<p>\n<hr>\n<p>\n<h2>Matches in rgb.txt:</h2>\n<table>\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("<tr><td colspan=3>No rgb.txt file</td></tr>");
print("</table>\n");
print("<h2>Matches in CSS color list:</h2><table>\n");
find_in_file ("csscolors.txt");
}
?>
</table>
<p>
<a href="colormatch.php.txt">The source code.</a>
<hr>
<a href="/linux/">Linux links</a> ...
<a href="/">Shallow Sky home</a> ...
<i><a href="/mailme.html">...Akkana</a></i>
</body>
</html>