@@ -372,40 +372,31 @@ def _to_rgba_no_colorcycle(c, alpha=None):
372
372
# This may turn c into a non-string, so we check again below.
373
373
c = _colors_full_map [c ]
374
374
except KeyError :
375
- if len (orig_c ) != 1 :
375
+ if len (c ) != 1 :
376
376
try :
377
377
c = _colors_full_map [c .lower ()]
378
378
except KeyError :
379
379
pass
380
380
if isinstance (c , str ):
381
- # hex color in #rrggbb format.
382
- match = re .match (r"\A#[a-fA-F0-9]{6}\Z" , c )
383
- if match :
384
- return (tuple (int (n , 16 ) / 255
385
- for n in [c [1 :3 ], c [3 :5 ], c [5 :7 ]])
386
- + (alpha if alpha is not None else 1. ,))
387
- # hex color in #rgb format, shorthand for #rrggbb.
388
- match = re .match (r"\A#[a-fA-F0-9]{3}\Z" , c )
389
- if match :
390
- return (tuple (int (n , 16 ) / 255
391
- for n in [c [1 ]* 2 , c [2 ]* 2 , c [3 ]* 2 ])
392
- + (alpha if alpha is not None else 1. ,))
393
- # hex color with alpha in #rrggbbaa format.
394
- match = re .match (r"\A#[a-fA-F0-9]{8}\Z" , c )
395
- if match :
396
- color = [int (n , 16 ) / 255
397
- for n in [c [1 :3 ], c [3 :5 ], c [5 :7 ], c [7 :9 ]]]
398
- if alpha is not None :
399
- color [- 1 ] = alpha
400
- return tuple (color )
401
- # hex color with alpha in #rgba format, shorthand for #rrggbbaa.
402
- match = re .match (r"\A#[a-fA-F0-9]{4}\Z" , c )
403
- if match :
404
- color = [int (n , 16 ) / 255
405
- for n in [c [1 ]* 2 , c [2 ]* 2 , c [3 ]* 2 , c [4 ]* 2 ]]
406
- if alpha is not None :
407
- color [- 1 ] = alpha
408
- return tuple (color )
381
+ if re .fullmatch ("#[a-fA-F0-9]+" , c ):
382
+ if len (c ) == 7 : # #rrggbb hex format.
383
+ return (* [n / 0xff for n in bytes .fromhex (c [1 :])],
384
+ alpha if alpha is not None else 1. )
385
+ elif len (c ) == 4 : # #rgb hex format, shorthand for #rrggbb.
386
+ return (* [int (n , 16 ) / 0xf for n in c [1 :]],
387
+ alpha if alpha is not None else 1. )
388
+ elif len (c ) == 9 : # #rrggbbaa hex format.
389
+ color = [n / 0xff for n in bytes .fromhex (c [1 :])]
390
+ if alpha is not None :
391
+ color [- 1 ] = alpha
392
+ return tuple (color )
393
+ elif len (c ) == 5 : # #rgba hex format, shorthand for #rrggbbaa.
394
+ color = [int (n , 16 ) / 0xf for n in c [1 :]]
395
+ if alpha is not None :
396
+ color [- 1 ] = alpha
397
+ return tuple (color )
398
+ else :
399
+ raise ValueError (f"Invalid hex color specifier: { orig_c !r} " )
409
400
# string gray.
410
401
try :
411
402
c = float (c )
0 commit comments