|
1
|
|
|
"""Test color converter.""" |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
import sys |
|
5
|
|
|
|
|
6
|
|
|
from unittest import TestCase |
|
7
|
|
|
|
|
8
|
|
|
COLOR_CONVERTER = sys.modules["Rainmeter.color.converter"] |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
class IntToHexTest(TestCase): |
|
12
|
|
|
"""Testing int to hex conversion and its corner cases.""" |
|
13
|
|
|
|
|
14
|
|
|
def test_below_lower_boundary(self): |
|
15
|
|
|
"""Rainmeter only supports from 0 upwards.""" |
|
16
|
|
|
self.assertRaises(AssertionError, COLOR_CONVERTER.int_to_hex, -1) |
|
17
|
|
|
|
|
18
|
|
|
def test_lower_boundary(self): |
|
19
|
|
|
"""Zero is a corner case and should return 00.""" |
|
20
|
|
|
hex_value = COLOR_CONVERTER.int_to_hex(0) |
|
21
|
|
|
|
|
22
|
|
|
self.assertEqual(hex_value, "00") |
|
23
|
|
|
|
|
24
|
|
|
def test_default(self): |
|
25
|
|
|
"""A random number within the boundary 0, 255 should work.""" |
|
26
|
|
|
hex_value = COLOR_CONVERTER.int_to_hex(128) |
|
27
|
|
|
|
|
28
|
|
|
self.assertEqual(hex_value, "80") |
|
29
|
|
|
|
|
30
|
|
|
def test_upper_boundary(self): |
|
31
|
|
|
"""255 is a corner case and should return FF.""" |
|
32
|
|
|
hex_value = COLOR_CONVERTER.int_to_hex(255) |
|
33
|
|
|
|
|
34
|
|
|
self.assertEqual(hex_value, "FF") |
|
35
|
|
|
|
|
36
|
|
|
def test_over_upper_boundary(self): |
|
37
|
|
|
"""Rainmeter only supports up to 255.""" |
|
38
|
|
|
self.assertRaises(AssertionError, COLOR_CONVERTER.int_to_hex, 256) |
|
39
|
|
|
|
|
40
|
|
|
def test_letter_case(self): |
|
41
|
|
|
"""We also support lower case if it is requested.""" |
|
42
|
|
|
hex_value = COLOR_CONVERTER.int_to_hex(255, letter_case=COLOR_CONVERTER.LetterCase.Lower) |
|
43
|
|
|
|
|
44
|
|
|
self.assertEqual(hex_value, "ff") |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
View Code Duplication |
class RGBsToHexesTest(TestCase): |
|
|
|
|
|
|
48
|
|
|
"""Testing RGBs to hexes conversion and its corner cases.""" |
|
49
|
|
|
|
|
50
|
|
|
def test_default_rgb_conversion(self): |
|
51
|
|
|
"""3 valid ints should convert to 3 hexes.""" |
|
52
|
|
|
hexes = COLOR_CONVERTER.rgbs_to_hexes([128, 128, 128]) |
|
53
|
|
|
|
|
54
|
|
|
self.assertEqual(hexes, ["80", "80", "80"]) |
|
55
|
|
|
|
|
56
|
|
|
def test_default_rgba_conversion(self): |
|
57
|
|
|
"""4 valid ints should convert to 4 hexes.""" |
|
58
|
|
|
hexes = COLOR_CONVERTER.rgbs_to_hexes([128, 128, 128, 128]) |
|
59
|
|
|
|
|
60
|
|
|
self.assertEqual(hexes, ["80", "80", "80", "80"]) |
|
61
|
|
|
|
|
62
|
|
|
def test_invalid_rgb_low_len(self): |
|
63
|
|
|
"""RGB are at least 3 values.""" |
|
64
|
|
|
self.assertRaises(AssertionError, COLOR_CONVERTER.rgbs_to_hexes, [128, 128]) |
|
65
|
|
|
|
|
66
|
|
|
def test_invalid_rgb_high_len(self): |
|
67
|
|
|
"""RGB are at most 4 values.""" |
|
68
|
|
|
self.assertRaises(AssertionError, COLOR_CONVERTER.rgbs_to_hexes, [128, 128, 128, 128, 128]) |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
class HexesToStringTest(TestCase): |
|
72
|
|
|
"""This test guerentees that a proper string conversion .""" |
|
73
|
|
|
|
|
74
|
|
|
def test_stringing(self): |
|
75
|
|
|
"""Default case with one spacing.""" |
|
76
|
|
|
stringed = COLOR_CONVERTER.hexes_to_string(["80", "80", "80"]) |
|
77
|
|
|
|
|
78
|
|
|
self.assertEqual(stringed, "808080") |
|
79
|
|
|
|
|
80
|
|
|
def test_rgba(self): |
|
81
|
|
|
"""RGBA case.""" |
|
82
|
|
|
stringed = COLOR_CONVERTER.hexes_to_string(["80", "80", "80", "80"]) |
|
83
|
|
|
|
|
84
|
|
|
self.assertEqual(stringed, "80808080") |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
class HexToIntTest(TestCase): |
|
88
|
|
|
"""Testing hex to int conversion and its corner cases.""" |
|
89
|
|
|
|
|
90
|
|
|
def test_below_lower_boundary(self): |
|
91
|
|
|
"""Rainmeter only supports from 0 upwards.""" |
|
92
|
|
|
self.assertRaises(AssertionError, COLOR_CONVERTER.hex_to_int, "-1") |
|
93
|
|
|
|
|
94
|
|
|
def test_lower_boundary(self): |
|
95
|
|
|
"""00 is a corner case and should return 0.""" |
|
96
|
|
|
int_value = COLOR_CONVERTER.hex_to_int("00") |
|
97
|
|
|
|
|
98
|
|
|
self.assertEqual(int_value, 0) |
|
99
|
|
|
|
|
100
|
|
|
def test_default(self): |
|
101
|
|
|
"""A random number within the boundary 0, 255 should work.""" |
|
102
|
|
|
int_value = COLOR_CONVERTER.hex_to_int("80") |
|
103
|
|
|
|
|
104
|
|
|
self.assertEqual(int_value, 128) |
|
105
|
|
|
|
|
106
|
|
|
def test_upper_boundary(self): |
|
107
|
|
|
"""FF is a corner case and should return 255.""" |
|
108
|
|
|
int_value = COLOR_CONVERTER.hex_to_int("FF") |
|
109
|
|
|
|
|
110
|
|
|
self.assertEqual(int_value, 255) |
|
111
|
|
|
|
|
112
|
|
|
def test_over_upper_boundary(self): |
|
113
|
|
|
"""Rainmeter only supports up to 255.""" |
|
114
|
|
|
self.assertRaises(AssertionError, COLOR_CONVERTER.hex_to_int, "100") |
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
|
View Code Duplication |
class HexesToRGBsTest(TestCase): |
|
|
|
|
|
|
118
|
|
|
"""Testing Hexes to RGBs conversion and its corner cases.""" |
|
119
|
|
|
|
|
120
|
|
|
def test_default_hex_conversion(self): |
|
121
|
|
|
""".""" |
|
122
|
|
|
rgb = COLOR_CONVERTER.hexes_to_rgbs(["80", "80", "80"]) |
|
123
|
|
|
|
|
124
|
|
|
self.assertEqual(rgb, [128, 128, 128]) |
|
125
|
|
|
|
|
126
|
|
|
def test_default_hexa_conversion(self): |
|
127
|
|
|
"""4 valid hexes should convert to rgba.""" |
|
128
|
|
|
rgba = COLOR_CONVERTER.hexes_to_rgbs(["80", "80", "80", "80"]) |
|
129
|
|
|
|
|
130
|
|
|
self.assertEqual(rgba, [128, 128, 128, 128]) |
|
131
|
|
|
|
|
132
|
|
|
def test_invalid_hex_low_len(self): |
|
133
|
|
|
"""Require at least 3 values.""" |
|
134
|
|
|
self.assertRaises(AssertionError, COLOR_CONVERTER.hexes_to_rgbs, ["FF", "FF"]) |
|
135
|
|
|
|
|
136
|
|
|
def test_invalid_hex_high_len(self): |
|
137
|
|
|
"""Require at most 4 values.""" |
|
138
|
|
|
self.assertRaises( |
|
139
|
|
|
AssertionError, |
|
140
|
|
|
COLOR_CONVERTER.hexes_to_rgbs, |
|
141
|
|
|
["FF", "FF", "FF", "FF", "FF"] |
|
142
|
|
|
) |
|
143
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
class RGBsToStringTest(TestCase): |
|
146
|
|
|
"""This test guerentees that a proper string conversion .""" |
|
147
|
|
|
|
|
148
|
|
|
def test_stringing(self): |
|
149
|
|
|
"""Default Rainmeter decimal color representation.""" |
|
150
|
|
|
stringed = COLOR_CONVERTER.rgbs_to_string([128, 128, 128]) |
|
151
|
|
|
|
|
152
|
|
|
self.assertEqual(stringed, "128,128,128") |
|
153
|
|
|
|
|
154
|
|
|
def test_with_spacing(self): |
|
155
|
|
|
"""For people who like to space things.""" |
|
156
|
|
|
stringed = COLOR_CONVERTER.rgbs_to_string([128, 128, 128], spacing=1) |
|
157
|
|
|
|
|
158
|
|
|
self.assertEqual(stringed, "128, 128, 128") |
|
159
|
|
|
|
|
160
|
|
|
def test_with_more_spacing(self): |
|
161
|
|
|
"""For people who like to use a lot of spacings.""" |
|
162
|
|
|
stringed = COLOR_CONVERTER.rgbs_to_string([128, 128, 128], spacing=5) |
|
163
|
|
|
|
|
164
|
|
|
self.assertEqual(stringed, "128, 128, 128") |
|
165
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
class HexAppendAlphaTest(TestCase): |
|
168
|
|
|
"""This test handles different cases of appended alpha values.""" |
|
169
|
|
|
|
|
170
|
|
|
def test_lower_case(self): |
|
171
|
|
|
"""Lower case hex string adds a lower-case full alpha channel.""" |
|
172
|
|
|
stringed = COLOR_CONVERTER.convert_hex_to_hex_with_alpha("ff8800") |
|
173
|
|
|
|
|
174
|
|
|
self.assertEqual(stringed, "ff8800ff") |
|
175
|
|
|
|
|
176
|
|
|
def test_upper_case(self): |
|
177
|
|
|
"""Upper case hex string adds upper-case full alpha channel.""" |
|
178
|
|
|
stringed = COLOR_CONVERTER.convert_hex_to_hex_with_alpha("FF8800") |
|
179
|
|
|
|
|
180
|
|
|
self.assertEqual(stringed, "FF8800FF") |
|
181
|
|
|
|
|
182
|
|
|
def test_mixed_case(self): |
|
183
|
|
|
"""If case is not clear add upper-case full alpha channel.""" |
|
184
|
|
|
stringed = COLOR_CONVERTER.convert_hex_to_hex_with_alpha("Ff8800") |
|
185
|
|
|
|
|
186
|
|
|
self.assertEqual(stringed, "Ff8800FF") |
|
187
|
|
|
|
|
188
|
|
|
def test_already_alpha(self): |
|
189
|
|
|
"""Only add alpha channel if have only RGB.""" |
|
190
|
|
|
stringed = COLOR_CONVERTER.convert_hex_to_hex_with_alpha("FF8800FF") |
|
191
|
|
|
|
|
192
|
|
|
self.assertEqual(stringed, "FF8800FF") |
|
193
|
|
|
|
|
194
|
|
|
|
|
195
|
|
|
class HexToRGBAStringTest(TestCase): |
|
196
|
|
|
"""Test behaviour of hex string to rgba string converter.""" |
|
197
|
|
|
|
|
198
|
|
|
def test_without_alpha(self): |
|
199
|
|
|
""" |
|
200
|
|
|
We always get a RGBA String, but it depends if previously it had an alpha channel or not. |
|
201
|
|
|
|
|
202
|
|
|
In case it had no alpha channel then we ignore the alpha channel |
|
203
|
|
|
unless we have a value which differs from FF or 255. |
|
204
|
|
|
The color picker will default to FF if a non alpha channel color is inputted. |
|
205
|
|
|
""" |
|
206
|
|
|
stringed = COLOR_CONVERTER.convert_hex_str_to_rgba_str("FFFFFFFF", False) |
|
207
|
|
|
|
|
208
|
|
|
self.assertEqual(stringed, "255,255,255") |
|
209
|
|
|
|
|
210
|
|
|
def test_with_alpha(self): |
|
211
|
|
|
""" |
|
212
|
|
|
If there was already an alpha channel there we have to respect that. |
|
213
|
|
|
|
|
214
|
|
|
That way even FF is written back. |
|
215
|
|
|
""" |
|
216
|
|
|
stringed = COLOR_CONVERTER.convert_hex_str_to_rgba_str("FFFFFFFF", True) |
|
217
|
|
|
|
|
218
|
|
|
self.assertEqual(stringed, "255,255,255,255") |
|
219
|
|
|
|
|
220
|
|
|
def test_without_alpha_but_non_max(self): |
|
221
|
|
|
""" |
|
222
|
|
|
If we had no alpha channel but we get a value back which is different from FF. |
|
223
|
|
|
|
|
224
|
|
|
In that case we have to translate that information too |
|
225
|
|
|
and force add the alpha channel to the content. |
|
226
|
|
|
""" |
|
227
|
|
|
stringed = COLOR_CONVERTER.convert_hex_str_to_rgba_str("FFFFFF01", False) |
|
228
|
|
|
|
|
229
|
|
|
self.assertEqual(stringed, "255,255,255,1") |
|
230
|
|
|
|