2014-07-07 21:03:50 +04:00
|
|
|
from helper import unittest, PillowTestCase
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
from PIL import ImageColor
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
class TestImageColor(PillowTestCase):
|
|
|
|
|
|
|
|
def test_sanity(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"))
|
|
|
|
|
2015-07-03 09:22:56 +03:00
|
|
|
self.assertRaises(ValueError,
|
|
|
|
lambda: ImageColor.getrgb("invalid color"))
|
2015-07-03 08:03:25 +03:00
|
|
|
|
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 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))
|
|
|
|
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")
|
|
|
|
|
|
|
|
self.assertEqual((0, 0, 0, 255), ImageColor.getcolor("black", "RGBA"))
|
|
|
|
self.assertEqual(
|
|
|
|
(255, 255, 255, 255), ImageColor.getcolor("white", "RGBA"))
|
|
|
|
self.assertEqual(
|
|
|
|
(0, 255, 115, 33),
|
|
|
|
ImageColor.getcolor("rgba(0, 255, 115, 33)", "RGBA"))
|
|
|
|
Image.new("RGBA", (1, 1), "white")
|
|
|
|
|
|
|
|
self.assertEqual(0, ImageColor.getcolor("black", "L"))
|
|
|
|
self.assertEqual(255, ImageColor.getcolor("white", "L"))
|
2015-07-03 09:22:56 +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
|
|
|
|
self.assertEqual(
|
|
|
|
162, ImageColor.getcolor("rgba(0, 255, 115, 33)", "1"))
|
|
|
|
# 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"))
|
|
|
|
self.assertEqual(
|
|
|
|
(162, 33), ImageColor.getcolor("rgba(0, 255, 115, 33)", "LA"))
|
|
|
|
Image.new("LA", (1, 1), "white")
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|