@@ -24,10 +24,8 @@ def __init__(self, space, params, alpha=1):
24
24
self .alpha = float (alpha )
25
25
26
26
def __repr__ (self ):
27
- return (
28
- f'color({ self .space } '
29
- f'{ " " .join (str (param ) for param in self .params )} '
30
- f'/ { self .alpha } )' )
27
+ params = ' ' .join (str (param ) for param in self .params )
28
+ return f'color({ self .space } { params } / { self .alpha } )'
31
29
32
30
def __iter__ (self ):
33
31
yield from self .params
@@ -84,7 +82,7 @@ def parse_color(input):
84
82
int (group * multiplier , 16 ) / 255
85
83
for group in match .groups ()]
86
84
if len (channels ) == 3 :
87
- channels .append (1. )
85
+ channels .append (1 )
88
86
return srgb (* channels )
89
87
elif token .type == 'function' :
90
88
tokens = [
@@ -121,21 +119,6 @@ def parse_color(input):
121
119
return _parse_oklch (args , alpha )
122
120
123
121
124
- def _parse_separated_args (tokens ):
125
- """Parse a list of tokens given to a color function.
126
-
127
- According to CSS Color Level 4, arguments must be made of a single token
128
- each, either comma seperated or space-seperated with an optional
129
- slash-seperated opacity.
130
-
131
- Return a tuple containing:
132
- * the argument list without commas or white spaces, or None if the
133
- function token content do not match the description above; and
134
- * a boolean telling if a comma was found in the function parameters.
135
-
136
- """
137
-
138
-
139
122
def _parse_alpha (args ):
140
123
"""Parse a list of one alpha value.
141
124
@@ -175,7 +158,6 @@ def _parse_hsl(args, alpha):
175
158
"""
176
159
if (args [1 ].type , args [2 ].type ) != ('percentage' , 'percentage' ):
177
160
return
178
-
179
161
hue = _parse_hue (args [0 ])
180
162
if hue is None :
181
163
return
@@ -192,11 +174,9 @@ def _parse_hwb(args, alpha):
192
174
"""
193
175
if (args [1 ].type , args [2 ].type ) != ('percentage' , 'percentage' ):
194
176
return
195
-
196
177
hue = _parse_hue (args [0 ])
197
178
if hue is None :
198
179
return
199
-
200
180
white , black = (arg .value / 100 for arg in args [1 :])
201
181
if white + black >= 1 :
202
182
gray = white / (white + black )
@@ -278,9 +258,8 @@ def _parse_oklch(args, alpha):
278
258
token, return xyz-d65 :class:`Color`. Otherwise, return None.
279
259
280
260
"""
281
- if len (args ) != 3 :
282
- return
283
- if {args [0 ].type , args [1 ].type } > {'number' , 'percentage' }:
261
+ if len (args ) != 3 or (
262
+ {args [0 ].type , args [1 ].type } > {'number' , 'percentage' }):
284
263
return
285
264
L = args [0 ].value
286
265
C = args [1 ].value * (1 if args [1 ].type == 'number' else 1.5 )
@@ -296,11 +275,9 @@ def _oklab_to_xyz(L, a, b):
296
275
# Code from https://www.w3.org/TR/css-color-4/#color-conversion-code
297
276
lab = (L / 100 , a , b )
298
277
lms = [
299
- sum (_OKLAB_TO_LMS [i ][j ] * lab [j ] for j in range (3 ))
300
- for i in range (3 )]
278
+ sum (_OKLAB_TO_LMS [i ][j ] * lab [j ] for j in range (3 )) for i in range (3 )]
301
279
X , Y , Z = [
302
- sum (_LMS_TO_XYZ [i ][j ] * lms [j ] ** 3 for j in range (3 ))
303
- for i in range (3 )]
280
+ sum (_LMS_TO_XYZ [i ][j ] * lms [j ]** 3 for j in range (3 )) for i in range (3 )]
304
281
return X , Y , Z
305
282
306
283
0 commit comments