Merge pull request #309 from yoavweiss/master

Add RGBA support to ImageColor
This commit is contained in:
Alex Clark ☺ 2013-07-31 09:55:43 -07:00
commit b72471c8fe
2 changed files with 13 additions and 3 deletions

View File

@ -30,7 +30,6 @@ import re
# as an RGB value.
def getrgb(color):
# FIXME: add RGBA support
try:
rgb = colormap[color]
except KeyError:
@ -87,6 +86,14 @@ def getrgb(color):
int(rgb[1] * 255 + 0.5),
int(rgb[2] * 255 + 0.5)
)
m = re.match("rgba\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$", color)
if m:
return (
int(m.group(1)),
int(m.group(2)),
int(m.group(3)),
int(m.group(4))
)
raise ValueError("unknown color specifier: %r" % color)
def getcolor(color, mode):
@ -95,8 +102,8 @@ def getcolor(color, mode):
if mode == "RGB":
return color
if mode == "RGBA":
r, g, b = color
return r, g, b, 255
r, g, b, a = color
return r, g, b, a
if Image.getmodebase(mode) == "L":
r, g, b = color
return (r*299 + g*587 + b*114)//1000

View File

@ -9,8 +9,11 @@ from PIL import ImageColor
assert_equal((255, 0, 0), ImageColor.getrgb("#f00"))
assert_equal((255, 0, 0), ImageColor.getrgb("#ff0000"))
assert_equal((255, 0, 0), ImageColor.getrgb("rgb(255,0,0)"))
assert_equal((255, 0, 0), ImageColor.getrgb("rgb(255, 0, 0)"))
assert_equal((255, 0, 0), ImageColor.getrgb("rgb(100%,0%,0%)"))
assert_equal((255, 0, 0), ImageColor.getrgb("hsl(0, 100%, 50%)"))
assert_equal((255, 0, 0, 0), ImageColor.getrgb("rgba(255,0,0,0)"))
assert_equal((255, 0, 0, 0), ImageColor.getrgb("rgba(255, 0, 0, 0)"))
assert_equal((255, 0, 0), ImageColor.getrgb("red"))
# --------------------------------------------------------------------