diff --git a/_plotly_utils/colors/__init__.py b/_plotly_utils/colors/__init__.py index 254c8ce264..193cb378e4 100644 --- a/_plotly_utils/colors/__init__.py +++ b/_plotly_utils/colors/__init__.py @@ -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( diff --git a/plotly/matplotlylib/mpltools.py b/plotly/matplotlylib/mpltools.py index 4268136003..0865a7df37 100644 --- a/plotly/matplotlylib/mpltools.py +++ b/plotly/matplotlylib/mpltools.py @@ -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)) diff --git a/tests/test_plotly_utils/colors/test_color_conversions.py b/tests/test_plotly_utils/colors/test_color_conversions.py index 282a81ce56..6513b7c323 100644 --- a/tests/test_plotly_utils/colors/test_color_conversions.py +++ b/tests/test_plotly_utils/colors/test_color_conversions.py @@ -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)"