Pillow

Форк
0
/
test_imagecolor.py 
208 строк · 7.9 Кб
1
from __future__ import annotations
2

3
import pytest
4

5
from PIL import Image, ImageColor
6

7

8
def test_hash() -> None:
9
    # short 3 components
10
    assert (255, 0, 0) == ImageColor.getrgb("#f00")
11
    assert (0, 255, 0) == ImageColor.getrgb("#0f0")
12
    assert (0, 0, 255) == ImageColor.getrgb("#00f")
13

14
    # short 4 components
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")
19

20
    # long 3 components
21
    assert (222, 0, 0) == ImageColor.getrgb("#de0000")
22
    assert (0, 222, 0) == ImageColor.getrgb("#00de00")
23
    assert (0, 0, 222) == ImageColor.getrgb("#0000de")
24

25
    # long 4 components
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")
30

31
    # case insensitivity
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")
36

37
    # not a number
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")
46

47
    # wrong number of components
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 ")
58

59

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")
64

65
    with pytest.raises(ValueError):
66
        ImageColor.getrgb("black ")
67

68

69
def test_functions() -> None:
70
    # rgb numbers
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)")
74

75
    # percents
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%)")
79

80
    # rgba numbers
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)")
85

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%)")
89

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%)")
93

94
    # alternate format
95
    assert ImageColor.getrgb("hsb(0,100%,50%)") == ImageColor.getrgb("hsv(0,100%,50%)")
96

97
    # floats
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%)")
100

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%)")
103

104
    # case insensitivity
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%)")
111

112
    # space agnosticism
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%  )")
118

119
    # wrong number of components
120
    with pytest.raises(ValueError):
121
        ImageColor.getrgb("rgb(255,0)")
122
    with pytest.raises(ValueError):
123
        ImageColor.getrgb("rgb(255,0,0,0)")
124

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%)")
133

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)")
138

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)")
149

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)")
160

161

162
# look for rounding errors (based on code by Tim Hatch)
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
168

169
    assert (0, 255, 115) == ImageColor.getcolor("rgba(0, 255, 115, 33)", "RGB")
170
    Image.new("RGB", (1, 1), "white")
171

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")
176

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")
181

182
    assert 0 == ImageColor.getcolor("black", "1")
183
    assert 255 == ImageColor.getcolor("white", "1")
184
    # The following test is wrong, but is current behavior
185
    # The correct result should be 255 due to the mode 1
186
    assert 163 == ImageColor.getcolor("rgba(0, 255, 115, 33)", "1")
187
    # Correct behavior
188
    # assert
189
    #     255, ImageColor.getcolor("rgba(0, 255, 115, 33)", "1"))
190
    Image.new("1", (1, 1), "white")
191

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")
196

197

198
def test_color_hsv() -> None:
199
    assert (170, 255, 255) == ImageColor.getcolor("hsv(240, 100%, 100%)", "HSV")
200

201

202
def test_color_too_long() -> None:
203
    # Arrange
204
    color_too_long = "hsl(" + "1" * 40 + "," + "1" * 40 + "%," + "1" * 40 + "%)"
205

206
    # Act / Assert
207
    with pytest.raises(ValueError):
208
        ImageColor.getrgb(color_too_long)
209

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.