1
from __future__ import annotations
5
from PIL import Image, ImageColor
8
def test_hash() -> None:
10
assert (255, 0, 0) == ImageColor.getrgb("#f00")
11
assert (0, 255, 0) == ImageColor.getrgb("#0f0")
12
assert (0, 0, 255) == ImageColor.getrgb("#00f")
15
assert (255, 0, 0, 0) == ImageColor.getrgb("#f000")
16
assert (0, 255, 0, 0) == ImageColor.getrgb("#0f00")
17
assert (0, 0, 255, 0) == ImageColor.getrgb("#00f0")
18
assert (0, 0, 0, 255) == ImageColor.getrgb("#000f")
21
assert (222, 0, 0) == ImageColor.getrgb("#de0000")
22
assert (0, 222, 0) == ImageColor.getrgb("#00de00")
23
assert (0, 0, 222) == ImageColor.getrgb("#0000de")
26
assert (222, 0, 0, 0) == ImageColor.getrgb("#de000000")
27
assert (0, 222, 0, 0) == ImageColor.getrgb("#00de0000")
28
assert (0, 0, 222, 0) == ImageColor.getrgb("#0000de00")
29
assert (0, 0, 0, 222) == ImageColor.getrgb("#000000de")
32
assert ImageColor.getrgb("#DEF") == ImageColor.getrgb("#def")
33
assert ImageColor.getrgb("#CDEF") == ImageColor.getrgb("#cdef")
34
assert ImageColor.getrgb("#DEFDEF") == ImageColor.getrgb("#defdef")
35
assert ImageColor.getrgb("#CDEFCDEF") == ImageColor.getrgb("#cdefcdef")
38
with pytest.raises(ValueError):
39
ImageColor.getrgb("#fo0")
40
with pytest.raises(ValueError):
41
ImageColor.getrgb("#fo00")
42
with pytest.raises(ValueError):
43
ImageColor.getrgb("#fo0000")
44
with pytest.raises(ValueError):
45
ImageColor.getrgb("#fo000000")
48
with pytest.raises(ValueError):
49
ImageColor.getrgb("#f0000")
50
with pytest.raises(ValueError):
51
ImageColor.getrgb("#f000000")
52
with pytest.raises(ValueError):
53
ImageColor.getrgb("#f00000000")
54
with pytest.raises(ValueError):
55
ImageColor.getrgb("#f000000000")
56
with pytest.raises(ValueError):
57
ImageColor.getrgb("#f00000 ")
60
def test_colormap() -> None:
61
assert (0, 0, 0) == ImageColor.getrgb("black")
62
assert (255, 255, 255) == ImageColor.getrgb("white")
63
assert (255, 255, 255) == ImageColor.getrgb("WHITE")
65
with pytest.raises(ValueError):
66
ImageColor.getrgb("black ")
69
def test_functions() -> None:
71
assert (255, 0, 0) == ImageColor.getrgb("rgb(255,0,0)")
72
assert (0, 255, 0) == ImageColor.getrgb("rgb(0,255,0)")
73
assert (0, 0, 255) == ImageColor.getrgb("rgb(0,0,255)")
76
assert (255, 0, 0) == ImageColor.getrgb("rgb(100%,0%,0%)")
77
assert (0, 255, 0) == ImageColor.getrgb("rgb(0%,100%,0%)")
78
assert (0, 0, 255) == ImageColor.getrgb("rgb(0%,0%,100%)")
81
assert (255, 0, 0, 0) == ImageColor.getrgb("rgba(255,0,0,0)")
82
assert (0, 255, 0, 0) == ImageColor.getrgb("rgba(0,255,0,0)")
83
assert (0, 0, 255, 0) == ImageColor.getrgb("rgba(0,0,255,0)")
84
assert (0, 0, 0, 255) == ImageColor.getrgb("rgba(0,0,0,255)")
86
assert (255, 0, 0) == ImageColor.getrgb("hsl(0,100%,50%)")
87
assert (255, 0, 0) == ImageColor.getrgb("hsl(360,100%,50%)")
88
assert (0, 255, 255) == ImageColor.getrgb("hsl(180,100%,50%)")
90
assert (255, 0, 0) == ImageColor.getrgb("hsv(0,100%,100%)")
91
assert (255, 0, 0) == ImageColor.getrgb("hsv(360,100%,100%)")
92
assert (0, 255, 255) == ImageColor.getrgb("hsv(180,100%,100%)")
95
assert ImageColor.getrgb("hsb(0,100%,50%)") == ImageColor.getrgb("hsv(0,100%,50%)")
98
assert (254, 3, 3) == ImageColor.getrgb("hsl(0.1,99.2%,50.3%)")
99
assert (255, 0, 0) == ImageColor.getrgb("hsl(360.,100.0%,50%)")
101
assert (253, 2, 2) == ImageColor.getrgb("hsv(0.1,99.2%,99.3%)")
102
assert (255, 0, 0) == ImageColor.getrgb("hsv(360.,100.0%,100%)")
105
assert ImageColor.getrgb("RGB(255,0,0)") == ImageColor.getrgb("rgb(255,0,0)")
106
assert ImageColor.getrgb("RGB(100%,0%,0%)") == ImageColor.getrgb("rgb(100%,0%,0%)")
107
assert ImageColor.getrgb("RGBA(255,0,0,0)") == ImageColor.getrgb("rgba(255,0,0,0)")
108
assert ImageColor.getrgb("HSL(0,100%,50%)") == ImageColor.getrgb("hsl(0,100%,50%)")
109
assert ImageColor.getrgb("HSV(0,100%,50%)") == ImageColor.getrgb("hsv(0,100%,50%)")
110
assert ImageColor.getrgb("HSB(0,100%,50%)") == ImageColor.getrgb("hsb(0,100%,50%)")
113
assert (255, 0, 0) == ImageColor.getrgb("rgb( 255 , 0 , 0 )")
114
assert (255, 0, 0) == ImageColor.getrgb("rgb( 100% , 0% , 0% )")
115
assert (255, 0, 0, 0) == ImageColor.getrgb("rgba( 255 , 0 , 0 , 0 )")
116
assert (255, 0, 0) == ImageColor.getrgb("hsl( 0 , 100% , 50% )")
117
assert (255, 0, 0) == ImageColor.getrgb("hsv( 0 , 100% , 100% )")
120
with pytest.raises(ValueError):
121
ImageColor.getrgb("rgb(255,0)")
122
with pytest.raises(ValueError):
123
ImageColor.getrgb("rgb(255,0,0,0)")
125
with pytest.raises(ValueError):
126
ImageColor.getrgb("rgb(100%,0%)")
127
with pytest.raises(ValueError):
128
ImageColor.getrgb("rgb(100%,0%,0)")
129
with pytest.raises(ValueError):
130
ImageColor.getrgb("rgb(100%,0%,0 %)")
131
with pytest.raises(ValueError):
132
ImageColor.getrgb("rgb(100%,0%,0%,0%)")
134
with pytest.raises(ValueError):
135
ImageColor.getrgb("rgba(255,0,0)")
136
with pytest.raises(ValueError):
137
ImageColor.getrgb("rgba(255,0,0,0,0)")
139
with pytest.raises(ValueError):
140
ImageColor.getrgb("hsl(0,100%)")
141
with pytest.raises(ValueError):
142
ImageColor.getrgb("hsl(0,100%,0%,0%)")
143
with pytest.raises(ValueError):
144
ImageColor.getrgb("hsl(0%,100%,50%)")
145
with pytest.raises(ValueError):
146
ImageColor.getrgb("hsl(0,100,50%)")
147
with pytest.raises(ValueError):
148
ImageColor.getrgb("hsl(0,100%,50)")
150
with pytest.raises(ValueError):
151
ImageColor.getrgb("hsv(0,100%)")
152
with pytest.raises(ValueError):
153
ImageColor.getrgb("hsv(0,100%,0%,0%)")
154
with pytest.raises(ValueError):
155
ImageColor.getrgb("hsv(0%,100%,50%)")
156
with pytest.raises(ValueError):
157
ImageColor.getrgb("hsv(0,100,50%)")
158
with pytest.raises(ValueError):
159
ImageColor.getrgb("hsv(0,100%,50)")
163
def test_rounding_errors() -> None:
164
for color in ImageColor.colormap:
165
expected = Image.new("RGB", (1, 1), color).convert("L").getpixel((0, 0))
166
actual = ImageColor.getcolor(color, "L")
167
assert expected == actual
169
assert (0, 255, 115) == ImageColor.getcolor("rgba(0, 255, 115, 33)", "RGB")
170
Image.new("RGB", (1, 1), "white")
172
assert (0, 0, 0, 255) == ImageColor.getcolor("black", "RGBA")
173
assert (255, 255, 255, 255) == ImageColor.getcolor("white", "RGBA")
174
assert (0, 255, 115, 33) == ImageColor.getcolor("rgba(0, 255, 115, 33)", "RGBA")
175
Image.new("RGBA", (1, 1), "white")
177
assert 0 == ImageColor.getcolor("black", "L")
178
assert 255 == ImageColor.getcolor("white", "L")
179
assert 163 == ImageColor.getcolor("rgba(0, 255, 115, 33)", "L")
180
Image.new("L", (1, 1), "white")
182
assert 0 == ImageColor.getcolor("black", "1")
183
assert 255 == ImageColor.getcolor("white", "1")
186
assert 163 == ImageColor.getcolor("rgba(0, 255, 115, 33)", "1")
190
Image.new("1", (1, 1), "white")
192
assert (0, 255) == ImageColor.getcolor("black", "LA")
193
assert (255, 255) == ImageColor.getcolor("white", "LA")
194
assert (163, 33) == ImageColor.getcolor("rgba(0, 255, 115, 33)", "LA")
195
Image.new("LA", (1, 1), "white")
198
def test_color_hsv() -> None:
199
assert (170, 255, 255) == ImageColor.getcolor("hsv(240, 100%, 100%)", "HSV")
202
def test_color_too_long() -> None:
204
color_too_long = "hsl(" + "1" * 40 + "," + "1" * 40 + "%," + "1" * 40 + "%)"
207
with pytest.raises(ValueError):
208
ImageColor.getrgb(color_too_long)