Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions tests/test_core/test_colors/test_color_conversions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import pytest

from _plotly_utils.colors import (
find_intermediate_color,
hex_to_rgb,
label_rgb,
unlabel_rgb,
)


def test_hex_to_rgb_basic_values():
assert hex_to_rgb("#ffffff") == (255, 255, 255)
assert hex_to_rgb("#000000") == (0, 0, 0)
assert hex_to_rgb("#aabbcc") == (170, 187, 204)


def test_label_rgb_formats_tuple():
assert label_rgb((255, 0, 0)) == "rgb(255, 0, 0)"
assert label_rgb((1, 2, 3)) == "rgb(1, 2, 3)"


def test_unlabel_rgb_parses_string():
assert unlabel_rgb("rgb(255, 0, 0)") == (255.0, 0.0, 0.0)
assert unlabel_rgb("rgb(1, 2, 3)") == (1.0, 2.0, 3.0)


def test_label_and_unlabel_are_inverses():
assert unlabel_rgb(label_rgb((10, 20, 30))) == (10.0, 20.0, 30.0)


def test_find_intermediate_color_tuple_midpoint():
assert find_intermediate_color((0, 0, 0), (1, 1, 1), 0.5) == (0.5, 0.5, 0.5)


def test_find_intermediate_color_endpoints():
low, high = (0.0, 0.0, 0.0), (1.0, 1.0, 1.0)
assert find_intermediate_color(low, high, 0.0) == low
assert find_intermediate_color(low, high, 1.0) == high


def test_find_intermediate_color_rgb_colortype():
result = find_intermediate_color(
"rgb(0, 0, 0)", "rgb(10, 20, 30)", 0.5, colortype="rgb"
)
assert result == "rgb(5.0, 10.0, 15.0)"


if __name__ == "__main__":
raise SystemExit(pytest.main([__file__, "-v"]))