Skip to content

Commit d4f5737

Browse files
committed
improve
1 parent 5239520 commit d4f5737

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

branca/colormap.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ def _is_hex(x: str) -> bool:
3838

3939
def _parse_hex(color_code: str) -> TypeRGBAFloats:
4040
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)),
4444
1.0,
4545
)
4646

4747

48-
def _color_int_to_float(x: int) -> float:
48+
def _color_byte_to_normalized_float(x: Union[int, float]) -> float:
4949
"""Convert an integer between 0 and 255 to a float between 0. and 1.0"""
5050
return x / 255.0
5151

5252

53-
def _color_float_to_int(x: float) -> int:
53+
def _color_normalized_float_to_byte_int(x: float) -> int:
5454
"""Convert a float between 0. and 1.0 to an integer between 0 and 255"""
5555
return int(x * 255.9999)
5656

@@ -72,19 +72,19 @@ def _parse_color(x: Union[tuple, list, str]) -> TypeRGBAFloats:
7272

7373
def _parse_color_as_numerical_sequence(x: Union[tuple, list]) -> TypeRGBAFloats:
7474
"""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.")
7577
if not 3 <= len(x) <= 4:
7678
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]
8585
if len(color) == 3:
8686
color.append(1.0) # add alpha channel
87-
return tuple(color) # type: ignore
87+
return color[0], color[1], color[2], color[3]
8888

8989

9090
def _base(x: float) -> float:
@@ -175,7 +175,7 @@ def rgba_bytes_tuple(self, x: float) -> TypeRGBAInts:
175175
"""Provides the color corresponding to value `x` in the
176176
form of a tuple (R,G,B,A) with int values between 0 and 255.
177177
"""
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
179179

180180
def rgb_bytes_tuple(self, x: float) -> TypeRGBInts:
181181
"""Provides the color corresponding to value `x` in the

0 commit comments

Comments
 (0)