Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions _plotly_utils/colors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,8 @@ def hex_to_rgb(value):
:rtype (tuple) (r_value, g_value, b_value): tuple of rgb values
"""
value = value.lstrip("#")
if len(value) == 3:
value = "".join(c * 2 for c in value)
hex_total_length = len(value)
rgb_section_length = hex_total_length // 3
return tuple(
Expand Down
2 changes: 2 additions & 0 deletions plotly/matplotlylib/mpltools.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ def hex_to_rgb(value):

"""
value = value.lstrip("#")
if len(value) == 3:
value = "".join(c * 2 for c in value)
lv = len(value)
return tuple(int(value[i : i + lv // 3], 16) for i in range(0, lv, lv // 3))

Expand Down
9 changes: 9 additions & 0 deletions tests/test_plotly_utils/colors/test_color_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ def test_hex_to_rgb_basic_values():
assert hex_to_rgb("#aabbcc") == (170, 187, 204)


def test_hex_to_rgb_shorthand_3_digit():
assert hex_to_rgb("#fff") == (255, 255, 255)
assert hex_to_rgb("#000") == (0, 0, 0)
assert hex_to_rgb("#abc") == (170, 187, 204)
assert hex_to_rgb("#f00") == (255, 0, 0)
assert hex_to_rgb("#0f0") == (0, 255, 0)
assert hex_to_rgb("#00f") == (0, 0, 255)


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)"
Expand Down