Skip to content

Commit a3be4b4

Browse files
committed
Add support for light-dark()
1 parent e533ccb commit a3be4b4

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

tinycss2/color5.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from . import color4
22

33
COLOR_SPACES = color4.COLOR_SPACES | {'device-cmyk'}
4+
COLOR_SCHEMES = {'light', 'dark'}
45
D50 = color4.D50
56
D65 = color4.D65
67

@@ -9,12 +10,21 @@ class Color(color4.Color):
910
COLOR_SPACES = None
1011

1112

12-
def parse_color(input):
13+
def parse_color(input, color_schemes=None):
1314
color = color4.parse_color(input)
1415

1516
if color:
1617
return color
1718

19+
if color_schemes is None or color_schemes == 'normal':
20+
color_scheme = 'light'
21+
else:
22+
for color_scheme in color_schemes:
23+
if color_scheme in COLOR_SCHEMES:
24+
break
25+
else:
26+
color_scheme = 'light'
27+
1828
if isinstance(input, str):
1929
token = color4.parse_one_component_value(input, skip_comments=True)
2030
else:
@@ -45,6 +55,8 @@ def parse_color(input):
4555

4656
if name == 'device-cmyk':
4757
return _parse_device_cmyk(args, alpha, old_syntax)
58+
elif name == 'light-dark':
59+
return _parse_light_dark(args, color_scheme)
4860
elif name == 'color' and not old_syntax:
4961
return _parse_color(space, args, alpha)
5062

@@ -71,6 +83,19 @@ def _parse_device_cmyk(args, alpha, old_syntax):
7183
return Color('device-cmyk', cmyk, alpha)
7284

7385

86+
def _parse_light_dark(args, color_scheme):
87+
colors = []
88+
for arg in args:
89+
if color := parse_color(arg, color_scheme):
90+
colors.append(color)
91+
if len(colors) == 2:
92+
if color_scheme == 'light':
93+
return colors[0]
94+
else:
95+
return colors[1]
96+
return
97+
98+
7499
def _parse_color(space, args, alpha):
75100
"""Parse a color space name list of coordinates.
76101

0 commit comments

Comments
 (0)