|
1 | 1 | import functools
|
2 | 2 | import json
|
3 | 3 | import pprint
|
| 4 | +from colorsys import hls_to_rgb |
4 | 5 | from pathlib import Path
|
5 | 6 |
|
6 | 7 | import pytest
|
@@ -164,25 +165,83 @@ def test_color_common_parse3(input):
|
164 | 165 |
|
165 | 166 | @json_test(filename='color.json')
|
166 | 167 | def test_color_common_parse4(input):
|
167 |
| - result = parse_color4(input) |
168 |
| - return RGBA(*result) if (result and result != 'currentColor') else result |
| 168 | + color = parse_color4(input) |
| 169 | + if not color or color == 'currentColor': |
| 170 | + return color |
| 171 | + elif color.space == 'srgb': |
| 172 | + return RGBA(*color) |
| 173 | + elif color.space == 'hsl': |
| 174 | + rgb = hls_to_rgb(color[0] / 360, color[2] / 100, color[1] / 100) |
| 175 | + return RGBA(*rgb, color.alpha) |
169 | 176 |
|
170 | 177 |
|
171 | 178 | @json_test()
|
172 | 179 | def test_color3(input):
|
173 | 180 | return parse_color3(input)
|
174 | 181 |
|
175 | 182 |
|
| 183 | +@json_test(filename='color_hsl.json') |
| 184 | +def test_color3_hsl(input): |
| 185 | + return parse_color3(input) |
| 186 | + |
| 187 | + |
| 188 | +@json_test(filename='color_hsl.json') |
| 189 | +def test_color4_hsl(input): |
| 190 | + color = parse_color4(input) |
| 191 | + assert color.space == 'hsl' |
| 192 | + rgb = hls_to_rgb(color[0] / 360, color[2] / 100, color[1] / 100) |
| 193 | + return RGBA(*rgb, color.alpha) if (color and color != 'currentColor') else color |
| 194 | + |
| 195 | + |
176 | 196 | @json_test()
|
177 |
| -def test_color4(input): |
178 |
| - return parse_color4(input) |
| 197 | +def test_color4_hwb(input): |
| 198 | + color = parse_color4(input) |
| 199 | + assert color.space == 'hwb' |
| 200 | + white, black = color[1:3] |
| 201 | + if white + black >= 100: |
| 202 | + rgb = (255 * white / (white + black),) * 3 |
| 203 | + else: |
| 204 | + rgb = hls_to_rgb(color[0] / 360, 0.5, 1) |
| 205 | + rgb = (2.55 * ((channel * (100 - white - black)) + white) for channel in rgb) |
| 206 | + rgb = (round(coordinate + 0.001) for coordinate in rgb) |
| 207 | + coordinates = ', '.join( |
| 208 | + str(int(coordinate) if coordinate.is_integer() else coordinate) |
| 209 | + for coordinate in rgb) |
| 210 | + if color.alpha == 0: |
| 211 | + return f'rgba({coordinates}, 0)' |
| 212 | + elif color.alpha == 1: |
| 213 | + return f'rgb({coordinates})' |
| 214 | + else: |
| 215 | + return f'rgba({coordinates}, {color.alpha})' |
| 216 | + return RGBA(*rgb, color.alpha) if (color and color != 'currentColor') else color |
179 | 217 |
|
180 | 218 |
|
181 |
| -# Do not use @json_test because parametrize is slow with that many values. |
182 |
| -@pytest.mark.parametrize(('parse_color'), (parse_color3, parse_color4)) |
183 |
| -def test_color_hsl(parse_color): |
184 |
| - for css, expected in load_json('color_hsl.json'): |
185 |
| - assert to_json(RGBA(*parse_color(css))) == expected |
| 219 | +@json_test() |
| 220 | +def test_color4_color_function(input): |
| 221 | + color = parse_color4(input) |
| 222 | + coordinates = ' '.join( |
| 223 | + str(int(coordinate) if coordinate.is_integer() else round(coordinate, 3)) |
| 224 | + for coordinate in color.coordinates) |
| 225 | + if color.alpha == 0: |
| 226 | + return f'color({color.space} {coordinates} / 0)' |
| 227 | + elif color.alpha == 1: |
| 228 | + return f'color({color.space} {coordinates})' |
| 229 | + else: |
| 230 | + return f'color({color.space} {coordinates} / {color.alpha})' |
| 231 | + |
| 232 | + |
| 233 | +@json_test() |
| 234 | +def test_color4_lab_lch_oklab_oklch(input): |
| 235 | + color = parse_color4(input) |
| 236 | + coordinates = ' '.join( |
| 237 | + str(int(coordinate) if coordinate.is_integer() else round(coordinate, 3)) |
| 238 | + for coordinate in color.coordinates) |
| 239 | + if color.alpha == 0: |
| 240 | + return f'{color.space}({coordinates} / 0)' |
| 241 | + elif color.alpha == 1: |
| 242 | + return f'{color.space}({coordinates})' |
| 243 | + else: |
| 244 | + return f'{color.space}({coordinates} / {color.alpha})' |
186 | 245 |
|
187 | 246 |
|
188 | 247 | @pytest.mark.parametrize(('filename', 'parse_color'), (
|
|
0 commit comments