| 
									
										
										
										
											1998-01-31 00:29:41 +00:00
										 |  |  |  | """Color Database.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-10-02 16:01:42 +00:00
										 |  |  |  | This file contains one class, called ColorDB, and several utility functions. | 
					
						
							|  |  |  |  | The class must be instantiated by the get_colordb() function in this file, | 
					
						
							|  |  |  |  | passing it a filename to read a database out of. | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | The get_colordb() function will try to examine the file to figure out what the | 
					
						
							|  |  |  |  | format of the file is.  If it can't figure out the file format, or it has | 
					
						
							|  |  |  |  | trouble reading the file, None is returned.  You can pass get_colordb() an | 
					
						
							|  |  |  |  | optional filetype argument. | 
					
						
							| 
									
										
										
										
											1998-01-31 00:29:41 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | Supporte file types are: | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     X_RGB_TXT -- X Consortium rgb.txt format files.  Three columns of numbers | 
					
						
							|  |  |  |  |                  from 0 .. 255 separated by whitespace.  Arbitrary trailing | 
					
						
							|  |  |  |  |                  columns used as the color name. | 
					
						
							| 
									
										
										
										
											1998-10-02 16:01:42 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | The utility functions are useful for converting between the various expected | 
					
						
							|  |  |  |  | color formats, and for calculating other color values. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-01-31 00:29:41 +00:00
										 |  |  |  | """
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | import sys | 
					
						
							|  |  |  |  | import re | 
					
						
							| 
									
										
										
										
											1998-02-11 17:19:23 +00:00
										 |  |  |  | from types import * | 
					
						
							| 
									
										
										
										
											1998-02-18 00:02:26 +00:00
										 |  |  |  | import operator | 
					
						
							| 
									
										
										
										
											1998-01-31 00:29:41 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-01-31 23:38:48 +00:00
										 |  |  |  | class BadColor(Exception): | 
					
						
							|  |  |  |  |     pass | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-02-11 17:19:23 +00:00
										 |  |  |  | DEFAULT_DB = None | 
					
						
							| 
									
										
										
										
											2001-07-10 21:38:47 +00:00
										 |  |  |  | SPACE = ' ' | 
					
						
							|  |  |  |  | COMMASPACE = ', ' | 
					
						
							| 
									
										
										
										
											1998-02-11 17:19:23 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-09-25 22:51:36 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-01-31 00:29:41 +00:00
										 |  |  |  |  | 
					
						
							|  |  |  |  | # generic class | 
					
						
							|  |  |  |  | class ColorDB: | 
					
						
							| 
									
										
										
										
											1999-04-26 23:17:16 +00:00
										 |  |  |  |     def __init__(self, fp): | 
					
						
							|  |  |  |  |         lineno = 2 | 
					
						
							|  |  |  |  |         self.__name = fp.name | 
					
						
							| 
									
										
										
										
											1998-01-31 00:29:41 +00:00
										 |  |  |  | 	# Maintain several dictionaries for indexing into the color database. | 
					
						
							|  |  |  |  | 	# Note that while Tk supports RGB intensities of 4, 8, 12, or 16 bits,  | 
					
						
							|  |  |  |  | 	# for now we only support 8 bit intensities.  At least on OpenWindows,  | 
					
						
							|  |  |  |  | 	# all intensities in the /usr/openwin/lib/rgb.txt file are 8-bit | 
					
						
							|  |  |  |  | 	# | 
					
						
							| 
									
										
										
										
											1998-02-11 18:55:37 +00:00
										 |  |  |  | 	# key is (red, green, blue) tuple, value is (name, [aliases]) | 
					
						
							|  |  |  |  | 	self.__byrgb = {} | 
					
						
							|  |  |  |  | 	# key is name, value is (red, green, blue) | 
					
						
							| 
									
										
										
										
											1998-01-31 00:29:41 +00:00
										 |  |  |  | 	self.__byname = {} | 
					
						
							| 
									
										
										
										
											1998-10-02 14:43:30 +00:00
										 |  |  |  |         # all unique names (non-aliases).  built-on demand | 
					
						
							|  |  |  |  |         self.__allnames = None | 
					
						
							| 
									
										
										
										
											1998-01-31 00:29:41 +00:00
										 |  |  |  | 	while 1: | 
					
						
							|  |  |  |  | 	    line = fp.readline() | 
					
						
							|  |  |  |  | 	    if not line: | 
					
						
							|  |  |  |  | 		break | 
					
						
							|  |  |  |  | 	    # get this compiled regular expression from derived class | 
					
						
							|  |  |  |  | 	    mo = self._re.match(line) | 
					
						
							|  |  |  |  | 	    if not mo: | 
					
						
							| 
									
										
										
										
											2001-07-10 21:38:47 +00:00
										 |  |  |  |                 print >> sys.stderr, 'Error in', fp.name, ' line', lineno | 
					
						
							|  |  |  |  | 		lineno += 1 | 
					
						
							| 
									
										
										
										
											1998-01-31 00:29:41 +00:00
										 |  |  |  | 		continue | 
					
						
							|  |  |  |  | 	    # extract the red, green, blue, and name | 
					
						
							| 
									
										
										
										
											1999-04-26 23:17:16 +00:00
										 |  |  |  |             red, green, blue = self._extractrgb(mo) | 
					
						
							|  |  |  |  |             name = self._extractname(mo) | 
					
						
							| 
									
										
										
										
											2001-07-10 21:38:47 +00:00
										 |  |  |  | 	    keyname = name.lower() | 
					
						
							|  |  |  |  | 	    # BAW: for now the `name' is just the first named color with the | 
					
						
							| 
									
										
										
										
											1998-01-31 00:29:41 +00:00
										 |  |  |  | 	    # rgb values we find.  Later, we might want to make the two word | 
					
						
							|  |  |  |  | 	    # version the `name', or the CapitalizedVersion, etc. | 
					
						
							| 
									
										
										
										
											1998-02-11 18:55:37 +00:00
										 |  |  |  | 	    key = (red, green, blue) | 
					
						
							|  |  |  |  | 	    foundname, aliases = self.__byrgb.get(key, (name, [])) | 
					
						
							| 
									
										
										
										
											1998-01-31 00:29:41 +00:00
										 |  |  |  | 	    if foundname <> name and foundname not in aliases: | 
					
						
							|  |  |  |  | 		aliases.append(name) | 
					
						
							| 
									
										
										
										
											1998-02-11 18:55:37 +00:00
										 |  |  |  | 	    self.__byrgb[key] = (foundname, aliases) | 
					
						
							| 
									
										
										
										
											1998-01-31 00:29:41 +00:00
										 |  |  |  | 	    # add to byname lookup | 
					
						
							| 
									
										
										
										
											1998-02-11 18:55:37 +00:00
										 |  |  |  | 	    self.__byname[keyname] = key | 
					
						
							| 
									
										
										
										
											1998-01-31 00:29:41 +00:00
										 |  |  |  | 	    lineno = lineno + 1 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-04-26 23:17:16 +00:00
										 |  |  |  |     # override in derived classes | 
					
						
							|  |  |  |  |     def _extractrgb(self, mo): | 
					
						
							| 
									
										
										
										
											2001-07-10 21:38:47 +00:00
										 |  |  |  |         return [int(x) for x in mo.group('red', 'green', 'blue')] | 
					
						
							| 
									
										
										
										
											1999-04-26 23:17:16 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     def _extractname(self, mo): | 
					
						
							|  |  |  |  |         return mo.group('name') | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     def filename(self): | 
					
						
							|  |  |  |  |         return self.__name | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-02-11 18:55:37 +00:00
										 |  |  |  |     def find_byrgb(self, rgbtuple): | 
					
						
							| 
									
										
										
										
											1999-04-26 23:17:16 +00:00
										 |  |  |  |         """Return name for rgbtuple""" | 
					
						
							| 
									
										
										
										
											1998-01-31 23:38:48 +00:00
										 |  |  |  | 	try: | 
					
						
							| 
									
										
										
										
											1998-02-11 18:55:37 +00:00
										 |  |  |  | 	    return self.__byrgb[rgbtuple] | 
					
						
							| 
									
										
										
										
											1998-01-31 23:38:48 +00:00
										 |  |  |  | 	except KeyError: | 
					
						
							| 
									
										
										
										
											1998-02-11 18:55:37 +00:00
										 |  |  |  | 	    raise BadColor(rgbtuple) | 
					
						
							| 
									
										
										
										
											1998-01-31 00:29:41 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     def find_byname(self, name): | 
					
						
							| 
									
										
										
										
											1999-04-26 23:17:16 +00:00
										 |  |  |  |         """Return (red, green, blue) for name""" | 
					
						
							| 
									
										
										
										
											2001-07-10 21:38:47 +00:00
										 |  |  |  | 	name = name.lower() | 
					
						
							| 
									
										
										
										
											1998-01-31 23:38:48 +00:00
										 |  |  |  | 	try: | 
					
						
							|  |  |  |  | 	    return self.__byname[name] | 
					
						
							|  |  |  |  | 	except KeyError: | 
					
						
							|  |  |  |  | 	    raise BadColor(name) | 
					
						
							| 
									
										
										
										
											1998-01-31 00:29:41 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-09-29 20:03:15 +00:00
										 |  |  |  |     def nearest(self, red, green, blue): | 
					
						
							| 
									
										
										
										
											1999-04-26 23:17:16 +00:00
										 |  |  |  |         """Return the name of color nearest (red, green, blue)""" | 
					
						
							| 
									
										
										
										
											2001-07-10 21:38:47 +00:00
										 |  |  |  | 	# BAW: should we use Voronoi diagrams, Delaunay triangulation, or | 
					
						
							| 
									
										
										
										
											1999-04-26 23:17:16 +00:00
										 |  |  |  | 	# octree for speeding up the locating of nearest point?  Exhaustive | 
					
						
							|  |  |  |  | 	# search is inefficient, but seems fast enough. | 
					
						
							| 
									
										
										
										
											1998-01-31 00:29:41 +00:00
										 |  |  |  | 	nearest = -1 | 
					
						
							|  |  |  |  | 	nearest_name = '' | 
					
						
							| 
									
										
										
										
											1998-02-11 18:55:37 +00:00
										 |  |  |  | 	for name, aliases in self.__byrgb.values(): | 
					
						
							| 
									
										
										
										
											2001-07-10 21:38:47 +00:00
										 |  |  |  | 	    r, g, b = self.__byname[name.lower()] | 
					
						
							| 
									
										
										
										
											1998-01-31 00:29:41 +00:00
										 |  |  |  | 	    rdelta = red - r | 
					
						
							|  |  |  |  | 	    gdelta = green - g | 
					
						
							|  |  |  |  | 	    bdelta = blue - b | 
					
						
							|  |  |  |  | 	    distance = rdelta * rdelta + gdelta * gdelta + bdelta * bdelta | 
					
						
							|  |  |  |  | 	    if nearest == -1 or distance < nearest: | 
					
						
							|  |  |  |  | 		nearest = distance | 
					
						
							|  |  |  |  | 		nearest_name = name | 
					
						
							|  |  |  |  | 	return nearest_name | 
					
						
							| 
									
										
										
										
											1998-10-02 14:43:30 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-10-02 15:59:20 +00:00
										 |  |  |  |     def unique_names(self): | 
					
						
							| 
									
										
										
										
											1998-10-02 14:43:30 +00:00
										 |  |  |  |         # sorted | 
					
						
							|  |  |  |  |         if not self.__allnames: | 
					
						
							|  |  |  |  |             self.__allnames = [] | 
					
						
							|  |  |  |  |             for name, aliases in self.__byrgb.values(): | 
					
						
							|  |  |  |  |                 self.__allnames.append(name) | 
					
						
							| 
									
										
										
										
											1998-10-02 15:59:20 +00:00
										 |  |  |  |             # sort irregardless of case | 
					
						
							|  |  |  |  |             def nocase_cmp(n1, n2): | 
					
						
							| 
									
										
										
										
											2001-07-10 21:38:47 +00:00
										 |  |  |  |                 return cmp(n1.lower(), n2.lower()) | 
					
						
							| 
									
										
										
										
											1998-10-02 15:59:20 +00:00
										 |  |  |  |             self.__allnames.sort(nocase_cmp) | 
					
						
							| 
									
										
										
										
											1998-10-02 14:43:30 +00:00
										 |  |  |  |         return self.__allnames | 
					
						
							| 
									
										
										
										
											1998-10-02 15:59:20 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     def aliases_of(self, red, green, blue): | 
					
						
							|  |  |  |  |         try: | 
					
						
							|  |  |  |  |             name, aliases = self.__byrgb[(red, green, blue)] | 
					
						
							|  |  |  |  |         except KeyError: | 
					
						
							|  |  |  |  |             raise BadColor((red, green, blue)) | 
					
						
							|  |  |  |  |         return [name] + aliases | 
					
						
							| 
									
										
										
										
											1998-01-31 00:29:41 +00:00
										 |  |  |  | 	 | 
					
						
							|  |  |  |  |  | 
					
						
							|  |  |  |  | class RGBColorDB(ColorDB): | 
					
						
							|  |  |  |  |     _re = re.compile( | 
					
						
							| 
									
										
										
										
											1999-04-26 23:17:16 +00:00
										 |  |  |  |         '\s*(?P<red>\d+)\s+(?P<green>\d+)\s+(?P<blue>\d+)\s+(?P<name>.*)') | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | class HTML40DB(ColorDB): | 
					
						
							|  |  |  |  |     _re = re.compile('(?P<name>\S+)\s+(?P<hexrgb>#[0-9a-fA-F]{6})') | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     def _extractrgb(self, mo): | 
					
						
							|  |  |  |  |         return rrggbb_to_triplet(mo.group('hexrgb')) | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | class LightlinkDB(HTML40DB): | 
					
						
							|  |  |  |  |     _re = re.compile('(?P<name>(.+))\s+(?P<hexrgb>#[0-9a-fA-F]{6})') | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     def _extractname(self, mo): | 
					
						
							| 
									
										
										
										
											2001-07-10 21:38:47 +00:00
										 |  |  |  |         return mo.group('name').strip() | 
					
						
							| 
									
										
										
										
											1999-04-26 23:17:16 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | class WebsafeDB(ColorDB): | 
					
						
							|  |  |  |  |     _re = re.compile('(?P<hexrgb>#[0-9a-fA-F]{6})') | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     def _extractrgb(self, mo): | 
					
						
							|  |  |  |  |         return rrggbb_to_triplet(mo.group('hexrgb')) | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     def _extractname(self, mo): | 
					
						
							| 
									
										
										
										
											2001-07-10 21:38:47 +00:00
										 |  |  |  |         return mo.group('hexrgb').upper() | 
					
						
							| 
									
										
										
										
											1998-01-31 00:29:41 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |  | 
					
						
							|  |  |  |  | # format is a tuple (RE, SCANLINES, CLASS) where RE is a compiled regular | 
					
						
							|  |  |  |  | # expression, SCANLINES is the number of header lines to scan, and CLASS is | 
					
						
							|  |  |  |  | # the class to instantiate if a match is found | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-04-26 23:17:16 +00:00
										 |  |  |  | FILETYPES = [ | 
					
						
							|  |  |  |  |     (re.compile('XConsortium'), RGBColorDB), | 
					
						
							|  |  |  |  |     (re.compile('HTML'), HTML40DB), | 
					
						
							|  |  |  |  |     (re.compile('lightlink'), LightlinkDB), | 
					
						
							|  |  |  |  |     (re.compile('Websafe'), WebsafeDB), | 
					
						
							|  |  |  |  |     ] | 
					
						
							| 
									
										
										
										
											1998-01-31 00:29:41 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-04-26 23:17:16 +00:00
										 |  |  |  | def get_colordb(file, filetype=None): | 
					
						
							| 
									
										
										
										
											1998-01-31 00:29:41 +00:00
										 |  |  |  |     colordb = None | 
					
						
							| 
									
										
										
										
											1999-04-26 23:17:16 +00:00
										 |  |  |  |     fp = open(file) | 
					
						
							| 
									
										
										
										
											1998-01-31 00:29:41 +00:00
										 |  |  |  |     try: | 
					
						
							| 
									
										
										
										
											1999-04-26 23:17:16 +00:00
										 |  |  |  |         line = fp.readline() | 
					
						
							|  |  |  |  |         if not line: | 
					
						
							|  |  |  |  |             return None | 
					
						
							|  |  |  |  |         # try to determine the type of RGB file it is | 
					
						
							|  |  |  |  |         if filetype is None: | 
					
						
							|  |  |  |  |             filetypes = FILETYPES | 
					
						
							|  |  |  |  |         else: | 
					
						
							|  |  |  |  |             filetypes = [filetype] | 
					
						
							|  |  |  |  |         for typere, class_ in filetypes: | 
					
						
							|  |  |  |  |             mo = typere.search(line) | 
					
						
							|  |  |  |  |             if mo: | 
					
						
							|  |  |  |  |                 break | 
					
						
							|  |  |  |  |         else: | 
					
						
							|  |  |  |  |             # no matching type | 
					
						
							|  |  |  |  |             return None | 
					
						
							|  |  |  |  |         # we know the type and the class to grok the type, so suck it in | 
					
						
							|  |  |  |  |         colordb = class_(fp) | 
					
						
							| 
									
										
										
										
											1998-01-31 00:29:41 +00:00
										 |  |  |  |     finally: | 
					
						
							| 
									
										
										
										
											1999-04-26 23:17:16 +00:00
										 |  |  |  |         fp.close() | 
					
						
							| 
									
										
										
										
											1998-02-11 17:19:23 +00:00
										 |  |  |  |     # save a global copy | 
					
						
							|  |  |  |  |     global DEFAULT_DB | 
					
						
							|  |  |  |  |     DEFAULT_DB = colordb | 
					
						
							| 
									
										
										
										
											1998-01-31 00:29:41 +00:00
										 |  |  |  |     return colordb | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-01-31 23:38:48 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |  | 
					
						
							| 
									
										
										
										
											1998-02-18 00:02:26 +00:00
										 |  |  |  | _namedict = {} | 
					
						
							| 
									
										
										
										
											2001-07-10 21:38:47 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | def rrggbb_to_triplet(color): | 
					
						
							| 
									
										
										
										
											1998-01-31 23:38:48 +00:00
										 |  |  |  |     """Converts a #rrggbb color to the tuple (red, green, blue).""" | 
					
						
							| 
									
										
										
										
											1998-02-18 00:02:26 +00:00
										 |  |  |  |     rgbtuple = _namedict.get(color) | 
					
						
							|  |  |  |  |     if rgbtuple is None: | 
					
						
							| 
									
										
										
										
											1998-10-06 18:10:59 +00:00
										 |  |  |  |         if color[0] <> '#': | 
					
						
							|  |  |  |  |             raise BadColor(color) | 
					
						
							| 
									
										
										
										
											1998-02-18 00:02:26 +00:00
										 |  |  |  | 	red = color[1:3] | 
					
						
							|  |  |  |  | 	green = color[3:5] | 
					
						
							|  |  |  |  | 	blue = color[5:7] | 
					
						
							| 
									
										
										
										
											2001-07-10 21:38:47 +00:00
										 |  |  |  |         rgbtuple = int(red, 16), int(green, 16), int(blue, 16) | 
					
						
							| 
									
										
										
										
											1998-02-18 00:02:26 +00:00
										 |  |  |  | 	_namedict[color] = rgbtuple | 
					
						
							|  |  |  |  |     return rgbtuple | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | _tripdict = {} | 
					
						
							| 
									
										
										
										
											1998-02-11 17:19:23 +00:00
										 |  |  |  | def triplet_to_rrggbb(rgbtuple): | 
					
						
							|  |  |  |  |     """Converts a (red, green, blue) tuple to #rrggbb.""" | 
					
						
							| 
									
										
										
										
											1999-04-26 23:17:16 +00:00
										 |  |  |  |     global _tripdict | 
					
						
							| 
									
										
										
										
											1998-02-18 00:02:26 +00:00
										 |  |  |  |     hexname = _tripdict.get(rgbtuple) | 
					
						
							|  |  |  |  |     if hexname is None: | 
					
						
							| 
									
										
										
										
											1998-02-18 17:01:12 +00:00
										 |  |  |  | 	hexname = '#%02x%02x%02x' % rgbtuple | 
					
						
							| 
									
										
										
										
											1998-02-18 00:02:26 +00:00
										 |  |  |  | 	_tripdict[rgbtuple] = hexname | 
					
						
							|  |  |  |  |     return hexname | 
					
						
							| 
									
										
										
										
											1998-01-31 23:38:48 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-02-18 00:02:26 +00:00
										 |  |  |  | _maxtuple = (256.0,) * 3 | 
					
						
							| 
									
										
										
										
											1998-09-25 22:51:36 +00:00
										 |  |  |  | def triplet_to_fractional_rgb(rgbtuple): | 
					
						
							| 
									
										
										
										
											1998-02-18 00:02:26 +00:00
										 |  |  |  |     return map(operator.__div__, rgbtuple, _maxtuple) | 
					
						
							| 
									
										
										
										
											1998-02-13 21:27:56 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-09-28 23:39:18 +00:00
										 |  |  |  | def triplet_to_brightness(rgbtuple): | 
					
						
							|  |  |  |  |     # return the brightness (grey level) along the scale 0.0==black to | 
					
						
							|  |  |  |  |     # 1.0==white | 
					
						
							|  |  |  |  |     r = 0.299 | 
					
						
							|  |  |  |  |     g = 0.587 | 
					
						
							|  |  |  |  |     b = 0.114 | 
					
						
							|  |  |  |  |     return r*rgbtuple[0] + g*rgbtuple[1] + b*rgbtuple[2] | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-01-31 00:29:41 +00:00
										 |  |  |  |  | 
					
						
							|  |  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |  |     colordb = get_colordb('/usr/openwin/lib/rgb.txt') | 
					
						
							|  |  |  |  |     if not colordb: | 
					
						
							|  |  |  |  | 	print 'No parseable color database found' | 
					
						
							|  |  |  |  | 	sys.exit(1) | 
					
						
							|  |  |  |  |     # on my system, this color matches exactly | 
					
						
							|  |  |  |  |     target = 'navy' | 
					
						
							| 
									
										
										
										
											1998-09-25 22:51:36 +00:00
										 |  |  |  |     red, green, blue = rgbtuple = colordb.find_byname(target) | 
					
						
							|  |  |  |  |     print target, ':', red, green, blue, triplet_to_rrggbb(rgbtuple) | 
					
						
							|  |  |  |  |     name, aliases = colordb.find_byrgb(rgbtuple) | 
					
						
							| 
									
										
										
										
											2001-07-10 21:38:47 +00:00
										 |  |  |  |     print 'name:', name, 'aliases:', COMMASPACE.join(aliases) | 
					
						
							| 
									
										
										
										
											1998-10-15 02:18:08 +00:00
										 |  |  |  |     r, g, b = (1, 1, 128)			  # nearest to navy | 
					
						
							|  |  |  |  |     r, g, b = (145, 238, 144)			  # nearest to lightgreen | 
					
						
							|  |  |  |  |     r, g, b = (255, 251, 250)			  # snow | 
					
						
							| 
									
										
										
										
											1998-01-31 00:29:41 +00:00
										 |  |  |  |     print 'finding nearest to', target, '...' | 
					
						
							|  |  |  |  |     import time | 
					
						
							|  |  |  |  |     t0 = time.time() | 
					
						
							| 
									
										
										
										
											1998-10-15 02:18:08 +00:00
										 |  |  |  |     nearest = colordb.nearest(r, g, b) | 
					
						
							| 
									
										
										
										
											1998-01-31 00:29:41 +00:00
										 |  |  |  |     t1 = time.time() | 
					
						
							|  |  |  |  |     print 'found nearest color', nearest, 'in', t1-t0, 'seconds' | 
					
						
							| 
									
										
										
										
											1998-10-15 02:18:08 +00:00
										 |  |  |  |     # dump the database | 
					
						
							|  |  |  |  |     for n in colordb.unique_names(): | 
					
						
							|  |  |  |  |         r, g, b = colordb.find_byname(n) | 
					
						
							|  |  |  |  |         aliases = colordb.aliases_of(r, g, b) | 
					
						
							|  |  |  |  |         print '%20s: (%3d/%3d/%3d) == %s' % (n, r, g, b, | 
					
						
							| 
									
										
										
										
											2001-07-10 21:38:47 +00:00
										 |  |  |  |                                              SPACE.join(aliases[1:])) |