diff options
author | Barry Warsaw <barry@python.org> | 1998-02-18 00:02:26 (GMT) |
---|---|---|
committer | Barry Warsaw <barry@python.org> | 1998-02-18 00:02:26 (GMT) |
commit | 8d3e5ee552b70512b13a910da594b5c32f3e9c26 (patch) | |
tree | 78d9af1d234845d0d5a8cf806dd7b8df8278c404 /Tools/pynche | |
parent | a219e7976f7d4c6873b38cbb180b95879c61b06d (diff) | |
download | cpython-8d3e5ee552b70512b13a910da594b5c32f3e9c26.zip cpython-8d3e5ee552b70512b13a910da594b5c32f3e9c26.tar.gz cpython-8d3e5ee552b70512b13a910da594b5c32f3e9c26.tar.bz2 |
Cache conversions from triplets to rrggbb's and vice versa. Wasteful
on space, but improves performance. Also use map to calculate
triplet_to_pmwrgb().
Diffstat (limited to 'Tools/pynche')
-rw-r--r-- | Tools/pynche/ColorDB.py | 35 |
1 files changed, 22 insertions, 13 deletions
diff --git a/Tools/pynche/ColorDB.py b/Tools/pynche/ColorDB.py index f9ba27e..4e4045a 100644 --- a/Tools/pynche/ColorDB.py +++ b/Tools/pynche/ColorDB.py @@ -17,6 +17,7 @@ import sys import string import re from types import * +import operator class BadColor(Exception): pass @@ -143,17 +144,21 @@ def get_colordb(file, filetype=X_RGB_TXT): +_namedict = {} def rrggbb_to_triplet(color): """Converts a #rrggbb color to the tuple (red, green, blue).""" - if color[0] <> '#': - raise BadColor(color) - - red = color[1:3] - green = color[3:5] - blue = color[5:7] - return tuple(map(lambda v: string.atoi(v, 16), (red, green, blue))) - - + rgbtuple = _namedict.get(color) + if rgbtuple is None: + assert color[0] == '#' + red = color[1:3] + green = color[3:5] + blue = color[5:7] + rgbtuple = tuple(map(lambda v: string.atoi(v, 16), (red, green, blue))) + _namedict[color] = rgbtuple + return rgbtuple + + +_tripdict = {} def triplet_to_rrggbb(rgbtuple): """Converts a (red, green, blue) tuple to #rrggbb.""" def hexify(v): @@ -161,12 +166,16 @@ def triplet_to_rrggbb(rgbtuple): if len(hexstr) < 2: hexstr = '0' + hexstr return hexstr - return '#%s%s%s' % tuple(map(hexify, rgbtuple)) + hexname = _tripdict.get(rgbtuple) + if hexname is None: + hexname = '#%s%s%s' % tuple(map(hexify, rgbtuple)) + _tripdict[rgbtuple] = hexname + return hexname -def triplet_to_pmwrgb(rgbtuple, MAX=256.0): - r, g, b = rgbtuple - return r/MAX, g/MAX, b/MAX +_maxtuple = (256.0,) * 3 +def triplet_to_pmwrgb(rgbtuple): + return map(operator.__div__, rgbtuple, _maxtuple) |