From 9453129a57675f0188c475d2bc99916a950848e0 Mon Sep 17 00:00:00 2001 From: homm Date: Sat, 17 Sep 2016 12:38:45 +0300 Subject: [PATCH] simpler colormap lookup --- PIL/ImageColor.py | 20 +++++++------------- Tests/test_imagecolor.py | 1 + 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/PIL/ImageColor.py b/PIL/ImageColor.py index c912587e6..bbd67bf60 100644 --- a/PIL/ImageColor.py +++ b/PIL/ImageColor.py @@ -31,15 +31,9 @@ def getrgb(color): :param color: A color string :return: ``(red, green, blue[, alpha])`` """ - try: - rgb = colormap[color] - except KeyError: - try: - # fall back on case-insensitive lookup - rgb = colormap[color.lower()] - except KeyError: - rgb = None - # found color in cache + color = color.lower() + + rgb = colormap.get(color, None) if rgb: if isinstance(rgb, tuple): return rgb @@ -47,14 +41,14 @@ def getrgb(color): return rgb # check for known string formats - if re.match('#[a-f0-9]{3}$', color, re.I): + if re.match('#[a-f0-9]{3}$', color): return ( int(color[1]*2, 16), int(color[2]*2, 16), int(color[3]*2, 16), ) - if re.match('#[a-f0-9]{4}$', color, re.I): + if re.match('#[a-f0-9]{4}$', color): return ( int(color[1]*2, 16), int(color[2]*2, 16), @@ -62,14 +56,14 @@ def getrgb(color): int(color[4]*2, 16), ) - if re.match('#[a-f0-9]{6}$', color, re.I): + if re.match('#[a-f0-9]{6}$', color): return ( int(color[1:3], 16), int(color[3:5], 16), int(color[5:7], 16), ) - if re.match('#[a-f0-9]{8}$', color, re.I): + if re.match('#[a-f0-9]{8}$', color): return ( int(color[1:3], 16), int(color[3:5], 16), diff --git a/Tests/test_imagecolor.py b/Tests/test_imagecolor.py index f9ce78fd3..63b663205 100644 --- a/Tests/test_imagecolor.py +++ b/Tests/test_imagecolor.py @@ -39,6 +39,7 @@ class TestImageColor(PillowTestCase): def test_colormap(self): self.assertEqual((0, 0, 0), ImageColor.getrgb("black")) self.assertEqual((255, 255, 255), ImageColor.getrgb("white")) + self.assertEqual((255, 255, 255), ImageColor.getrgb("WHITE")) self.assertRaises(ValueError, ImageColor.getrgb, "black0")