Skip to content

Commit 707e0e6

Browse files
authored
Merge pull request #5659 from eeshsaxena/test/colors-helpers
test: add unit tests for rgb/hex color conversion helpers
2 parents 801beb4 + 330959e commit 707e0e6

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

tests/test_plotly_utils/colors/__init__.py

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

0 commit comments

Comments
 (0)