__init__(): simplify, use tuple as key to __byrgb, lowercase __byname key.

find_byrgb(): Changed name, interface (takes a tuple), and return
value

find_byname(): lowercase color name
This commit is contained in:
Barry Warsaw 1998-02-11 18:55:37 +00:00
parent 37400e8943
commit eb9b8af8fc
1 changed files with 22 additions and 22 deletions

View File

@ -14,6 +14,7 @@ Supporte file types are:
""" """
import sys import sys
import string
import re import re
from types import * from types import *
@ -31,10 +32,10 @@ class ColorDB:
# for now we only support 8 bit intensities. At least on OpenWindows, # 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 # all intensities in the /usr/openwin/lib/rgb.txt file are 8-bit
# #
# key is rrggbb, value is (name, [aliases]) # key is (red, green, blue) tuple, value is (name, [aliases])
self.__byrrggbb = {} self.__byrgb = {}
# #
# key is name, value is (red, green, blue, rrggbb) # key is name, value is (red, green, blue)
self.__byname = {} self.__byname = {}
# #
while 1: while 1:
@ -49,35 +50,34 @@ class ColorDB:
continue continue
# #
# extract the red, green, blue, and name # extract the red, green, blue, and name
#
red, green, blue = map(int, mo.group('red', 'green', 'blue')) red, green, blue = map(int, mo.group('red', 'green', 'blue'))
name = mo.group('name') name = mo.group('name')
# keyname = string.lower(name)
# calculate the 24 bit representation of the color
rrggbb = (red << 16) + (blue << 8) + green
# #
# TBD: for now the `name' is just the first named color with the # TBD: for now the `name' is just the first named color with the
# rgb values we find. Later, we might want to make the two word # rgb values we find. Later, we might want to make the two word
# version the `name', or the CapitalizedVersion, etc. # version the `name', or the CapitalizedVersion, etc.
foundname, aliases = self.__byrrggbb.get(rrggbb, (name, [])) #
key = (red, green, blue)
foundname, aliases = self.__byrgb.get(key, (name, []))
if foundname <> name and foundname not in aliases: if foundname <> name and foundname not in aliases:
aliases.append(name) aliases.append(name)
# self.__byrgb[key] = (foundname, aliases)
# add to by 24bit value
self.__byrrggbb[rrggbb] = (foundname, aliases)
# #
# add to byname lookup # add to byname lookup
point = (red, green, blue, rrggbb) #
self.__byname[name] = point self.__byname[keyname] = key
lineno = lineno + 1 lineno = lineno + 1
def find(self, red, green, blue): def find_byrgb(self, rgbtuple):
rrggbb = (red << 16) + (blue << 8) + green
try: try:
return self.__byrrggbb[rrggbb] return self.__byrgb[rgbtuple]
except KeyError: except KeyError:
raise BadColor(red, green, blue) raise BadColor(rgbtuple)
def find_byname(self, name): def find_byname(self, name):
name = string.lower(name)
try: try:
return self.__byname[name] return self.__byname[name]
except KeyError: except KeyError:
@ -85,13 +85,13 @@ class ColorDB:
def nearest(self, rgbtuple): def nearest(self, rgbtuple):
# TBD: use Voronoi diagrams, Delaunay triangulation, or octree for # TBD: use Voronoi diagrams, Delaunay triangulation, or octree for
# speeding up the locating of nearest point. This is really # speeding up the locating of nearest point. Exhaustive search is
# inefficient! # inefficient, but may be fast enough.
red, green, blue = rgbtuple red, green, blue = rgbtuple
nearest = -1 nearest = -1
nearest_name = '' nearest_name = ''
for name, aliases in self.__byrrggbb.values(): for name, aliases in self.__byrgb.values():
r, g, b, rrggbb = self.__byname[name] r, g, b = self.__byname[string.lower(name)]
rdelta = red - r rdelta = red - r
gdelta = green - g gdelta = green - g
bdelta = blue - b bdelta = blue - b
@ -175,9 +175,9 @@ if __name__ == '__main__':
# on my system, this color matches exactly # on my system, this color matches exactly
target = 'navy' target = 'navy'
target = 'snow' target = 'snow'
red, green, blue, rrggbb = colordb.find_byname(target) red, green, blue = colordb.find_byname(target)
print target, ':', red, green, blue, hex(rrggbb) print target, ':', red, green, blue, hex(rrggbb)
name, aliases = colordb.find(red, green, blue) name, aliases = colordb.find_byrgb((red, green, blue))
print 'name:', name, 'aliases:', string.join(aliases, ", ") print 'name:', name, 'aliases:', string.join(aliases, ", ")
target = (1, 1, 128) # nearest to navy target = (1, 1, 128) # nearest to navy
target = (145, 238, 144) # nearest to lightgreen target = (145, 238, 144) # nearest to lightgreen