mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-11-10 19:56:47 +03:00
complete getrgb tests
This commit is contained in:
parent
951143c936
commit
859a75ba07
|
@ -84,6 +84,7 @@ def getrgb(color):
|
|||
int(m.group(2)),
|
||||
int(m.group(3))
|
||||
)
|
||||
|
||||
m = re.match("rgb\(\s*(\d+)%\s*,\s*(\d+)%\s*,\s*(\d+)%\s*\)$", color)
|
||||
if m:
|
||||
return (
|
||||
|
@ -91,6 +92,7 @@ def getrgb(color):
|
|||
int((int(m.group(2)) * 255) / 100.0 + 0.5),
|
||||
int((int(m.group(3)) * 255) / 100.0 + 0.5)
|
||||
)
|
||||
|
||||
m = re.match("hsl\(\s*(\d+)\s*,\s*(\d+)%\s*,\s*(\d+)%\s*\)$", color)
|
||||
if m:
|
||||
from colorsys import hls_to_rgb
|
||||
|
@ -104,6 +106,7 @@ 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:
|
||||
|
|
|
@ -6,20 +6,85 @@ from PIL import ImageColor
|
|||
|
||||
class TestImageColor(PillowTestCase):
|
||||
|
||||
def test_sanity(self):
|
||||
def test_hash(self):
|
||||
self.assertEqual((255, 0, 0), ImageColor.getrgb("#f00"))
|
||||
self.assertEqual((255, 0, 0), ImageColor.getrgb("#ff0000"))
|
||||
self.assertEqual((255, 0, 0), ImageColor.getrgb("rgb(255,0,0)"))
|
||||
self.assertEqual((255, 0, 0), ImageColor.getrgb("rgb(255, 0, 0)"))
|
||||
self.assertEqual((255, 0, 0), ImageColor.getrgb("rgb(100%,0%,0%)"))
|
||||
self.assertEqual((255, 0, 0), ImageColor.getrgb("hsl(0, 100%, 50%)"))
|
||||
self.assertEqual((255, 0, 0, 0), ImageColor.getrgb("rgba(255,0,0,0)"))
|
||||
self.assertEqual(
|
||||
(255, 0, 0, 0), ImageColor.getrgb("rgba(255, 0, 0, 0)"))
|
||||
self.assertEqual((255, 0, 0), ImageColor.getrgb("red"))
|
||||
self.assertEqual((0, 255, 0), ImageColor.getrgb("#0f0"))
|
||||
self.assertEqual((0, 0, 255), ImageColor.getrgb("#00f"))
|
||||
|
||||
self.assertRaises(ValueError,
|
||||
lambda: ImageColor.getrgb("invalid color"))
|
||||
self.assertEqual((255, 0, 0, 0), ImageColor.getrgb("#f000"))
|
||||
self.assertEqual((0, 255, 0, 0), ImageColor.getrgb("#0f00"))
|
||||
self.assertEqual((0, 0, 255, 0), ImageColor.getrgb("#00f0"))
|
||||
self.assertEqual((0, 0, 0, 255), ImageColor.getrgb("#000f"))
|
||||
|
||||
self.assertEqual((222, 0, 0), ImageColor.getrgb("#de0000"))
|
||||
self.assertEqual((0, 222, 0), ImageColor.getrgb("#00de00"))
|
||||
self.assertEqual((0, 0, 222), ImageColor.getrgb("#0000de"))
|
||||
|
||||
self.assertEqual((222, 0, 0, 0), ImageColor.getrgb("#de000000"))
|
||||
self.assertEqual((0, 222, 0, 0), ImageColor.getrgb("#00de0000"))
|
||||
self.assertEqual((0, 0, 222, 0), ImageColor.getrgb("#0000de00"))
|
||||
self.assertEqual((0, 0, 0, 222), ImageColor.getrgb("#000000de"))
|
||||
|
||||
self.assertEqual(ImageColor.getrgb("#DEF"), ImageColor.getrgb("#def"))
|
||||
|
||||
self.assertRaises(ValueError, ImageColor.getrgb, "#fo0")
|
||||
self.assertRaises(ValueError, ImageColor.getrgb, "#fo00")
|
||||
self.assertRaises(ValueError, ImageColor.getrgb, "#fo0000")
|
||||
self.assertRaises(ValueError, ImageColor.getrgb, "#fo000000")
|
||||
|
||||
self.assertRaises(ValueError, ImageColor.getrgb, "#f0000")
|
||||
self.assertRaises(ValueError, ImageColor.getrgb, "#f000000")
|
||||
self.assertRaises(ValueError, ImageColor.getrgb, "#f00000000")
|
||||
|
||||
def test_colormap(self):
|
||||
self.assertEqual((0, 0, 0), ImageColor.getrgb("black"))
|
||||
self.assertEqual((255, 255, 255), ImageColor.getrgb("white"))
|
||||
|
||||
self.assertRaises(ValueError, ImageColor.getrgb, "black0")
|
||||
|
||||
def test_functions(self):
|
||||
self.assertEqual((255, 0, 0), ImageColor.getrgb("rgb(255,0,0)"))
|
||||
self.assertEqual((0, 255, 0), ImageColor.getrgb("rgb(0,255,0)"))
|
||||
self.assertEqual((0, 0, 255), ImageColor.getrgb("rgb(0,0,255)"))
|
||||
|
||||
self.assertEqual((255, 0, 0), ImageColor.getrgb("rgb(100%,0%,0%)"))
|
||||
self.assertEqual((0, 255, 0), ImageColor.getrgb("rgb(0%,100%,0%)"))
|
||||
self.assertEqual((0, 0, 255), ImageColor.getrgb("rgb(0%,0%,100%)"))
|
||||
|
||||
self.assertEqual((255, 0, 0, 0), ImageColor.getrgb("rgba(255,0,0,0)"))
|
||||
self.assertEqual((0, 255, 0, 0), ImageColor.getrgb("rgba(0,255,0,0)"))
|
||||
self.assertEqual((0, 0, 255, 0), ImageColor.getrgb("rgba(0,0,255,0)"))
|
||||
self.assertEqual((0, 0, 0, 255), ImageColor.getrgb("rgba(0,0,0,255)"))
|
||||
|
||||
self.assertEqual((255, 0, 0), ImageColor.getrgb("hsl(0,100%,50%)"))
|
||||
self.assertEqual((255, 0, 0), ImageColor.getrgb("hsl(360,100%,50%)"))
|
||||
self.assertEqual((0, 255, 255), ImageColor.getrgb("hsl(180,100%,50%)"))
|
||||
|
||||
actual = ImageColor.getrgb("rgb( 255 , 0 , 0 )")
|
||||
self.assertEqual((255, 0, 0), actual)
|
||||
actual = ImageColor.getrgb("rgb( 100% , 0% , 0% )")
|
||||
self.assertEqual((255, 0, 0), actual)
|
||||
actual = ImageColor.getrgb("rgba( 255 , 0 , 0 , 0 )")
|
||||
self.assertEqual((255, 0, 0, 0), actual)
|
||||
actual = ImageColor.getrgb("hsl( 0 , 100% , 50% )")
|
||||
self.assertEqual((255, 0, 0), actual)
|
||||
|
||||
self.assertRaises(ValueError, ImageColor.getrgb, "rgb(255,0)")
|
||||
self.assertRaises(ValueError, ImageColor.getrgb, "rgb(255,0,0,0)")
|
||||
|
||||
self.assertRaises(ValueError, ImageColor.getrgb, "rgb(100%,0%)")
|
||||
self.assertRaises(ValueError, ImageColor.getrgb, "rgb(100%,0%,0)")
|
||||
self.assertRaises(ValueError, ImageColor.getrgb, "rgb(100%,0%,0 %)")
|
||||
self.assertRaises(ValueError, ImageColor.getrgb, "rgb(100%,0%,0%,0%)")
|
||||
|
||||
self.assertRaises(ValueError, ImageColor.getrgb, "rgba(255,0,0)")
|
||||
self.assertRaises(ValueError, ImageColor.getrgb, "rgba(255,0,0,0,0)")
|
||||
|
||||
self.assertRaises(ValueError, ImageColor.getrgb, "hsl(0,100%)")
|
||||
self.assertRaises(ValueError, ImageColor.getrgb, "hsl(0,100%,0%,0%)")
|
||||
self.assertRaises(ValueError, ImageColor.getrgb, "hsl(0%,100%,50%)")
|
||||
self.assertRaises(ValueError, ImageColor.getrgb, "hsl(0,100,50%)")
|
||||
self.assertRaises(ValueError, ImageColor.getrgb, "hsl(0,100%,50)")
|
||||
|
||||
# look for rounding errors (based on code by Tim Hatch)
|
||||
def test_rounding_errors(self):
|
||||
|
@ -27,11 +92,9 @@ class TestImageColor(PillowTestCase):
|
|||
for color in list(ImageColor.colormap.keys()):
|
||||
expected = Image.new(
|
||||
"RGB", (1, 1), color).convert("L").getpixel((0, 0))
|
||||
actual = Image.new("L", (1, 1), color).getpixel((0, 0))
|
||||
actual = ImageColor.getcolor(color, 'L')
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
self.assertEqual((0, 0, 0), ImageColor.getcolor("black", "RGB"))
|
||||
self.assertEqual((255, 255, 255), ImageColor.getcolor("white", "RGB"))
|
||||
self.assertEqual(
|
||||
(0, 255, 115), ImageColor.getcolor("rgba(0, 255, 115, 33)", "RGB"))
|
||||
Image.new("RGB", (1, 1), "white")
|
||||
|
|
Loading…
Reference in New Issue
Block a user