Skip to content

Commit 80a94ec

Browse files
committed
accept 4- and 8- character hex codes with a warning
1 parent c1cb8c2 commit 80a94ec

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

‎_plotly_utils/colors/__init__.py‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676

7777
import decimal
7878
from numbers import Number
79+
from warnings import warn
7980

8081
from _plotly_utils import exceptions
8182

@@ -763,15 +764,27 @@ def hex_to_rgb(value):
763764
'#FFF' --> (255, 255, 255)
764765
765766
"""
767+
768+
input_value = value
766769
value = value.lstrip("#")
767770
if len(value) == 3:
768771
value = "".join(c * 2 for c in value)
772+
elif len(value) == 4:
773+
warn(
774+
"4-character hex color provided; 4th character will be ignored."
775+
"got {!r}".format(input_value)
776+
)
777+
value = "".join(c * 2 for c in value)[:6]
778+
elif len(value) == 8:
779+
warn(
780+
"8-character hex color provided; last two characters will be ignored."
781+
"got {!r}".format(input_value)
782+
)
783+
value = value[:6]
769784
elif len(value) != 6:
770-
# Any other length silently produced a tuple of the wrong size: the
771-
# section width is len // 3, so "#12345" returned a 5-tuple.
772785
raise ValueError(
773786
"hex color must be 3 or 6 hex digits, optionally prefixed with "
774-
"'#'; got {!r}".format(value)
787+
"'#'; got {!r}".format(input_value)
775788
)
776789
return tuple(int(value[i : i + 2], 16) for i in range(0, 6, 2))
777790

0 commit comments

Comments
 (0)