Skip to content

Commit fdc5cca

Browse files
committed
deal with case of int {0, 1}
1 parent 91e20b1 commit fdc5cca

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

branca/colormap.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def _color_float_to_int(x: float) -> int:
5656

5757

5858
def _parse_color(x: Union[tuple, list, str]) -> TypeRGBAFloats:
59-
"""Convert an unknown color value to an RGBA tuple between 0 and 1."""
59+
"""Convert an unknown color value to an RGBA tuple of floats between 0 and 1."""
6060
if isinstance(x, (tuple, list)):
6161
return _parse_color_as_numerical_sequence(x)
6262
elif isinstance(x, str) and _is_hex(x):
@@ -71,19 +71,28 @@ def _parse_color(x: Union[tuple, list, str]) -> TypeRGBAFloats:
7171

7272

7373
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."""
74+
"""Convert a color as a sequence of numbers to an RGBA tuple of floats between 0 and 1."""
7575
if not all(isinstance(value, (int, float)) for value in x):
7676
raise TypeError("Components in color sequence should all be int or float.")
7777
if not 3 <= len(x) <= 4:
78-
raise ValueError(f"Color sequence should have 3 or 4 elements, not {len(x)}.")
79-
conversion_function: Callable = float
80-
if 1 < max(x) <= 255:
81-
conversion_function = _color_int_to_float
78+
raise ValueError(f"Color sequence should have 3 or 4 components, not {len(x)}.")
8279
if min(x) < 0 or max(x) > 255:
8380
raise ValueError("Color components should be between 0.0 and 1.0 or 0 and 255.")
81+
82+
if all(isinstance(value, int) for value in x):
83+
# assume integers are a sequence of bytes that have to be normalized
84+
conversion_function = _color_int_to_float
85+
elif 1 < max(x) <= 255:
86+
# values between 1 and 255 are bytes no matter the type and should be normalized
87+
conversion_function = _color_int_to_float
88+
else:
89+
# else assume it's already normalized
90+
conversion_function = float
91+
8492
color: List[float] = [conversion_function(value) for value in x]
8593
if len(color) == 3:
8694
color.append(1.0) # add alpha channel
95+
8796
return color[0], color[1], color[2], color[3]
8897

8998

tests/test_colormap_parse.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,26 @@ def test_parse_color(input_data, expected):
3939
@pytest.mark.parametrize(
4040
"input_data, expected",
4141
[
42+
# these are byte values as ints and should be normalized and converted
4243
((0, 0, 0), (0.0, 0.0, 0.0, 1.0)),
4344
((255, 255, 255), (1.0, 1.0, 1.0, 1.0)),
4445
((255, 0, 0), (1.0, 0.0, 0.0, 1.0)),
46+
# a special case: ints that are 0 or 1 should be considered bytes
47+
((0, 0, 1), (0.0, 0.0, 1 / 255, 1.0)),
48+
((0, 0, 0, 1), (0.0, 0.0, 0.0, 1 / 255)),
49+
# these already are normalized floats
4550
((0.0, 0.0, 0.0), (0.0, 0.0, 0.0, 1.0)),
51+
((0.0, 0.0, 1.0), (0.0, 0.0, 1.0, 1.0)),
4652
((0.5, 0.5, 0.5), (0.5, 0.5, 0.5, 1.0)),
4753
((0.1, 0.2, 0.3, 0.4), (0.1, 0.2, 0.3, 0.4)),
4854
((0.0, 1.0, 0.0, 0.5), (0.0, 1.0, 0.0, 0.5)),
55+
# these are byte values as floats and should be normalized
4956
((0, 0, 0, 255.0), (0.0, 0.0, 0.0, 1.0)),
5057
((0, 0, 255.0, 0.0), (0.0, 0.0, 1.0, 0.0)),
58+
# if floats and ints are mixed, assume they are intended as floats
59+
((0, 0, 1.0), (0.0, 0.0, 1.0, 1.0)),
60+
# unless some of them are between 1 and 255
61+
((0, 0, 1.0, 128), (0.0, 0.0, 1 / 255, 128 / 255)),
5162
],
5263
)
5364
def test_parse_color_as_numerical_sequence(input_data, expected):

0 commit comments

Comments
 (0)