Skip to content

Commit

Permalink
Handle CIELab and OKLab colors
Browse files Browse the repository at this point in the history
  • Loading branch information
liZe committed Dec 18, 2023
1 parent d4f5d50 commit 8774356
Show file tree
Hide file tree
Showing 2 changed files with 262 additions and 91 deletions.
55 changes: 32 additions & 23 deletions tests/test_tinycss2.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
WhitespaceToken)
from tinycss2.color3 import RGBA
from tinycss2.color3 import parse_color as parse_color3
from tinycss2.color4 import Color
from tinycss2.color4 import parse_color as parse_color4
from tinycss2.nth import parse_nth
from webencodings import Encoding, lookup
Expand Down Expand Up @@ -58,7 +59,7 @@ def numeric(t):
NumberToken: lambda t: ['number'] + numeric(t),
PercentageToken: lambda t: ['percentage'] + numeric(t),
DimensionToken: lambda t: ['dimension'] + numeric(t) + [t.unit],
UnicodeRangeToken: lambda t: ['unicode-range', t.start, t.end],
UnicodeRangeToken: lambda t: ['urange', t.start, t.end],

CurlyBracketsBlock: lambda t: ['{}'] + to_json(t.content),
SquareBracketsBlock: lambda t: ['[]'] + to_json(t.content),
Expand All @@ -73,6 +74,7 @@ def numeric(t):
to_json(r.content)],

RGBA: lambda v: [round(c, 10) for c in v],
Color: lambda v: [round(c, 10) for c in v],
}


Expand Down Expand Up @@ -139,40 +141,47 @@ def test_nth(input):
return parse_nth(input)


@json_test()
def test_color3(input):
@json_test(filename='color.json')
def test_color_parse3(input):
return parse_color3(input)


# Do not use @pytest.mark.parametrize because it is slow with that many values.
def test_color3_hsl():
for css, expected in load_json('color3_hsl.json'):
assert to_json(parse_color3(css)) == expected
@json_test(filename='color.json')
def test_color_common_parse3(input):
return parse_color3(input)


def test_color3_keywords():
for css, expected in load_json('color3_keywords.json'):
result = parse_color3(css)
if result is not None:
r, g, b, a = result
result = [r * 255, g * 255, b * 255, a]
assert result == expected
@json_test(filename='color.json')
def test_color_common_parse4(input):
return parse_color4(input)


@json_test(filename='color3.json')
def test_color4_compatibility(input):
@json_test()
def test_color3(input):
return parse_color3(input)


@json_test()
def test_color4(input):
return parse_color4(input)


# Do not use @pytest.mark.parametrize because it is slow with that many values.
def test_color4_hsl_compatibility():
for css, expected in load_json('color3_hsl.json'):
assert to_json(parse_color4(css)) == expected
# Do not use @json_test because parametrize is slow with that many values.
@pytest.mark.parametrize(('parse_color'), (parse_color3, parse_color4))
def test_color_hsl(parse_color):
for css, expected in load_json('color_hsl.json'):
assert to_json(parse_color(css)) == expected


def test_color4_keywords_compatibility():
for css, expected in load_json('color3_keywords.json'):
result = parse_color4(css)
@pytest.mark.parametrize(('filename', 'parse_color'), (
('color_keywords.json', parse_color3),
('color_keywords.json', parse_color4),
('color3_keywords.json', parse_color3),
('color4_keywords.json', parse_color4),
))
def test_color_keywords(filename, parse_color):
for css, expected in load_json(filename):
result = parse_color(css)
if result is not None:
r, g, b, a = result
result = [r * 255, g * 255, b * 255, a]
Expand Down
Loading

0 comments on commit 8774356

Please sign in to comment.