Skip to content

Commit 196a1ae

Browse files
committed
test: add unit tests for rgb/hex color conversion helpers
1 parent c27c986 commit 196a1ae

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import pytest
2+
3+
from _plotly_utils.colors import (
4+
find_intermediate_color,
5+
hex_to_rgb,
6+
label_rgb,
7+
unlabel_rgb,
8+
)
9+
10+
11+
def test_hex_to_rgb_basic_values():
12+
assert hex_to_rgb("#ffffff") == (255, 255, 255)
13+
assert hex_to_rgb("#000000") == (0, 0, 0)
14+
assert hex_to_rgb("#aabbcc") == (170, 187, 204)
15+
16+
17+
def test_label_rgb_formats_tuple():
18+
assert label_rgb((255, 0, 0)) == "rgb(255, 0, 0)"
19+
assert label_rgb((1, 2, 3)) == "rgb(1, 2, 3)"
20+
21+
22+
def test_unlabel_rgb_parses_string():
23+
assert unlabel_rgb("rgb(255, 0, 0)") == (255.0, 0.0, 0.0)
24+
assert unlabel_rgb("rgb(1, 2, 3)") == (1.0, 2.0, 3.0)
25+
26+
27+
def test_label_and_unlabel_are_inverses():
28+
assert unlabel_rgb(label_rgb((10, 20, 30))) == (10.0, 20.0, 30.0)
29+
30+
31+
def test_find_intermediate_color_tuple_midpoint():
32+
assert find_intermediate_color((0, 0, 0), (1, 1, 1), 0.5) == (0.5, 0.5, 0.5)
33+
34+
35+
def test_find_intermediate_color_endpoints():
36+
low, high = (0.0, 0.0, 0.0), (1.0, 1.0, 1.0)
37+
assert find_intermediate_color(low, high, 0.0) == low
38+
assert find_intermediate_color(low, high, 1.0) == high
39+
40+
41+
def test_find_intermediate_color_rgb_colortype():
42+
result = find_intermediate_color(
43+
"rgb(0, 0, 0)", "rgb(10, 20, 30)", 0.5, colortype="rgb"
44+
)
45+
assert result == "rgb(5.0, 10.0, 15.0)"
46+
47+
48+
if __name__ == "__main__":
49+
raise SystemExit(pytest.main([__file__, "-v"]))

0 commit comments

Comments
 (0)