@@ -56,8 +56,9 @@ def _color_float_to_int(x: float) -> int:
56
56
57
57
58
58
def _parse_color (x : Union [tuple , list , str ]) -> TypeRGBAFloats :
59
+ """Convert an unknown color value to an RGBA tuple between 0 and 1."""
59
60
if isinstance (x , (tuple , list )):
60
- return tuple ( tuple ( x ) + ( 1.0 ,))[: 4 ] # type: ignore
61
+ return _parse_color_as_numerical_sequence ( x )
61
62
elif isinstance (x , str ) and _is_hex (x ):
62
63
return _parse_hex (x )
63
64
elif isinstance (x , str ):
@@ -69,6 +70,23 @@ def _parse_color(x: Union[tuple, list, str]) -> TypeRGBAFloats:
69
70
raise ValueError (f"Unrecognized color code { x !r} " )
70
71
71
72
73
+ def _parse_color_as_numerical_sequence (x : Union [tuple , list ]) -> TypeRGBAFloats :
74
+ """Convert a color as a sequence of numbers to an RGBA tuple between 0 and 1."""
75
+ if not 3 <= len (x ) <= 4 :
76
+ raise ValueError (f"Color sequence should have 3 or 4 elements, not { len (x )} ." )
77
+ color : List [float ] = []
78
+ for value in x :
79
+ if isinstance (value , int ):
80
+ color .append (_color_int_to_float (value ))
81
+ elif isinstance (value , float ):
82
+ color .append (value )
83
+ else :
84
+ raise TypeError (f"Unexpected type in color sequence: { type (value )} ." )
85
+ if len (color ) == 3 :
86
+ color .append (1.0 ) # add alpha channel
87
+ return tuple (color ) # type: ignore
88
+
89
+
72
90
def _base (x : float ) -> float :
73
91
if x > 0 :
74
92
base = pow (10 , math .floor (math .log10 (x )))
0 commit comments