Mercurial > cpython-lru_cache-weakref
annotate Tools/pynche/ColorDB.py @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | 63741c7521aa |
| children |
| rev | line source |
|---|---|
| 7180 | 1 """Color Database. |
| 2 | |
| 9225 | 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. | |
| 7180 | 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. | |
| 9225 | 17 |
| 18 The utility functions are useful for converting between the various expected | |
| 19 color formats, and for calculating other color values. | |
| 20 | |
| 7180 | 21 """ |
| 22 | |
| 23 import sys | |
| 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 | 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 | 35 |
| 7180 | 36 |
| 37 # generic class | |
| 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 |
|
44859
feba5d5cac22
Merged revisions 60151-60159,60161-60168,60170,60172-60173,60175 via svnmerge from
Georg Brandl <georg@python.org>
parents:
42427
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: |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
25877
diff
changeset
|
57 print('Error in', fp.name, ' line', lineno, file=sys.stderr) |
|
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, [])) |
|
47103
63741c7521aa
More 2to3 fixes in the Tools directory. Fixes #2893.
Georg Brandl <georg@python.org>
parents:
45049
diff
changeset
|
69 if foundname != name and foundname not in aliases: |
|
25877
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 | 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 | 92 |
| 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 | 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) |
|
47103
63741c7521aa
More 2to3 fixes in the Tools directory. Fixes #2893.
Georg Brandl <georg@python.org>
parents:
45049
diff
changeset
|
125 self.__allnames.sort(key=str.lower) |
|
9221
3c9fec57b858
all_names(): Return just the unique names of rgb colors
Barry Warsaw <barry@python.org>
parents:
9173
diff
changeset
|
126 return self.__allnames |
|
9224
73df24f6bccf
all_names(): => unique_names()
Barry Warsaw <barry@python.org>
parents:
9221
diff
changeset
|
127 |
|
73df24f6bccf
all_names(): => unique_names()
Barry Warsaw <barry@python.org>
parents:
9221
diff
changeset
|
128 def aliases_of(self, red, green, blue): |
|
73df24f6bccf
all_names(): => unique_names()
Barry Warsaw <barry@python.org>
parents:
9221
diff
changeset
|
129 try: |
|
73df24f6bccf
all_names(): => unique_names()
Barry Warsaw <barry@python.org>
parents:
9221
diff
changeset
|
130 name, aliases = self.__byrgb[(red, green, blue)] |
|
73df24f6bccf
all_names(): => unique_names()
Barry Warsaw <barry@python.org>
parents:
9221
diff
changeset
|
131 except KeyError: |
|
73df24f6bccf
all_names(): => unique_names()
Barry Warsaw <barry@python.org>
parents:
9221
diff
changeset
|
132 raise BadColor((red, green, blue)) |
|
73df24f6bccf
all_names(): => unique_names()
Barry Warsaw <barry@python.org>
parents:
9221
diff
changeset
|
133 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
|
134 |
| 7180 | 135 |
| 136 class RGBColorDB(ColorDB): | |
| 137 _re = re.compile( | |
|
10744
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
138 '\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
|
139 |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
140 |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
141 class HTML40DB(ColorDB): |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
142 _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
|
143 |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
144 def _extractrgb(self, mo): |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
145 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
|
146 |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
147 class LightlinkDB(HTML40DB): |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
148 _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
|
149 |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
150 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
|
151 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
|
152 |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
153 class WebsafeDB(ColorDB): |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
154 _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
|
155 |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
156 def _extractrgb(self, mo): |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
157 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
|
158 |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
159 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
|
160 return mo.group('hexrgb').upper() |
| 7180 | 161 |
| 162 | |
| 163 | |
| 164 # format is a tuple (RE, SCANLINES, CLASS) where RE is a compiled regular | |
| 165 # expression, SCANLINES is the number of header lines to scan, and CLASS is | |
| 166 # the class to instantiate if a match is found | |
| 167 | |
|
10744
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
168 FILETYPES = [ |
|
25877
15607ec49faf
FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents:
18456
diff
changeset
|
169 (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
|
170 (re.compile('XConsortium'), RGBColorDB), |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
171 (re.compile('HTML'), HTML40DB), |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
172 (re.compile('lightlink'), LightlinkDB), |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
173 (re.compile('Websafe'), WebsafeDB), |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
174 ] |
| 7180 | 175 |
|
10744
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
176 def get_colordb(file, filetype=None): |
| 7180 | 177 colordb = None |
|
10744
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
178 fp = open(file) |
| 7180 | 179 try: |
|
10744
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
180 line = fp.readline() |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
181 if not line: |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
182 return None |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
183 # 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
|
184 if filetype is None: |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
185 filetypes = FILETYPES |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
186 else: |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
187 filetypes = [filetype] |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
188 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
|
189 mo = typere.search(line) |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
190 if mo: |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
191 break |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
192 else: |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
193 # no matching type |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
194 return None |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
195 # 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
|
196 colordb = class_(fp) |
| 7180 | 197 finally: |
|
10744
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9367
diff
changeset
|
198 fp.close() |
|
7236
fd7a5993d06d
Simplification of conversion routines
Barry Warsaw <barry@python.org>
parents:
7182
diff
changeset
|
199 # save a global copy |
|
fd7a5993d06d
Simplification of conversion routines
Barry Warsaw <barry@python.org>
parents:
7182
diff
changeset
|
200 global DEFAULT_DB |
|
fd7a5993d06d
Simplification of conversion routines
Barry Warsaw <barry@python.org>
parents:
7182
diff
changeset
|
201 DEFAULT_DB = colordb |
| 7180 | 202 return colordb |
| 203 | |
|
7182
59362ea7e0f8
BadColor: new exception class
Barry Warsaw <barry@python.org>
parents:
7181
diff
changeset
|
204 |
|
59362ea7e0f8
BadColor: new exception class
Barry Warsaw <barry@python.org>
parents:
7181
diff
changeset
|
205 |
|
7346
5a9256b8c338
Cache conversions from triplets to rrggbb's and vice versa. Wasteful
Barry Warsaw <barry@python.org>
parents:
7293
diff
changeset
|
206 _namedict = {} |
|
18456
70af4a173a95
De-string-module-ification and other Python 2.x improvements.
Barry Warsaw <barry@python.org>
parents:
10744
diff
changeset
|
207 |
|
70af4a173a95
De-string-module-ification and other Python 2.x improvements.
Barry Warsaw <barry@python.org>
parents:
10744
diff
changeset
|
208 def rrggbb_to_triplet(color): |
|
7182
59362ea7e0f8
BadColor: new exception class
Barry Warsaw <barry@python.org>
parents:
7181
diff
changeset
|
209 """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
|
210 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
|
211 if rgbtuple is None: |
|
47103
63741c7521aa
More 2to3 fixes in the Tools directory. Fixes #2893.
Georg Brandl <georg@python.org>
parents:
45049
diff
changeset
|
212 if color[0] != '#': |
|
9249
0283759407e1
rrggbb_to_triplet(): When color doesn't start with `#' raise a
Barry Warsaw <barry@python.org>
parents:
9225
diff
changeset
|
213 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
|
214 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
|
215 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
|
216 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
|
217 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
|
218 _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
|
219 return rgbtuple |
|
7236
fd7a5993d06d
Simplification of conversion routines
Barry Warsaw <barry@python.org>
parents:
7182
diff
changeset
|
220 |
|
7182
59362ea7e0f8
BadColor: new exception class
Barry Warsaw <barry@python.org>
parents:
7181
diff
changeset
|
221 |
|
7346
5a9256b8c338
Cache conversions from triplets to rrggbb's and vice versa. Wasteful
Barry Warsaw <barry@python.org>
parents:
7293
diff
changeset
|
222 _tripdict = {} |
|
7236
fd7a5993d06d
Simplification of conversion routines
Barry Warsaw <barry@python.org>
parents:
7182
diff
changeset
|
223 def triplet_to_rrggbb(rgbtuple): |
|
fd7a5993d06d
Simplification of conversion routines
Barry Warsaw <barry@python.org>
parents:
7182
diff
changeset
|
224 """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
|
225 global _tripdict |
|
7346
5a9256b8c338
Cache conversions from triplets to rrggbb's and vice versa. Wasteful
Barry Warsaw <barry@python.org>
parents:
7293
diff
changeset
|
226 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
|
227 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
|
228 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
|
229 _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
|
230 return hexname |
|
7182
59362ea7e0f8
BadColor: new exception class
Barry Warsaw <barry@python.org>
parents:
7181
diff
changeset
|
231 |
|
59362ea7e0f8
BadColor: new exception class
Barry Warsaw <barry@python.org>
parents:
7181
diff
changeset
|
232 |
|
7346
5a9256b8c338
Cache conversions from triplets to rrggbb's and vice versa. Wasteful
Barry Warsaw <barry@python.org>
parents:
7293
diff
changeset
|
233 _maxtuple = (256.0,) * 3 |
| 9146 | 234 def triplet_to_fractional_rgb(rgbtuple): |
|
47103
63741c7521aa
More 2to3 fixes in the Tools directory. Fixes #2893.
Georg Brandl <georg@python.org>
parents:
45049
diff
changeset
|
235 return list(map(operator.__div__, rgbtuple, _maxtuple)) |
|
7293
0dbec98acfc9
triplet_to_pmwrgb(): New function
Barry Warsaw <barry@python.org>
parents:
7239
diff
changeset
|
236 |
|
0dbec98acfc9
triplet_to_pmwrgb(): New function
Barry Warsaw <barry@python.org>
parents:
7239
diff
changeset
|
237 |
|
9165
d3ef4d10fb95
triplet_to_brightness(): New function
Barry Warsaw <barry@python.org>
parents:
9146
diff
changeset
|
238 def triplet_to_brightness(rgbtuple): |
|
d3ef4d10fb95
triplet_to_brightness(): New function
Barry Warsaw <barry@python.org>
parents:
9146
diff
changeset
|
239 # 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
|
240 # 1.0==white |
|
d3ef4d10fb95
triplet_to_brightness(): New function
Barry Warsaw <barry@python.org>
parents:
9146
diff
changeset
|
241 r = 0.299 |
|
d3ef4d10fb95
triplet_to_brightness(): New function
Barry Warsaw <barry@python.org>
parents:
9146
diff
changeset
|
242 g = 0.587 |
|
d3ef4d10fb95
triplet_to_brightness(): New function
Barry Warsaw <barry@python.org>
parents:
9146
diff
changeset
|
243 b = 0.114 |
|
d3ef4d10fb95
triplet_to_brightness(): New function
Barry Warsaw <barry@python.org>
parents:
9146
diff
changeset
|
244 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
|
245 |
|
d3ef4d10fb95
triplet_to_brightness(): New function
Barry Warsaw <barry@python.org>
parents:
9146
diff
changeset
|
246 |
| 7180 | 247 |
| 248 if __name__ == '__main__': | |
| 249 colordb = get_colordb('/usr/openwin/lib/rgb.txt') | |
| 250 if not colordb: | |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
25877
diff
changeset
|
251 print('No parseable color database found') |
|
25877
15607ec49faf
FILETYPES: Newer XFree86 rgb.txt files use the key Xorg instead of
Barry Warsaw <barry@python.org>
parents:
18456
diff
changeset
|
252 sys.exit(1) |
| 7180 | 253 # on my system, this color matches exactly |
| 254 target = 'navy' | |
| 9146 | 255 red, green, blue = rgbtuple = colordb.find_byname(target) |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
25877
diff
changeset
|
256 print(target, ':', red, green, blue, triplet_to_rrggbb(rgbtuple)) |
| 9146 | 257 name, aliases = colordb.find_byrgb(rgbtuple) |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
25877
diff
changeset
|
258 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
|
259 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
|
260 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
|
261 r, g, b = (255, 251, 250) # snow |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
25877
diff
changeset
|
262 print('finding nearest to', target, '...') |
| 7180 | 263 import time |
| 264 t0 = time.time() | |
|
9367
f6819b926fb2
Updated __main__ unit test
Barry Warsaw <barry@python.org>
parents:
9249
diff
changeset
|
265 nearest = colordb.nearest(r, g, b) |
| 7180 | 266 t1 = time.time() |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
25877
diff
changeset
|
267 print('found nearest color', nearest, 'in', t1-t0, 'seconds') |
|
9367
f6819b926fb2
Updated __main__ unit test
Barry Warsaw <barry@python.org>
parents:
9249
diff
changeset
|
268 # dump the database |
|
f6819b926fb2
Updated __main__ unit test
Barry Warsaw <barry@python.org>
parents:
9249
diff
changeset
|
269 for n in colordb.unique_names(): |
|
f6819b926fb2
Updated __main__ unit test
Barry Warsaw <barry@python.org>
parents:
9249
diff
changeset
|
270 r, g, b = colordb.find_byname(n) |
|
f6819b926fb2
Updated __main__ unit test
Barry Warsaw <barry@python.org>
parents:
9249
diff
changeset
|
271 aliases = colordb.aliases_of(r, g, b) |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
25877
diff
changeset
|
272 print('%20s: (%3d/%3d/%3d) == %s' % (n, r, g, b, |
|
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
25877
diff
changeset
|
273 SPACE.join(aliases[1:]))) |
