@@ -234,9 +234,35 @@ def is_color_like(c):
234
234
235
235
236
236
def _has_alpha_channel (c ):
237
- """Return whether *c* is a color with an alpha channel."""
238
- # 4-element sequences are interpreted as r, g, b, a
239
- return not isinstance (c , str ) and len (c ) == 4
237
+ """
238
+ Return whether *c* is a color with an alpha channel.
239
+
240
+ If *c* is not a valid color specifier, then the result is undefined.
241
+ """
242
+ # The following logic uses the assumption that c is a valid color spec.
243
+ # For speed and simplicity, we intentionally don't care about other inputs.
244
+ # Anything can happen with them.
245
+
246
+ # if c is a hex, it has an alpha channel when it has 4 (or 8) digits after '#'
247
+ if isinstance (c , str ):
248
+ if c [0 ] == '#' and (len (c ) == 5 or len (c ) == 9 ):
249
+ # example: '#fff8' or '#0f0f0f80'
250
+ return True
251
+ else :
252
+ # if c isn't a string, it can be an RGB(A) or a color-alpha tuple
253
+ # if it has length 4, it has an alpha channel
254
+ if len (c ) == 4 :
255
+ # example: [0.5, 0.5, 0.5, 0.5]
256
+ return True
257
+
258
+ # if it has length 2, it's a color/alpha tuple
259
+ # if the second element isn't None or the first element has length = 4
260
+ if len (c ) == 2 and (c [1 ] is not None or _has_alpha_channel (c [0 ])):
261
+ # example: ([0.5, 0.5, 0.5, 0.5], None) or ('r', 0.5)
262
+ return True
263
+
264
+ # otherwise it doesn't have an alpha channel
265
+ return False
240
266
241
267
242
268
def _check_color_like (** kwargs ):
0 commit comments