@@ -38,19 +38,19 @@ def _is_hex(x: str) -> bool:
38
38
39
39
def _parse_hex (color_code : str ) -> TypeRGBAFloats :
40
40
return (
41
- _color_int_to_float (int (color_code [1 :3 ], 16 )),
42
- _color_int_to_float (int (color_code [3 :5 ], 16 )),
43
- _color_int_to_float (int (color_code [5 :7 ], 16 )),
41
+ _color_byte_to_normalized_float (int (color_code [1 :3 ], 16 )),
42
+ _color_byte_to_normalized_float (int (color_code [3 :5 ], 16 )),
43
+ _color_byte_to_normalized_float (int (color_code [5 :7 ], 16 )),
44
44
1.0 ,
45
45
)
46
46
47
47
48
- def _color_int_to_float (x : int ) -> float :
48
+ def _color_byte_to_normalized_float (x : Union [ int , float ] ) -> float :
49
49
"""Convert an integer between 0 and 255 to a float between 0. and 1.0"""
50
50
return x / 255.0
51
51
52
52
53
- def _color_float_to_int (x : float ) -> int :
53
+ def _color_normalized_float_to_byte_int (x : float ) -> int :
54
54
"""Convert a float between 0. and 1.0 to an integer between 0 and 255"""
55
55
return int (x * 255.9999 )
56
56
@@ -72,19 +72,19 @@ def _parse_color(x: Union[tuple, list, str]) -> TypeRGBAFloats:
72
72
73
73
def _parse_color_as_numerical_sequence (x : Union [tuple , list ]) -> TypeRGBAFloats :
74
74
"""Convert a color as a sequence of numbers to an RGBA tuple between 0 and 1."""
75
+ if not all (isinstance (value , (int , float )) for value in x ):
76
+ raise TypeError ("Components in color sequence should all be int or float." )
75
77
if not 3 <= len (x ) <= 4 :
76
78
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 )} ." )
79
+ conversion_function = float
80
+ if 1 < max (x ) <= 255 :
81
+ conversion_function = _color_byte_to_normalized_float
82
+ if min (x ) < 0 or max (x ) > 255 :
83
+ raise ValueError ("Color components should be between 0.0 and 1.0 or 0 and 255." )
84
+ color : List [float ] = [conversion_function (value ) for value in x ]
85
85
if len (color ) == 3 :
86
86
color .append (1.0 ) # add alpha channel
87
- return tuple ( color ) # type: ignore
87
+ return color [ 0 ], color [ 1 ], color [ 2 ], color [ 3 ]
88
88
89
89
90
90
def _base (x : float ) -> float :
@@ -175,7 +175,7 @@ def rgba_bytes_tuple(self, x: float) -> TypeRGBAInts:
175
175
"""Provides the color corresponding to value `x` in the
176
176
form of a tuple (R,G,B,A) with int values between 0 and 255.
177
177
"""
178
- return tuple (_color_float_to_int (u ) for u in self .rgba_floats_tuple (x )) # type: ignore
178
+ return tuple (_color_normalized_float_to_byte_int (u ) for u in self .rgba_floats_tuple (x )) # type: ignore
179
179
180
180
def rgb_bytes_tuple (self , x : float ) -> TypeRGBInts :
181
181
"""Provides the color corresponding to value `x` in the
0 commit comments