annotate Tools/pynche/ColorDB.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents a6b190b0cf06
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7180
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1 """Color Database.
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
2
9225
ce8f3cef6aed Updated docstring.
Barry Warsaw <barry@python.org>
parents: 9224
diff changeset
3 This file contains one class, called ColorDB, and several utility functions.
ce8f3cef6aed Updated docstring.
Barry Warsaw <barry@python.org>
parents: 9224
diff changeset
4 The class must be instantiated by the get_colordb() function in this file,
ce8f3cef6aed Updated docstring.
Barry Warsaw <barry@python.org>
parents: 9224
diff changeset
5 passing it a filename to read a database out of.
ce8f3cef6aed Updated docstring.
Barry Warsaw <barry@python.org>
parents: 9224
diff changeset
6
ce8f3cef6aed Updated docstring.
Barry Warsaw <barry@python.org>
parents: 9224
diff changeset
7 The get_colordb() function will try to examine the file to figure out what the
ce8f3cef6aed Updated docstring.
Barry Warsaw <barry@python.org>
parents: 9224
diff changeset
8 format of the file is. If it can't figure out the file format, or it has
ce8f3cef6aed Updated docstring.
Barry Warsaw <barry@python.org>
parents: 9224
diff changeset
9 trouble reading the file, None is returned. You can pass get_colordb() an
ce8f3cef6aed Updated docstring.
Barry Warsaw <barry@python.org>
parents: 9224
diff changeset
10 optional filetype argument.
7180
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
11
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
12 Supporte file types are:
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
13
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
14 X_RGB_TXT -- X Consortium rgb.txt format files. Three columns of numbers
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
15 from 0 .. 255 separated by whitespace. Arbitrary trailing
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
16 columns used as the color name.
9225
ce8f3cef6aed Updated docstring.
Barry Warsaw <barry@python.org>
parents: 9224
diff changeset
17
ce8f3cef6aed Updated docstring.
Barry Warsaw <barry@python.org>
parents: 9224
diff changeset
18 The utility functions are useful for converting between the various expected
ce8f3cef6aed Updated docstring.
Barry Warsaw <barry@python.org>
parents: 9224
diff changeset
19 color formats, and for calculating other color values.
ce8f3cef6aed Updated docstring.
Barry Warsaw <barry@python.org>
parents: 9224
diff changeset
20
7180
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
21 """
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
22
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
23 import sys
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
24 import re
7236
fd7a5993d06d Simplification of conversion routines
Barry Warsaw <barry@python.org>
parents: 7182
diff changeset
25 from types import *
7346
5a9256b8c338 Cache conversions from triplets to rrggbb's and vice versa. Wasteful
Barry Warsaw <barry@python.org>
parents: 7293
diff changeset
26 import operator
7180
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
27
7182
59362ea7e0f8 BadColor: new exception class
Barry Warsaw <barry@python.org>
parents: 7181
diff changeset
28 class BadColor(Exception):
59362ea7e0f8 BadColor: new exception class
Barry Warsaw <barry@python.org>
parents: 7181
diff changeset
29 pass
59362ea7e0f8 BadColor: new exception class
Barry Warsaw <barry@python.org>
parents: 7181
diff changeset
30
7236
fd7a5993d06d Simplification of conversion routines
Barry Warsaw <barry@python.org>
parents: 7182
diff changeset
31 DEFAULT_DB = None
18456
70af4a173a95 De-string-module-ification and other Python 2.x improvements.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
32 SPACE = ' '
70af4a173a95 De-string-module-ification and other Python 2.x improvements.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
33 COMMASPACE = ', '
7236
fd7a5993d06d Simplification of conversion routines
Barry Warsaw <barry@python.org>
parents: 7182
diff changeset
34
9146
c4602236409e Fixed unit test
Barry Warsaw <barry@python.org>
parents: 7371
diff changeset
35
7180
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
36
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
37 # generic class
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
38 class ColorDB:
10744
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
39 def __init__(self, fp):
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
40 lineno = 2
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
41 self.__name = fp.name
25877
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
42 # Maintain several dictionaries for indexing into the color database.
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
43 # Note that while Tk supports RGB intensities of 4, 8, 12, or 16 bits,
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
44 # for now we only support 8 bit intensities. At least on OpenWindows,
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
45 # all intensities in the /usr/openwin/lib/rgb.txt file are 8-bit
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
46 #
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
47 # key is (red, green, blue) tuple, value is (name, [aliases])
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
48 self.__byrgb = {}
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
49 # key is name, value is (red, green, blue)
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
50 self.__byname = {}
9221
3c9fec57b858 all_names(): Return just the unique names of rgb colors
Barry Warsaw <barry@python.org>
parents: 9173
diff changeset
51 # all unique names (non-aliases). built-on demand
3c9fec57b858 all_names(): Return just the unique names of rgb colors
Barry Warsaw <barry@python.org>
parents: 9173
diff changeset
52 self.__allnames = None
44847
a6b190b0cf06 #1726198: replace while 1: fp.readline() with file iteration.
Georg Brandl <georg@python.org>
parents: 25877
diff changeset
53 for line in fp:
25877
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
54 # get this compiled regular expression from derived class
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
55 mo = self._re.match(line)
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
56 if not mo:
18456
70af4a173a95 De-string-module-ification and other Python 2.x improvements.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
57 print >> sys.stderr, 'Error in', fp.name, ' line', lineno
25877
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
58 lineno += 1
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
59 continue
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
60 # extract the red, green, blue, and name
10744
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
61 red, green, blue = self._extractrgb(mo)
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
62 name = self._extractname(mo)
25877
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
63 keyname = name.lower()
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
64 # BAW: for now the `name' is just the first named color with the
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
65 # rgb values we find. Later, we might want to make the two word
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
66 # version the `name', or the CapitalizedVersion, etc.
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
67 key = (red, green, blue)
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
68 foundname, aliases = self.__byrgb.get(key, (name, []))
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
69 if foundname <> name and foundname not in aliases:
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
70 aliases.append(name)
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
71 self.__byrgb[key] = (foundname, aliases)
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
72 # add to byname lookup
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
73 self.__byname[keyname] = key
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
74 lineno = lineno + 1
7180
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
75
10744
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
76 # override in derived classes
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
77 def _extractrgb(self, mo):
18456
70af4a173a95 De-string-module-ification and other Python 2.x improvements.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
78 return [int(x) for x in mo.group('red', 'green', 'blue')]
10744
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
79
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
80 def _extractname(self, mo):
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
81 return mo.group('name')
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
82
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
83 def filename(self):
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
84 return self.__name
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
85
7239
306f737bab57 __init__(): simplify, use tuple as key to __byrgb, lowercase __byname key.
Barry Warsaw <barry@python.org>
parents: 7236
diff changeset
86 def find_byrgb(self, rgbtuple):
10744
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
87 """Return name for rgbtuple"""
25877
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
88 try:
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
89 return self.__byrgb[rgbtuple]
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
90 except KeyError:
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
91 raise BadColor(rgbtuple)
7180
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
92
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
93 def find_byname(self, name):
10744
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
94 """Return (red, green, blue) for name"""
25877
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
95 name = name.lower()
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
96 try:
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
97 return self.__byname[name]
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
98 except KeyError:
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
99 raise BadColor(name)
7180
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
100
9173
32b19491e70b Changed interface for nearest() method to take individual red, green,
Barry Warsaw <barry@python.org>
parents: 9165
diff changeset
101 def nearest(self, red, green, blue):
10744
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
102 """Return the name of color nearest (red, green, blue)"""
25877
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
103 # BAW: should we use Voronoi diagrams, Delaunay triangulation, or
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
104 # octree for speeding up the locating of nearest point? Exhaustive
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
105 # search is inefficient, but seems fast enough.
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
106 nearest = -1
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
107 nearest_name = ''
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
108 for name, aliases in self.__byrgb.values():
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
109 r, g, b = self.__byname[name.lower()]
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
110 rdelta = red - r
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
111 gdelta = green - g
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
112 bdelta = blue - b
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
113 distance = rdelta * rdelta + gdelta * gdelta + bdelta * bdelta
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
114 if nearest == -1 or distance < nearest:
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
115 nearest = distance
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
116 nearest_name = name
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
117 return nearest_name
9221
3c9fec57b858 all_names(): Return just the unique names of rgb colors
Barry Warsaw <barry@python.org>
parents: 9173
diff changeset
118
9224
73df24f6bccf all_names(): => unique_names()
Barry Warsaw <barry@python.org>
parents: 9221
diff changeset
119 def unique_names(self):
9221
3c9fec57b858 all_names(): Return just the unique names of rgb colors
Barry Warsaw <barry@python.org>
parents: 9173
diff changeset
120 # sorted
3c9fec57b858 all_names(): Return just the unique names of rgb colors
Barry Warsaw <barry@python.org>
parents: 9173
diff changeset
121 if not self.__allnames:
3c9fec57b858 all_names(): Return just the unique names of rgb colors
Barry Warsaw <barry@python.org>
parents: 9173
diff changeset
122 self.__allnames = []
3c9fec57b858 all_names(): Return just the unique names of rgb colors
Barry Warsaw <barry@python.org>
parents: 9173
diff changeset
123 for name, aliases in self.__byrgb.values():
3c9fec57b858 all_names(): Return just the unique names of rgb colors
Barry Warsaw <barry@python.org>
parents: 9173
diff changeset
124 self.__allnames.append(name)
9224
73df24f6bccf all_names(): => unique_names()
Barry Warsaw <barry@python.org>
parents: 9221
diff changeset
125 # sort irregardless of case
73df24f6bccf all_names(): => unique_names()
Barry Warsaw <barry@python.org>
parents: 9221
diff changeset
126 def nocase_cmp(n1, n2):
18456
70af4a173a95 De-string-module-ification and other Python 2.x improvements.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
127 return cmp(n1.lower(), n2.lower())
9224
73df24f6bccf all_names(): => unique_names()
Barry Warsaw <barry@python.org>
parents: 9221
diff changeset
128 self.__allnames.sort(nocase_cmp)
9221
3c9fec57b858 all_names(): Return just the unique names of rgb colors
Barry Warsaw <barry@python.org>
parents: 9173
diff changeset
129 return self.__allnames
9224
73df24f6bccf all_names(): => unique_names()
Barry Warsaw <barry@python.org>
parents: 9221
diff changeset
130
73df24f6bccf all_names(): => unique_names()
Barry Warsaw <barry@python.org>
parents: 9221
diff changeset
131 def aliases_of(self, red, green, blue):
73df24f6bccf all_names(): => unique_names()
Barry Warsaw <barry@python.org>
parents: 9221
diff changeset
132 try:
73df24f6bccf all_names(): => unique_names()
Barry Warsaw <barry@python.org>
parents: 9221
diff changeset
133 name, aliases = self.__byrgb[(red, green, blue)]
73df24f6bccf all_names(): => unique_names()
Barry Warsaw <barry@python.org>
parents: 9221
diff changeset
134 except KeyError:
73df24f6bccf all_names(): => unique_names()
Barry Warsaw <barry@python.org>
parents: 9221
diff changeset
135 raise BadColor((red, green, blue))
73df24f6bccf all_names(): => unique_names()
Barry Warsaw <barry@python.org>
parents: 9221
diff changeset
136 return [name] + aliases
25877
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
137
7180
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
138
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
139 class RGBColorDB(ColorDB):
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
140 _re = re.compile(
10744
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
141 '\s*(?P<red>\d+)\s+(?P<green>\d+)\s+(?P<blue>\d+)\s+(?P<name>.*)')
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
142
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
143
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
144 class HTML40DB(ColorDB):
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
145 _re = re.compile('(?P<name>\S+)\s+(?P<hexrgb>#[0-9a-fA-F]{6})')
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
146
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
147 def _extractrgb(self, mo):
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
148 return rrggbb_to_triplet(mo.group('hexrgb'))
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
149
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
150 class LightlinkDB(HTML40DB):
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
151 _re = re.compile('(?P<name>(.+))\s+(?P<hexrgb>#[0-9a-fA-F]{6})')
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
152
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
153 def _extractname(self, mo):
18456
70af4a173a95 De-string-module-ification and other Python 2.x improvements.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
154 return mo.group('name').strip()
10744
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
155
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
156 class WebsafeDB(ColorDB):
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
157 _re = re.compile('(?P<hexrgb>#[0-9a-fA-F]{6})')
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
158
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
159 def _extractrgb(self, mo):
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
160 return rrggbb_to_triplet(mo.group('hexrgb'))
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
161
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
162 def _extractname(self, mo):
18456
70af4a173a95 De-string-module-ification and other Python 2.x improvements.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
163 return mo.group('hexrgb').upper()
7180
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
164
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
165
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
166
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
167 # format is a tuple (RE, SCANLINES, CLASS) where RE is a compiled regular
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
168 # expression, SCANLINES is the number of header lines to scan, and CLASS is
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
169 # the class to instantiate if a match is found
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
170
10744
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
171 FILETYPES = [
25877
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
172 (re.compile('Xorg'), RGBColorDB),
10744
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
173 (re.compile('XConsortium'), RGBColorDB),
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
174 (re.compile('HTML'), HTML40DB),
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
175 (re.compile('lightlink'), LightlinkDB),
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
176 (re.compile('Websafe'), WebsafeDB),
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
177 ]
7180
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
178
10744
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
179 def get_colordb(file, filetype=None):
7180
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
180 colordb = None
10744
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
181 fp = open(file)
7180
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
182 try:
10744
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
183 line = fp.readline()
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
184 if not line:
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
185 return None
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
186 # try to determine the type of RGB file it is
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
187 if filetype is None:
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
188 filetypes = FILETYPES
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
189 else:
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
190 filetypes = [filetype]
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
191 for typere, class_ in filetypes:
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
192 mo = typere.search(line)
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
193 if mo:
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
194 break
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
195 else:
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
196 # no matching type
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
197 return None
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
198 # we know the type and the class to grok the type, so suck it in
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
199 colordb = class_(fp)
7180
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
200 finally:
10744
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
201 fp.close()
7236
fd7a5993d06d Simplification of conversion routines
Barry Warsaw <barry@python.org>
parents: 7182
diff changeset
202 # save a global copy
fd7a5993d06d Simplification of conversion routines
Barry Warsaw <barry@python.org>
parents: 7182
diff changeset
203 global DEFAULT_DB
fd7a5993d06d Simplification of conversion routines
Barry Warsaw <barry@python.org>
parents: 7182
diff changeset
204 DEFAULT_DB = colordb
7180
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
205 return colordb
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
206
7182
59362ea7e0f8 BadColor: new exception class
Barry Warsaw <barry@python.org>
parents: 7181
diff changeset
207
59362ea7e0f8 BadColor: new exception class
Barry Warsaw <barry@python.org>
parents: 7181
diff changeset
208
7346
5a9256b8c338 Cache conversions from triplets to rrggbb's and vice versa. Wasteful
Barry Warsaw <barry@python.org>
parents: 7293
diff changeset
209 _namedict = {}
18456
70af4a173a95 De-string-module-ification and other Python 2.x improvements.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
210
70af4a173a95 De-string-module-ification and other Python 2.x improvements.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
211 def rrggbb_to_triplet(color):
7182
59362ea7e0f8 BadColor: new exception class
Barry Warsaw <barry@python.org>
parents: 7181
diff changeset
212 """Converts a #rrggbb color to the tuple (red, green, blue)."""
7346
5a9256b8c338 Cache conversions from triplets to rrggbb's and vice versa. Wasteful
Barry Warsaw <barry@python.org>
parents: 7293
diff changeset
213 rgbtuple = _namedict.get(color)
5a9256b8c338 Cache conversions from triplets to rrggbb's and vice versa. Wasteful
Barry Warsaw <barry@python.org>
parents: 7293
diff changeset
214 if rgbtuple is None:
9249
0283759407e1 rrggbb_to_triplet(): When color doesn't start with `#' raise a
Barry Warsaw <barry@python.org>
parents: 9225
diff changeset
215 if color[0] <> '#':
0283759407e1 rrggbb_to_triplet(): When color doesn't start with `#' raise a
Barry Warsaw <barry@python.org>
parents: 9225
diff changeset
216 raise BadColor(color)
25877
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
217 red = color[1:3]
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
218 green = color[3:5]
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
219 blue = color[5:7]
18456
70af4a173a95 De-string-module-ification and other Python 2.x improvements.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
220 rgbtuple = int(red, 16), int(green, 16), int(blue, 16)
25877
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
221 _namedict[color] = rgbtuple
7346
5a9256b8c338 Cache conversions from triplets to rrggbb's and vice versa. Wasteful
Barry Warsaw <barry@python.org>
parents: 7293
diff changeset
222 return rgbtuple
7236
fd7a5993d06d Simplification of conversion routines
Barry Warsaw <barry@python.org>
parents: 7182
diff changeset
223
7182
59362ea7e0f8 BadColor: new exception class
Barry Warsaw <barry@python.org>
parents: 7181
diff changeset
224
7346
5a9256b8c338 Cache conversions from triplets to rrggbb's and vice versa. Wasteful
Barry Warsaw <barry@python.org>
parents: 7293
diff changeset
225 _tripdict = {}
7236
fd7a5993d06d Simplification of conversion routines
Barry Warsaw <barry@python.org>
parents: 7182
diff changeset
226 def triplet_to_rrggbb(rgbtuple):
fd7a5993d06d Simplification of conversion routines
Barry Warsaw <barry@python.org>
parents: 7182
diff changeset
227 """Converts a (red, green, blue) tuple to #rrggbb."""
10744
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9367
diff changeset
228 global _tripdict
7346
5a9256b8c338 Cache conversions from triplets to rrggbb's and vice versa. Wasteful
Barry Warsaw <barry@python.org>
parents: 7293
diff changeset
229 hexname = _tripdict.get(rgbtuple)
5a9256b8c338 Cache conversions from triplets to rrggbb's and vice versa. Wasteful
Barry Warsaw <barry@python.org>
parents: 7293
diff changeset
230 if hexname is None:
25877
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
231 hexname = '#%02x%02x%02x' % rgbtuple
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
232 _tripdict[rgbtuple] = hexname
7346
5a9256b8c338 Cache conversions from triplets to rrggbb's and vice versa. Wasteful
Barry Warsaw <barry@python.org>
parents: 7293
diff changeset
233 return hexname
7182
59362ea7e0f8 BadColor: new exception class
Barry Warsaw <barry@python.org>
parents: 7181
diff changeset
234
59362ea7e0f8 BadColor: new exception class
Barry Warsaw <barry@python.org>
parents: 7181
diff changeset
235
7346
5a9256b8c338 Cache conversions from triplets to rrggbb's and vice versa. Wasteful
Barry Warsaw <barry@python.org>
parents: 7293
diff changeset
236 _maxtuple = (256.0,) * 3
9146
c4602236409e Fixed unit test
Barry Warsaw <barry@python.org>
parents: 7371
diff changeset
237 def triplet_to_fractional_rgb(rgbtuple):
7346
5a9256b8c338 Cache conversions from triplets to rrggbb's and vice versa. Wasteful
Barry Warsaw <barry@python.org>
parents: 7293
diff changeset
238 return map(operator.__div__, rgbtuple, _maxtuple)
7293
0dbec98acfc9 triplet_to_pmwrgb(): New function
Barry Warsaw <barry@python.org>
parents: 7239
diff changeset
239
0dbec98acfc9 triplet_to_pmwrgb(): New function
Barry Warsaw <barry@python.org>
parents: 7239
diff changeset
240
9165
d3ef4d10fb95 triplet_to_brightness(): New function
Barry Warsaw <barry@python.org>
parents: 9146
diff changeset
241 def triplet_to_brightness(rgbtuple):
d3ef4d10fb95 triplet_to_brightness(): New function
Barry Warsaw <barry@python.org>
parents: 9146
diff changeset
242 # return the brightness (grey level) along the scale 0.0==black to
d3ef4d10fb95 triplet_to_brightness(): New function
Barry Warsaw <barry@python.org>
parents: 9146
diff changeset
243 # 1.0==white
d3ef4d10fb95 triplet_to_brightness(): New function
Barry Warsaw <barry@python.org>
parents: 9146
diff changeset
244 r = 0.299
d3ef4d10fb95 triplet_to_brightness(): New function
Barry Warsaw <barry@python.org>
parents: 9146
diff changeset
245 g = 0.587
d3ef4d10fb95 triplet_to_brightness(): New function
Barry Warsaw <barry@python.org>
parents: 9146
diff changeset
246 b = 0.114
d3ef4d10fb95 triplet_to_brightness(): New function
Barry Warsaw <barry@python.org>
parents: 9146
diff changeset
247 return r*rgbtuple[0] + g*rgbtuple[1] + b*rgbtuple[2]
d3ef4d10fb95 triplet_to_brightness(): New function
Barry Warsaw <barry@python.org>
parents: 9146
diff changeset
248
d3ef4d10fb95 triplet_to_brightness(): New function
Barry Warsaw <barry@python.org>
parents: 9146
diff changeset
249
7180
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
250
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
251 if __name__ == '__main__':
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
252 colordb = get_colordb('/usr/openwin/lib/rgb.txt')
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
253 if not colordb:
25877
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
254 print 'No parseable color database found'
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
255 sys.exit(1)
7180
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
256 # on my system, this color matches exactly
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
257 target = 'navy'
9146
c4602236409e Fixed unit test
Barry Warsaw <barry@python.org>
parents: 7371
diff changeset
258 red, green, blue = rgbtuple = colordb.find_byname(target)
c4602236409e Fixed unit test
Barry Warsaw <barry@python.org>
parents: 7371
diff changeset
259 print target, ':', red, green, blue, triplet_to_rrggbb(rgbtuple)
c4602236409e Fixed unit test
Barry Warsaw <barry@python.org>
parents: 7371
diff changeset
260 name, aliases = colordb.find_byrgb(rgbtuple)
18456
70af4a173a95 De-string-module-ification and other Python 2.x improvements.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
261 print 'name:', name, 'aliases:', COMMASPACE.join(aliases)
25877
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
262 r, g, b = (1, 1, 128) # nearest to navy
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
263 r, g, b = (145, 238, 144) # nearest to lightgreen
15607ec49faf FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents: 18456
diff changeset
264 r, g, b = (255, 251, 250) # snow
7180
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
265 print 'finding nearest to', target, '...'
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
266 import time
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
267 t0 = time.time()
9367
f6819b926fb2 Updated __main__ unit test
Barry Warsaw <barry@python.org>
parents: 9249
diff changeset
268 nearest = colordb.nearest(r, g, b)
7180
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
269 t1 = time.time()
3a960b2e2667 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
270 print 'found nearest color', nearest, 'in', t1-t0, 'seconds'
9367
f6819b926fb2 Updated __main__ unit test
Barry Warsaw <barry@python.org>
parents: 9249
diff changeset
271 # dump the database
f6819b926fb2 Updated __main__ unit test
Barry Warsaw <barry@python.org>
parents: 9249
diff changeset
272 for n in colordb.unique_names():
f6819b926fb2 Updated __main__ unit test
Barry Warsaw <barry@python.org>
parents: 9249
diff changeset
273 r, g, b = colordb.find_byname(n)
f6819b926fb2 Updated __main__ unit test
Barry Warsaw <barry@python.org>
parents: 9249
diff changeset
274 aliases = colordb.aliases_of(r, g, b)
f6819b926fb2 Updated __main__ unit test
Barry Warsaw <barry@python.org>
parents: 9249
diff changeset
275 print '%20s: (%3d/%3d/%3d) == %s' % (n, r, g, b,
18456
70af4a173a95 De-string-module-ification and other Python 2.x improvements.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
276 SPACE.join(aliases[1:]))