Skip to content

Commit 5124975

Browse files
committed
Add support of device-cmyk() function
1 parent bfe945e commit 5124975

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

tinycss2/color4.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ class Color:
2323
to [0, 1]. Coordinates can also be set to ``None`` when undefined.
2424
2525
"""
26+
COLOR_SPACES = COLOR_SPACES
27+
2628
def __init__(self, space, coordinates, alpha):
27-
assert space in COLOR_SPACES, f"{space} is not a supported color space"
29+
assert space in self.COLOR_SPACES, f"{space} is not a supported color space"
2830
self.space = space
2931
self.coordinates = tuple(
3032
None if coordinate is None else float(coordinate)

tinycss2/color5.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from . import color4
2+
3+
COLOR_SPACES = color4.COLOR_SPACES | {'device-cmyk'}
4+
D50 = color4.D50
5+
D65 = color4.D65
6+
7+
8+
class Color(color4.Color):
9+
COLOR_SPACES = COLOR_SPACES
10+
11+
12+
def parse_color(input):
13+
color = color4.parse_color(input)
14+
15+
if color:
16+
return color
17+
18+
if isinstance(input, str):
19+
token = parse_one_component_value(input, skip_comments=True)
20+
else:
21+
token = input
22+
23+
if token.type == 'function':
24+
tokens = [
25+
token for token in token.arguments
26+
if token.type not in ('whitespace', 'comment')]
27+
name = token.lower_name
28+
length = len(tokens)
29+
30+
if length in (7, 9) and all(token == ',' for token in tokens[1::2]):
31+
old_syntax = True
32+
tokens = tokens[::2]
33+
elif length == 4:
34+
old_syntax = False
35+
elif length == 6 and tokens[4] == '/':
36+
tokens.pop(4)
37+
old_syntax = False
38+
else:
39+
return
40+
args, alpha = tokens[:4], color4._parse_alpha(tokens[4:])
41+
42+
if name == 'device-cmyk':
43+
return _parse_device_cmyk(args, alpha, old_syntax)
44+
45+
46+
def _parse_device_cmyk(args, alpha, old_syntax):
47+
"""Parse a list of CMYK channels.
48+
49+
If args is a list of 4 NUMBER or PERCENTAGE tokens, return
50+
device-cmyk :class:`Color`. Otherwise, return None.
51+
52+
Input C, M, Y, K ranges are [0, 1], output are [0, 1].
53+
54+
"""
55+
if old_syntax:
56+
if color4._types(args) != {'number'}:
57+
return
58+
else:
59+
if color4._types(args) <= {'numbers', 'percentage'}:
60+
return
61+
cmyk = [
62+
arg.value if arg.type == 'number' else
63+
arg.value / 100 if arg.type == 'percentage' else None
64+
for arg in args]
65+
return Color('device-cmyk', cmyk, alpha)

0 commit comments

Comments
 (0)