Barry Warsaw | 2406b1d | 1998-01-31 00:29:41 +0000 | [diff] [blame] | 1 | """Color Database. |
| 2 | |
Barry Warsaw | a9053f6 | 1998-10-02 16:01:42 +0000 | [diff] [blame] | 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. |
Barry Warsaw | 2406b1d | 1998-01-31 00:29:41 +0000 | [diff] [blame] | 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. |
Barry Warsaw | a9053f6 | 1998-10-02 16:01:42 +0000 | [diff] [blame] | 17 | |
| 18 | The utility functions are useful for converting between the various expected |
| 19 | color formats, and for calculating other color values. |
| 20 | |
Barry Warsaw | 2406b1d | 1998-01-31 00:29:41 +0000 | [diff] [blame] | 21 | """ |
| 22 | |
| 23 | import sys |
| 24 | import re |
Barry Warsaw | f4562a7 | 1998-02-11 17:19:23 +0000 | [diff] [blame] | 25 | from types import * |
Barry Warsaw | 2406b1d | 1998-01-31 00:29:41 +0000 | [diff] [blame] | 26 | |
Barry Warsaw | 9f4d73a | 1998-01-31 23:38:48 +0000 | [diff] [blame] | 27 | class BadColor(Exception): |
| 28 | pass |
| 29 | |
Barry Warsaw | f4562a7 | 1998-02-11 17:19:23 +0000 | [diff] [blame] | 30 | DEFAULT_DB = None |
Barry Warsaw | 6c50ae0 | 2001-07-10 21:38:47 +0000 | [diff] [blame] | 31 | SPACE = ' ' |
| 32 | COMMASPACE = ', ' |
Barry Warsaw | f4562a7 | 1998-02-11 17:19:23 +0000 | [diff] [blame] | 33 | |
Barry Warsaw | a5a018f | 1998-09-25 22:51:36 +0000 | [diff] [blame] | 34 | |
Barry Warsaw | 2406b1d | 1998-01-31 00:29:41 +0000 | [diff] [blame] | 35 | |
| 36 | # generic class |
| 37 | class ColorDB: |
Barry Warsaw | 0604d72 | 1999-04-26 23:17:16 +0000 | [diff] [blame] | 38 | def __init__(self, fp): |
| 39 | lineno = 2 |
| 40 | self.__name = fp.name |
Barry Warsaw | 5c45805 | 2002-10-21 14:25:24 +0000 | [diff] [blame] | 41 | # Maintain several dictionaries for indexing into the color database. |
| 42 | # Note that while Tk supports RGB intensities of 4, 8, 12, or 16 bits, |
| 43 | # for now we only support 8 bit intensities. At least on OpenWindows, |
| 44 | # all intensities in the /usr/openwin/lib/rgb.txt file are 8-bit |
| 45 | # |
| 46 | # key is (red, green, blue) tuple, value is (name, [aliases]) |
| 47 | self.__byrgb = {} |
| 48 | # key is name, value is (red, green, blue) |
| 49 | self.__byname = {} |
Barry Warsaw | 8be2594 | 1998-10-02 14:43:30 +0000 | [diff] [blame] | 50 | # all unique names (non-aliases). built-on demand |
| 51 | self.__allnames = None |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 52 | for line in fp: |
Barry Warsaw | 5c45805 | 2002-10-21 14:25:24 +0000 | [diff] [blame] | 53 | # get this compiled regular expression from derived class |
| 54 | mo = self._re.match(line) |
| 55 | if not mo: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 56 | print('Error in', fp.name, ' line', lineno, file=sys.stderr) |
Barry Warsaw | 5c45805 | 2002-10-21 14:25:24 +0000 | [diff] [blame] | 57 | lineno += 1 |
| 58 | continue |
| 59 | # extract the red, green, blue, and name |
Barry Warsaw | 0604d72 | 1999-04-26 23:17:16 +0000 | [diff] [blame] | 60 | red, green, blue = self._extractrgb(mo) |
| 61 | name = self._extractname(mo) |
Barry Warsaw | 5c45805 | 2002-10-21 14:25:24 +0000 | [diff] [blame] | 62 | keyname = name.lower() |
| 63 | # BAW: for now the `name' is just the first named color with the |
| 64 | # rgb values we find. Later, we might want to make the two word |
| 65 | # version the `name', or the CapitalizedVersion, etc. |
| 66 | key = (red, green, blue) |
| 67 | foundname, aliases = self.__byrgb.get(key, (name, [])) |
Georg Brandl | bf82e37 | 2008-05-16 17:02:34 +0000 | [diff] [blame] | 68 | if foundname != name and foundname not in aliases: |
Barry Warsaw | 5c45805 | 2002-10-21 14:25:24 +0000 | [diff] [blame] | 69 | aliases.append(name) |
| 70 | self.__byrgb[key] = (foundname, aliases) |
| 71 | # add to byname lookup |
| 72 | self.__byname[keyname] = key |
| 73 | lineno = lineno + 1 |
Barry Warsaw | 2406b1d | 1998-01-31 00:29:41 +0000 | [diff] [blame] | 74 | |
Barry Warsaw | 0604d72 | 1999-04-26 23:17:16 +0000 | [diff] [blame] | 75 | # override in derived classes |
| 76 | def _extractrgb(self, mo): |
Barry Warsaw | 6c50ae0 | 2001-07-10 21:38:47 +0000 | [diff] [blame] | 77 | return [int(x) for x in mo.group('red', 'green', 'blue')] |
Barry Warsaw | 0604d72 | 1999-04-26 23:17:16 +0000 | [diff] [blame] | 78 | |
| 79 | def _extractname(self, mo): |
| 80 | return mo.group('name') |
| 81 | |
| 82 | def filename(self): |
| 83 | return self.__name |
| 84 | |
Barry Warsaw | eb9b8af | 1998-02-11 18:55:37 +0000 | [diff] [blame] | 85 | def find_byrgb(self, rgbtuple): |
Barry Warsaw | 0604d72 | 1999-04-26 23:17:16 +0000 | [diff] [blame] | 86 | """Return name for rgbtuple""" |
Barry Warsaw | 5c45805 | 2002-10-21 14:25:24 +0000 | [diff] [blame] | 87 | try: |
| 88 | return self.__byrgb[rgbtuple] |
| 89 | except KeyError: |
| 90 | raise BadColor(rgbtuple) |
Barry Warsaw | 2406b1d | 1998-01-31 00:29:41 +0000 | [diff] [blame] | 91 | |
| 92 | def find_byname(self, name): |
Barry Warsaw | 0604d72 | 1999-04-26 23:17:16 +0000 | [diff] [blame] | 93 | """Return (red, green, blue) for name""" |
Barry Warsaw | 5c45805 | 2002-10-21 14:25:24 +0000 | [diff] [blame] | 94 | name = name.lower() |
| 95 | try: |
| 96 | return self.__byname[name] |
| 97 | except KeyError: |
| 98 | raise BadColor(name) |
Barry Warsaw | 2406b1d | 1998-01-31 00:29:41 +0000 | [diff] [blame] | 99 | |
Barry Warsaw | 7a13418 | 1998-09-29 20:03:15 +0000 | [diff] [blame] | 100 | def nearest(self, red, green, blue): |
Barry Warsaw | 0604d72 | 1999-04-26 23:17:16 +0000 | [diff] [blame] | 101 | """Return the name of color nearest (red, green, blue)""" |
Barry Warsaw | 5c45805 | 2002-10-21 14:25:24 +0000 | [diff] [blame] | 102 | # BAW: should we use Voronoi diagrams, Delaunay triangulation, or |
| 103 | # octree for speeding up the locating of nearest point? Exhaustive |
| 104 | # search is inefficient, but seems fast enough. |
| 105 | nearest = -1 |
| 106 | nearest_name = '' |
| 107 | for name, aliases in self.__byrgb.values(): |
| 108 | r, g, b = self.__byname[name.lower()] |
| 109 | rdelta = red - r |
| 110 | gdelta = green - g |
| 111 | bdelta = blue - b |
| 112 | distance = rdelta * rdelta + gdelta * gdelta + bdelta * bdelta |
| 113 | if nearest == -1 or distance < nearest: |
| 114 | nearest = distance |
| 115 | nearest_name = name |
| 116 | return nearest_name |
Barry Warsaw | 8be2594 | 1998-10-02 14:43:30 +0000 | [diff] [blame] | 117 | |
Barry Warsaw | 9f3ea21 | 1998-10-02 15:59:20 +0000 | [diff] [blame] | 118 | def unique_names(self): |
Barry Warsaw | 8be2594 | 1998-10-02 14:43:30 +0000 | [diff] [blame] | 119 | # sorted |
| 120 | if not self.__allnames: |
| 121 | self.__allnames = [] |
| 122 | for name, aliases in self.__byrgb.values(): |
| 123 | self.__allnames.append(name) |
Georg Brandl | bf82e37 | 2008-05-16 17:02:34 +0000 | [diff] [blame] | 124 | self.__allnames.sort(key=str.lower) |
Barry Warsaw | 8be2594 | 1998-10-02 14:43:30 +0000 | [diff] [blame] | 125 | return self.__allnames |
Barry Warsaw | 9f3ea21 | 1998-10-02 15:59:20 +0000 | [diff] [blame] | 126 | |
| 127 | def aliases_of(self, red, green, blue): |
| 128 | try: |
| 129 | name, aliases = self.__byrgb[(red, green, blue)] |
| 130 | except KeyError: |
| 131 | raise BadColor((red, green, blue)) |
| 132 | return [name] + aliases |
Barry Warsaw | 5c45805 | 2002-10-21 14:25:24 +0000 | [diff] [blame] | 133 | |
Barry Warsaw | 2406b1d | 1998-01-31 00:29:41 +0000 | [diff] [blame] | 134 | |
| 135 | class RGBColorDB(ColorDB): |
| 136 | _re = re.compile( |
R David Murray | 44b548d | 2016-09-08 13:59:53 -0400 | [diff] [blame] | 137 | r'\s*(?P<red>\d+)\s+(?P<green>\d+)\s+(?P<blue>\d+)\s+(?P<name>.*)') |
Barry Warsaw | 0604d72 | 1999-04-26 23:17:16 +0000 | [diff] [blame] | 138 | |
| 139 | |
| 140 | class HTML40DB(ColorDB): |
R David Murray | 44b548d | 2016-09-08 13:59:53 -0400 | [diff] [blame] | 141 | _re = re.compile(r'(?P<name>\S+)\s+(?P<hexrgb>#[0-9a-fA-F]{6})') |
Barry Warsaw | 0604d72 | 1999-04-26 23:17:16 +0000 | [diff] [blame] | 142 | |
| 143 | def _extractrgb(self, mo): |
| 144 | return rrggbb_to_triplet(mo.group('hexrgb')) |
| 145 | |
| 146 | class LightlinkDB(HTML40DB): |
R David Murray | 44b548d | 2016-09-08 13:59:53 -0400 | [diff] [blame] | 147 | _re = re.compile(r'(?P<name>(.+))\s+(?P<hexrgb>#[0-9a-fA-F]{6})') |
Barry Warsaw | 0604d72 | 1999-04-26 23:17:16 +0000 | [diff] [blame] | 148 | |
| 149 | def _extractname(self, mo): |
Barry Warsaw | 6c50ae0 | 2001-07-10 21:38:47 +0000 | [diff] [blame] | 150 | return mo.group('name').strip() |
Barry Warsaw | 0604d72 | 1999-04-26 23:17:16 +0000 | [diff] [blame] | 151 | |
| 152 | class WebsafeDB(ColorDB): |
| 153 | _re = re.compile('(?P<hexrgb>#[0-9a-fA-F]{6})') |
| 154 | |
| 155 | def _extractrgb(self, mo): |
| 156 | return rrggbb_to_triplet(mo.group('hexrgb')) |
| 157 | |
| 158 | def _extractname(self, mo): |
Barry Warsaw | 6c50ae0 | 2001-07-10 21:38:47 +0000 | [diff] [blame] | 159 | return mo.group('hexrgb').upper() |
Barry Warsaw | 2406b1d | 1998-01-31 00:29:41 +0000 | [diff] [blame] | 160 | |
| 161 | |
| 162 | |
| 163 | # format is a tuple (RE, SCANLINES, CLASS) where RE is a compiled regular |
| 164 | # expression, SCANLINES is the number of header lines to scan, and CLASS is |
| 165 | # the class to instantiate if a match is found |
| 166 | |
Barry Warsaw | 0604d72 | 1999-04-26 23:17:16 +0000 | [diff] [blame] | 167 | FILETYPES = [ |
Barry Warsaw | 5c45805 | 2002-10-21 14:25:24 +0000 | [diff] [blame] | 168 | (re.compile('Xorg'), RGBColorDB), |
Barry Warsaw | 0604d72 | 1999-04-26 23:17:16 +0000 | [diff] [blame] | 169 | (re.compile('XConsortium'), RGBColorDB), |
| 170 | (re.compile('HTML'), HTML40DB), |
| 171 | (re.compile('lightlink'), LightlinkDB), |
| 172 | (re.compile('Websafe'), WebsafeDB), |
| 173 | ] |
Barry Warsaw | 2406b1d | 1998-01-31 00:29:41 +0000 | [diff] [blame] | 174 | |
Barry Warsaw | 0604d72 | 1999-04-26 23:17:16 +0000 | [diff] [blame] | 175 | def get_colordb(file, filetype=None): |
Barry Warsaw | 2406b1d | 1998-01-31 00:29:41 +0000 | [diff] [blame] | 176 | colordb = None |
Barry Warsaw | 0604d72 | 1999-04-26 23:17:16 +0000 | [diff] [blame] | 177 | fp = open(file) |
Barry Warsaw | 2406b1d | 1998-01-31 00:29:41 +0000 | [diff] [blame] | 178 | try: |
Barry Warsaw | 0604d72 | 1999-04-26 23:17:16 +0000 | [diff] [blame] | 179 | line = fp.readline() |
| 180 | if not line: |
| 181 | return None |
| 182 | # try to determine the type of RGB file it is |
| 183 | if filetype is None: |
| 184 | filetypes = FILETYPES |
| 185 | else: |
| 186 | filetypes = [filetype] |
| 187 | for typere, class_ in filetypes: |
| 188 | mo = typere.search(line) |
| 189 | if mo: |
| 190 | break |
| 191 | else: |
| 192 | # no matching type |
| 193 | return None |
| 194 | # we know the type and the class to grok the type, so suck it in |
| 195 | colordb = class_(fp) |
Barry Warsaw | 2406b1d | 1998-01-31 00:29:41 +0000 | [diff] [blame] | 196 | finally: |
Barry Warsaw | 0604d72 | 1999-04-26 23:17:16 +0000 | [diff] [blame] | 197 | fp.close() |
Barry Warsaw | f4562a7 | 1998-02-11 17:19:23 +0000 | [diff] [blame] | 198 | # save a global copy |
| 199 | global DEFAULT_DB |
| 200 | DEFAULT_DB = colordb |
Barry Warsaw | 2406b1d | 1998-01-31 00:29:41 +0000 | [diff] [blame] | 201 | return colordb |
| 202 | |
Barry Warsaw | 9f4d73a | 1998-01-31 23:38:48 +0000 | [diff] [blame] | 203 | |
| 204 | |
Barry Warsaw | 8d3e5ee | 1998-02-18 00:02:26 +0000 | [diff] [blame] | 205 | _namedict = {} |
Barry Warsaw | 6c50ae0 | 2001-07-10 21:38:47 +0000 | [diff] [blame] | 206 | |
| 207 | def rrggbb_to_triplet(color): |
Barry Warsaw | 9f4d73a | 1998-01-31 23:38:48 +0000 | [diff] [blame] | 208 | """Converts a #rrggbb color to the tuple (red, green, blue).""" |
Barry Warsaw | 8d3e5ee | 1998-02-18 00:02:26 +0000 | [diff] [blame] | 209 | rgbtuple = _namedict.get(color) |
| 210 | if rgbtuple is None: |
Georg Brandl | bf82e37 | 2008-05-16 17:02:34 +0000 | [diff] [blame] | 211 | if color[0] != '#': |
Barry Warsaw | b7b1cf0 | 1998-10-06 18:10:59 +0000 | [diff] [blame] | 212 | raise BadColor(color) |
Barry Warsaw | 5c45805 | 2002-10-21 14:25:24 +0000 | [diff] [blame] | 213 | red = color[1:3] |
| 214 | green = color[3:5] |
| 215 | blue = color[5:7] |
Barry Warsaw | 6c50ae0 | 2001-07-10 21:38:47 +0000 | [diff] [blame] | 216 | rgbtuple = int(red, 16), int(green, 16), int(blue, 16) |
Barry Warsaw | 5c45805 | 2002-10-21 14:25:24 +0000 | [diff] [blame] | 217 | _namedict[color] = rgbtuple |
Barry Warsaw | 8d3e5ee | 1998-02-18 00:02:26 +0000 | [diff] [blame] | 218 | return rgbtuple |
Barry Warsaw | 9f4d73a | 1998-01-31 23:38:48 +0000 | [diff] [blame] | 219 | |
Barry Warsaw | f4562a7 | 1998-02-11 17:19:23 +0000 | [diff] [blame] | 220 | |
Barry Warsaw | 8d3e5ee | 1998-02-18 00:02:26 +0000 | [diff] [blame] | 221 | _tripdict = {} |
Barry Warsaw | f4562a7 | 1998-02-11 17:19:23 +0000 | [diff] [blame] | 222 | def triplet_to_rrggbb(rgbtuple): |
| 223 | """Converts a (red, green, blue) tuple to #rrggbb.""" |
Barry Warsaw | 0604d72 | 1999-04-26 23:17:16 +0000 | [diff] [blame] | 224 | global _tripdict |
Barry Warsaw | 8d3e5ee | 1998-02-18 00:02:26 +0000 | [diff] [blame] | 225 | hexname = _tripdict.get(rgbtuple) |
| 226 | if hexname is None: |
Barry Warsaw | 5c45805 | 2002-10-21 14:25:24 +0000 | [diff] [blame] | 227 | hexname = '#%02x%02x%02x' % rgbtuple |
| 228 | _tripdict[rgbtuple] = hexname |
Barry Warsaw | 8d3e5ee | 1998-02-18 00:02:26 +0000 | [diff] [blame] | 229 | return hexname |
Barry Warsaw | 9f4d73a | 1998-01-31 23:38:48 +0000 | [diff] [blame] | 230 | |
| 231 | |
Barry Warsaw | a5a018f | 1998-09-25 22:51:36 +0000 | [diff] [blame] | 232 | def triplet_to_fractional_rgb(rgbtuple): |
Serhiy Storchaka | a60c2fe | 2015-03-12 21:56:08 +0200 | [diff] [blame] | 233 | return [x / 256 for x in rgbtuple] |
Barry Warsaw | 2662e15 | 1998-02-13 21:27:56 +0000 | [diff] [blame] | 234 | |
| 235 | |
Barry Warsaw | 0e3e699 | 1998-09-28 23:39:18 +0000 | [diff] [blame] | 236 | def triplet_to_brightness(rgbtuple): |
| 237 | # return the brightness (grey level) along the scale 0.0==black to |
| 238 | # 1.0==white |
| 239 | r = 0.299 |
| 240 | g = 0.587 |
| 241 | b = 0.114 |
| 242 | return r*rgbtuple[0] + g*rgbtuple[1] + b*rgbtuple[2] |
| 243 | |
| 244 | |
Barry Warsaw | 2406b1d | 1998-01-31 00:29:41 +0000 | [diff] [blame] | 245 | |
| 246 | if __name__ == '__main__': |
Barry Warsaw | 2406b1d | 1998-01-31 00:29:41 +0000 | [diff] [blame] | 247 | colordb = get_colordb('/usr/openwin/lib/rgb.txt') |
| 248 | if not colordb: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 249 | print('No parseable color database found') |
Barry Warsaw | 5c45805 | 2002-10-21 14:25:24 +0000 | [diff] [blame] | 250 | sys.exit(1) |
Barry Warsaw | 2406b1d | 1998-01-31 00:29:41 +0000 | [diff] [blame] | 251 | # on my system, this color matches exactly |
| 252 | target = 'navy' |
Barry Warsaw | a5a018f | 1998-09-25 22:51:36 +0000 | [diff] [blame] | 253 | red, green, blue = rgbtuple = colordb.find_byname(target) |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 254 | print(target, ':', red, green, blue, triplet_to_rrggbb(rgbtuple)) |
Barry Warsaw | a5a018f | 1998-09-25 22:51:36 +0000 | [diff] [blame] | 255 | name, aliases = colordb.find_byrgb(rgbtuple) |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 256 | print('name:', name, 'aliases:', COMMASPACE.join(aliases)) |
Barry Warsaw | 5c45805 | 2002-10-21 14:25:24 +0000 | [diff] [blame] | 257 | r, g, b = (1, 1, 128) # nearest to navy |
| 258 | r, g, b = (145, 238, 144) # nearest to lightgreen |
| 259 | r, g, b = (255, 251, 250) # snow |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 260 | print('finding nearest to', target, '...') |
Barry Warsaw | 2406b1d | 1998-01-31 00:29:41 +0000 | [diff] [blame] | 261 | import time |
| 262 | t0 = time.time() |
Barry Warsaw | 840a84d | 1998-10-15 02:18:08 +0000 | [diff] [blame] | 263 | nearest = colordb.nearest(r, g, b) |
Barry Warsaw | 2406b1d | 1998-01-31 00:29:41 +0000 | [diff] [blame] | 264 | t1 = time.time() |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 265 | print('found nearest color', nearest, 'in', t1-t0, 'seconds') |
Barry Warsaw | 840a84d | 1998-10-15 02:18:08 +0000 | [diff] [blame] | 266 | # dump the database |
| 267 | for n in colordb.unique_names(): |
| 268 | r, g, b = colordb.find_byname(n) |
| 269 | aliases = colordb.aliases_of(r, g, b) |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 270 | print('%20s: (%3d/%3d/%3d) == %s' % (n, r, g, b, |
| 271 | SPACE.join(aliases[1:]))) |