| 1 |
"""Color Database. |
| 2 |
|
| 3 |
This file contains one class, called ColorDB, and several utility functions. |
| 4 |
The class must be instantiated by the get_colordb() function in this file, |
| 5 |
passing it a filename to read a database out of. |
| 6 |
|
| 7 |
The get_colordb() function will try to examine the file to figure out what the |
| 8 |
format of the file is. If it can't figure out the file format, or it has |
| 9 |
trouble reading the file, None is returned. You can pass get_colordb() an |
| 10 |
optional filetype argument. |
| 11 |
|
| 12 |
Supporte file types are: |
| 13 |
|
| 14 |
X_RGB_TXT -- X Consortium rgb.txt format files. Three columns of numbers |
| 15 |
from 0 .. 255 separated by whitespace. Arbitrary trailing |
| 16 |
columns used as the color name. |
| 17 |
|
| 18 |
The utility functions are useful for converting between the various expected |
| 19 |
color formats, and for calculating other color values. |
| 20 |
|
| 21 |
""" |
| 22 |
|
| 23 |
import sys |
| 24 |
import re |
| 25 |
from types import * |
| 26 |
import operator |
| 27 |
|
| 28 |
class BadColor(Exception): |
| 29 |
pass |
| 30 |
|
| 31 |
DEFAULT_DB = None |
| 32 |
SPACE = ' ' |
| 33 |
COMMASPACE = ', ' |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class ColorDB: |
| 39 |
def __init__(self, fp): |
| 40 |
lineno = 2 |
| 41 |
self.__name = fp.name |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
self.__byrgb = {} |
| 49 |
|
| 50 |
self.__byname = {} |
| 51 |
|
| 52 |
self.__allnames = None |
| 53 |
for line in fp: |
| 54 |
|
| 55 |
mo = self._re.match(line) |
| 56 |
if not mo: |
| 57 |
print >> sys.stderr, 'Error in', fp.name, ' line', lineno |
| 58 |
lineno += 1 |
| 59 |
continue |
| 60 |
|
| 61 |
red, green, blue = self._extractrgb(mo) |
| 62 |
name = self._extractname(mo) |
| 63 |
keyname = name.lower() |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
key = (red, green, blue) |
| 68 |
foundname, aliases = self.__byrgb.get(key, (name, [])) |
| 69 |
if foundname <> name and foundname not in aliases: |
| 70 |
aliases.append(name) |
| 71 |
self.__byrgb[key] = (foundname, aliases) |
| 72 |
|
| 73 |
self.__byname[keyname] = key |
| 74 |
lineno = lineno + 1 |
| 75 |
|
| 76 |
|
| 77 |
def _extractrgb(self, mo): |
| 78 |
return [int(x) for x in mo.group('red', 'green', 'blue')] |
| 79 |
|
| 80 |
def _extractname(self, mo): |
| 81 |
return mo.group('name') |
| 82 |
|
| 83 |
def filename(self): |
| 84 |
return self.__name |
| 85 |
|
| 86 |
def find_byrgb(self, rgbtuple): |
| 87 |
"""Return name for rgbtuple""" |
| 88 |
try: |
| 89 |
return self.__byrgb[rgbtuple] |
| 90 |
except KeyError: |
| 91 |
raise BadColor(rgbtuple) |
| 92 |
|
| 93 |
def find_byname(self, name): |
| 94 |
"""Return (red, green, blue) for name""" |
| 95 |
name = name.lower() |
| 96 |
try: |
| 97 |
return self.__byname[name] |
| 98 |
except KeyError: |
| 99 |
raise BadColor(name) |
| 100 |
|
| 101 |
def nearest(self, red, green, blue): |
| 102 |
"""Return the name of color nearest (red, green, blue)""" |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
nearest = -1 |
| 107 |
nearest_name = '' |
| 108 |
for name, aliases in self.__byrgb.values(): |
| 109 |
r, g, b = self.__byname[name.lower()] |
| 110 |
rdelta = red - r |
| 111 |
gdelta = green - g |
| 112 |
bdelta = blue - b |
| 113 |
distance = rdelta * rdelta + gdelta * gdelta + bdelta * bdelta |
| 114 |
if nearest == -1 or distance < nearest: |
| 115 |
nearest = distance |
| 116 |
nearest_name = name |
| 117 |
return nearest_name |
| 118 |
|
| 119 |
def unique_names(self): |
| 120 |
|
| 121 |
if not self.__allnames: |
| 122 |
self.__allnames = [] |
| 123 |
for name, aliases in self.__byrgb.values(): |
| 124 |
self.__allnames.append(name) |
| 125 |
|
| 126 |
def nocase_cmp(n1, n2): |
| 127 |
return cmp(n1.lower(), n2.lower()) |
| 128 |
self.__allnames.sort(nocase_cmp) |
| 129 |
return self.__allnames |
| 130 |
|
| 131 |
def aliases_of(self, red, green, blue): |
| 132 |
try: |
| 133 |
name, aliases = self.__byrgb[(red, green, blue)] |
| 134 |
except KeyError: |
| 135 |
raise BadColor((red, green, blue)) |
| 136 |
return [name] + aliases |
| 137 |
|
| 138 |
|
| 139 |
class RGBColorDB(ColorDB): |
| 140 |
_re = re.compile( |
| 141 |
'\s*(?P<red>\d+)\s+(?P<green>\d+)\s+(?P<blue>\d+)\s+(?P<name>.*)') |
| 142 |
|
| 143 |
|
| 144 |
class HTML40DB(ColorDB): |
| 145 |
_re = re.compile('(?P<name>\S+)\s+(?P<hexrgb>#[0-9a-fA-F]{6})') |
| 146 |
|
| 147 |
def _extractrgb(self, mo): |
| 148 |
return rrggbb_to_triplet(mo.group('hexrgb')) |
| 149 |
|
| 150 |
class LightlinkDB(HTML40DB): |
| 151 |
_re = re.compile('(?P<name>(.+))\s+(?P<hexrgb>#[0-9a-fA-F]{6})') |
| 152 |
|
| 153 |
def _extractname(self, mo): |
| 154 |
return mo.group('name').strip() |
| 155 |
|
| 156 |
class WebsafeDB(ColorDB): |
| 157 |
_re = re.compile('(?P<hexrgb>#[0-9a-fA-F]{6})') |
| 158 |
|
| 159 |
def _extractrgb(self, mo): |
| 160 |
return rrggbb_to_triplet(mo.group('hexrgb')) |
| 161 |
|
| 162 |
def _extractname(self, mo): |
| 163 |
return mo.group('hexrgb').upper() |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
FILETYPES = [ |
| 172 |
(re.compile('Xorg'), RGBColorDB), |
| 173 |
(re.compile('XConsortium'), RGBColorDB), |
| 174 |
(re.compile('HTML'), HTML40DB), |
| 175 |
(re.compile('lightlink'), LightlinkDB), |
| 176 |
(re.compile('Websafe'), WebsafeDB), |
| 177 |
] |
| 178 |
|
| 179 |
def get_colordb(file, filetype=None): |
| 180 |
colordb = None |
| 181 |
fp = open(file) |
| 182 |
try: |
| 183 |
line = fp.readline() |
| 184 |
if not line: |
| 185 |
return None |
| 186 |
|
| 187 |
if filetype is None: |
| 188 |
filetypes = FILETYPES |
| 189 |
else: |
| 190 |
filetypes = [filetype] |
| 191 |
for typere, class_ in filetypes: |
| 192 |
mo = typere.search(line) |
| 193 |
if mo: |
| 194 |
break |
| 195 |
else: |
| 196 |
|
| 197 |
return None |
| 198 |
|
| 199 |
colordb = class_(fp) |
| 200 |
finally: |
| 201 |
fp.close() |
| 202 |
|
| 203 |
global DEFAULT_DB |
| 204 |
DEFAULT_DB = colordb |
| 205 |
return colordb |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
_namedict = {} |
| 210 |
|
| 211 |
def rrggbb_to_triplet(color): |
| 212 |
"""Converts a #rrggbb color to the tuple (red, green, blue).""" |
| 213 |
rgbtuple = _namedict.get(color) |
| 214 |
if rgbtuple is None: |
| 215 |
if color[0] <> '#': |
| 216 |
raise BadColor(color) |
| 217 |
red = color[1:3] |
| 218 |
green = color[3:5] |
| 219 |
blue = color[5:7] |
| 220 |
rgbtuple = int(red, 16), int(green, 16), int(blue, 16) |
| 221 |
_namedict[color] = rgbtuple |
| 222 |
return rgbtuple |
| 223 |
|
| 224 |
|
| 225 |
_tripdict = {} |
| 226 |
def triplet_to_rrggbb(rgbtuple): |
| 227 |
"""Converts a (red, green, blue) tuple to #rrggbb.""" |
| 228 |
global _tripdict |
| 229 |
hexname = _tripdict.get(rgbtuple) |
| 230 |
if hexname is None: |
| 231 |
hexname = '#%02x%02x%02x' % rgbtuple |
| 232 |
_tripdict[rgbtuple] = hexname |
| 233 |
return hexname |
| 234 |
|
| 235 |
|
| 236 |
_maxtuple = (256.0,) * 3 |
| 237 |
def triplet_to_fractional_rgb(rgbtuple): |
| 238 |
return map(operator.__div__, rgbtuple, _maxtuple) |
| 239 |
|
| 240 |
|
| 241 |
def triplet_to_brightness(rgbtuple): |
| 242 |
|
| 243 |
|
| 244 |
r = 0.299 |
| 245 |
g = 0.587 |
| 246 |
b = 0.114 |
| 247 |
return r*rgbtuple[0] + g*rgbtuple[1] + b*rgbtuple[2] |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
if __name__ == '__main__': |
| 252 |
colordb = get_colordb('/usr/openwin/lib/rgb.txt') |
| 253 |
if not colordb: |
| 254 |
print 'No parseable color database found' |
| 255 |
sys.exit(1) |
| 256 |
|
| 257 |
target = 'navy' |
| 258 |
red, green, blue = rgbtuple = colordb.find_byname(target) |
| 259 |
print target, ':', red, green, blue, triplet_to_rrggbb(rgbtuple) |
| 260 |
name, aliases = colordb.find_byrgb(rgbtuple) |
| 261 |
print 'name:', name, 'aliases:', COMMASPACE.join(aliases) |
| 262 |
r, g, b = (1, 1, 128) |
| 263 |
r, g, b = (145, 238, 144) |
| 264 |
r, g, b = (255, 251, 250) |
| 265 |
print 'finding nearest to', target, '...' |
| 266 |
import time |
| 267 |
t0 = time.time() |
| 268 |
nearest = colordb.nearest(r, g, b) |
| 269 |
t1 = time.time() |
| 270 |
print 'found nearest color', nearest, 'in', t1-t0, 'seconds' |
| 271 |
|
| 272 |
for n in colordb.unique_names(): |
| 273 |
r, g, b = colordb.find_byname(n) |
| 274 |
aliases = colordb.aliases_of(r, g, b) |
| 275 |
print '%20s: (%3d/%3d/%3d) == %s' % (n, r, g, b, |
| 276 |
SPACE.join(aliases[1:])) |