Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2e7a320

Browse files
committed
rrggbb_to_triplet(), triplet_to_rrggbb(): Improvements given by GvR
1 parent 4435d5a commit 2e7a320

1 file changed

Lines changed: 3 additions & 8 deletions

File tree

Tools/pynche/ColorDB.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,30 +145,25 @@ def get_colordb(file, filetype=X_RGB_TXT):
145145

146146

147147
_namedict = {}
148-
def rrggbb_to_triplet(color):
148+
def rrggbb_to_triplet(color, atoi=string.atoi):
149149
"""Converts a #rrggbb color to the tuple (red, green, blue)."""
150150
rgbtuple = _namedict.get(color)
151151
if rgbtuple is None:
152152
assert color[0] == '#'
153153
red = color[1:3]
154154
green = color[3:5]
155155
blue = color[5:7]
156-
rgbtuple = tuple(map(lambda v: string.atoi(v, 16), (red, green, blue)))
156+
rgbtuple = (atoi(red, 16), atoi(green, 16), atoi(blue, 16))
157157
_namedict[color] = rgbtuple
158158
return rgbtuple
159159

160160

161161
_tripdict = {}
162162
def triplet_to_rrggbb(rgbtuple):
163163
"""Converts a (red, green, blue) tuple to #rrggbb."""
164-
def hexify(v):
165-
hexstr = hex(v)[2:4]
166-
if len(hexstr) < 2:
167-
hexstr = '0' + hexstr
168-
return hexstr
169164
hexname = _tripdict.get(rgbtuple)
170165
if hexname is None:
171-
hexname = '#%s%s%s' % tuple(map(hexify, rgbtuple))
166+
hexname = '#%02x%02x%02x' % rgbtuple
172167
_tripdict[rgbtuple] = hexname
173168
return hexname
174169

0 commit comments

Comments
 (0)