Pillow/Tests/test_imagecolor.py

186 lines
8.7 KiB
Python
Raw Normal View History

from .helper import PillowTestCase
from PIL import Image
from PIL import ImageColor
2014-06-10 13:10:47 +04:00
class TestImageColor(PillowTestCase):
2016-09-17 12:22:27 +03:00
def test_hash(self):
2016-09-19 12:12:16 +03:00
# short 3 components
2014-06-10 13:10:47 +04:00
self.assertEqual((255, 0, 0), ImageColor.getrgb("#f00"))
2016-09-17 12:22:27 +03:00
self.assertEqual((0, 255, 0), ImageColor.getrgb("#0f0"))
self.assertEqual((0, 0, 255), ImageColor.getrgb("#00f"))
2016-09-19 12:12:16 +03:00
# short 4 components
2016-09-17 12:22:27 +03:00
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"))
2016-09-19 12:12:16 +03:00
# long 3 components
2016-09-17 12:22:27 +03:00
self.assertEqual((222, 0, 0), ImageColor.getrgb("#de0000"))
self.assertEqual((0, 222, 0), ImageColor.getrgb("#00de00"))
self.assertEqual((0, 0, 222), ImageColor.getrgb("#0000de"))
2016-09-19 12:12:16 +03:00
# long 4 components
2016-09-17 12:22:27 +03:00
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"))
2016-09-19 12:12:16 +03:00
# case insensitivity
2016-09-17 12:22:27 +03:00
self.assertEqual(ImageColor.getrgb("#DEF"), ImageColor.getrgb("#def"))
2019-06-13 18:54:46 +03:00
self.assertEqual(ImageColor.getrgb("#CDEF"), ImageColor.getrgb("#cdef"))
self.assertEqual(ImageColor.getrgb("#DEFDEF"), ImageColor.getrgb("#defdef"))
self.assertEqual(ImageColor.getrgb("#CDEFCDEF"), ImageColor.getrgb("#cdefcdef"))
2016-09-17 12:22:27 +03:00
2016-09-19 12:12:16 +03:00
# not a number
2016-09-17 12:22:27 +03:00
self.assertRaises(ValueError, ImageColor.getrgb, "#fo0")
self.assertRaises(ValueError, ImageColor.getrgb, "#fo00")
self.assertRaises(ValueError, ImageColor.getrgb, "#fo0000")
self.assertRaises(ValueError, ImageColor.getrgb, "#fo000000")
2016-09-19 12:12:16 +03:00
# wrong number of components
2016-09-17 12:22:27 +03:00
self.assertRaises(ValueError, ImageColor.getrgb, "#f0000")
self.assertRaises(ValueError, ImageColor.getrgb, "#f000000")
self.assertRaises(ValueError, ImageColor.getrgb, "#f00000000")
2016-09-19 12:12:16 +03:00
self.assertRaises(ValueError, ImageColor.getrgb, "#f000000000")
self.assertRaises(ValueError, ImageColor.getrgb, "#f00000 ")
2016-09-17 12:22:27 +03:00
def test_colormap(self):
self.assertEqual((0, 0, 0), ImageColor.getrgb("black"))
self.assertEqual((255, 255, 255), ImageColor.getrgb("white"))
2016-09-17 12:38:45 +03:00
self.assertEqual((255, 255, 255), ImageColor.getrgb("WHITE"))
2016-09-17 12:22:27 +03:00
2016-09-19 12:12:16 +03:00
self.assertRaises(ValueError, ImageColor.getrgb, "black ")
2016-09-17 12:22:27 +03:00
def test_functions(self):
2016-09-19 12:12:16 +03:00
# rgb numbers
2014-06-10 13:10:47 +04:00
self.assertEqual((255, 0, 0), ImageColor.getrgb("rgb(255,0,0)"))
2016-09-17 12:22:27 +03:00
self.assertEqual((0, 255, 0), ImageColor.getrgb("rgb(0,255,0)"))
self.assertEqual((0, 0, 255), ImageColor.getrgb("rgb(0,0,255)"))
2016-09-19 12:12:16 +03:00
# percents
2014-06-10 13:10:47 +04:00
self.assertEqual((255, 0, 0), ImageColor.getrgb("rgb(100%,0%,0%)"))
2016-09-17 12:22:27 +03:00
self.assertEqual((0, 255, 0), ImageColor.getrgb("rgb(0%,100%,0%)"))
self.assertEqual((0, 0, 255), ImageColor.getrgb("rgb(0%,0%,100%)"))
2016-09-19 12:12:16 +03:00
# rgba numbers
2014-06-10 13:10:47 +04:00
self.assertEqual((255, 0, 0, 0), ImageColor.getrgb("rgba(255,0,0,0)"))
2016-09-17 12:22:27 +03:00
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%)"))
2018-05-30 23:13:22 +03:00
self.assertEqual((255, 0, 0), ImageColor.getrgb("hsv(0,100%,100%)"))
self.assertEqual((255, 0, 0), ImageColor.getrgb("hsv(360,100%,100%)"))
2019-06-13 18:54:46 +03:00
self.assertEqual((0, 255, 255), ImageColor.getrgb("hsv(180,100%,100%)"))
2018-05-30 23:13:22 +03:00
2018-05-30 23:14:29 +03:00
# alternate format
2019-06-13 18:54:46 +03:00
self.assertEqual(
ImageColor.getrgb("hsb(0,100%,50%)"), ImageColor.getrgb("hsv(0,100%,50%)")
)
2018-05-30 23:14:29 +03:00
# floats
2019-06-13 18:54:46 +03:00
self.assertEqual((254, 3, 3), ImageColor.getrgb("hsl(0.1,99.2%,50.3%)"))
self.assertEqual((255, 0, 0), ImageColor.getrgb("hsl(360.,100.0%,50%)"))
2018-06-24 15:32:25 +03:00
2019-06-13 18:54:46 +03:00
self.assertEqual((253, 2, 2), ImageColor.getrgb("hsv(0.1,99.2%,99.3%)"))
self.assertEqual((255, 0, 0), ImageColor.getrgb("hsv(360.,100.0%,100%)"))
2018-05-30 23:13:22 +03:00
2016-09-19 12:12:16 +03:00
# case insensitivity
2019-06-13 18:54:46 +03:00
self.assertEqual(
ImageColor.getrgb("RGB(255,0,0)"), ImageColor.getrgb("rgb(255,0,0)")
)
self.assertEqual(
ImageColor.getrgb("RGB(100%,0%,0%)"), ImageColor.getrgb("rgb(100%,0%,0%)")
)
self.assertEqual(
ImageColor.getrgb("RGBA(255,0,0,0)"), ImageColor.getrgb("rgba(255,0,0,0)")
)
self.assertEqual(
ImageColor.getrgb("HSL(0,100%,50%)"), ImageColor.getrgb("hsl(0,100%,50%)")
)
self.assertEqual(
ImageColor.getrgb("HSV(0,100%,50%)"), ImageColor.getrgb("hsv(0,100%,50%)")
)
self.assertEqual(
ImageColor.getrgb("HSB(0,100%,50%)"), ImageColor.getrgb("hsb(0,100%,50%)")
)
2016-09-19 12:12:16 +03:00
# space agnosticism
2019-06-13 18:54:46 +03:00
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, 0), ImageColor.getrgb("rgba( 255 , 0 , 0 , 0 )")
)
self.assertEqual((255, 0, 0), ImageColor.getrgb("hsl( 0 , 100% , 50% )"))
self.assertEqual((255, 0, 0), ImageColor.getrgb("hsv( 0 , 100% , 100% )"))
2016-09-19 12:12:16 +03:00
# wrong number of components
2016-09-17 12:22:27 +03:00
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)")
2014-06-10 13:10:47 +04:00
2016-09-17 12:22:27 +03:00
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)")
2015-07-03 08:03:25 +03:00
2018-05-30 23:13:22 +03:00
self.assertRaises(ValueError, ImageColor.getrgb, "hsv(0,100%)")
self.assertRaises(ValueError, ImageColor.getrgb, "hsv(0,100%,0%,0%)")
self.assertRaises(ValueError, ImageColor.getrgb, "hsv(0%,100%,50%)")
self.assertRaises(ValueError, ImageColor.getrgb, "hsv(0,100,50%)")
self.assertRaises(ValueError, ImageColor.getrgb, "hsv(0,100%,50)")
2014-06-10 13:10:47 +04:00
# look for rounding errors (based on code by Tim Hatch)
def test_rounding_errors(self):
for color in ImageColor.colormap:
2019-06-13 18:54:46 +03:00
expected = Image.new("RGB", (1, 1), color).convert("L").getpixel((0, 0))
actual = ImageColor.getcolor(color, "L")
2014-06-10 13:10:47 +04:00
self.assertEqual(expected, actual)
self.assertEqual(
2019-06-13 18:54:46 +03:00
(0, 255, 115), ImageColor.getcolor("rgba(0, 255, 115, 33)", "RGB")
)
2014-06-10 13:10:47 +04:00
Image.new("RGB", (1, 1), "white")
self.assertEqual((0, 0, 0, 255), ImageColor.getcolor("black", "RGBA"))
2019-06-13 18:54:46 +03:00
self.assertEqual((255, 255, 255, 255), ImageColor.getcolor("white", "RGBA"))
2014-06-10 13:10:47 +04:00
self.assertEqual(
2019-06-13 18:54:46 +03:00
(0, 255, 115, 33), ImageColor.getcolor("rgba(0, 255, 115, 33)", "RGBA")
)
2014-06-10 13:10:47 +04:00
Image.new("RGBA", (1, 1), "white")
self.assertEqual(0, ImageColor.getcolor("black", "L"))
self.assertEqual(255, ImageColor.getcolor("white", "L"))
2019-06-13 18:54:46 +03:00
self.assertEqual(162, ImageColor.getcolor("rgba(0, 255, 115, 33)", "L"))
2014-06-10 13:10:47 +04:00
Image.new("L", (1, 1), "white")
self.assertEqual(0, ImageColor.getcolor("black", "1"))
self.assertEqual(255, ImageColor.getcolor("white", "1"))
# The following test is wrong, but is current behavior
# The correct result should be 255 due to the mode 1
2019-06-13 18:54:46 +03:00
self.assertEqual(162, ImageColor.getcolor("rgba(0, 255, 115, 33)", "1"))
2014-06-10 13:10:47 +04:00
# Correct behavior
# self.assertEqual(
# 255, ImageColor.getcolor("rgba(0, 255, 115, 33)", "1"))
Image.new("1", (1, 1), "white")
self.assertEqual((0, 255), ImageColor.getcolor("black", "LA"))
self.assertEqual((255, 255), ImageColor.getcolor("white", "LA"))
2019-06-13 18:54:46 +03:00
self.assertEqual((162, 33), ImageColor.getcolor("rgba(0, 255, 115, 33)", "LA"))
2014-06-10 13:10:47 +04:00
Image.new("LA", (1, 1), "white")