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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Fix `mpl_to_plotly` conversion of matplotlib contour lines and line collections: close contour rings ending with `Z` codes, support dash styles, convert date x-axes, separate disjoint subpaths with `None` separators, hide line collection traces from the legend by default, and group consecutive same-style lines into single traces [[#5770](https://github.com/plotly/plotly.py/pull/5770)], with thanks to @robertoffmoura for the contribution!
- Fix `plotly.io.write_images` ignoring `layout.width`/`layout.height` (and the template's) and exporting every figure at the default 700x500 size; it now uses the same width/height priority as `write_image` and `to_image` [[#5784](https://github.com/plotly/plotly.py/pull/5784)], with thanks to @breken-ai for the contribution!
- Fix color utilities mutating the list of colors passed to them [[#5836](https://github.com/plotly/plotly.py/pull/5836)], with thanks to @Enesuygurs for the contribution!
- Accept CSS color formats that plotly.js supports but the color validator rejected, such as negative `lab()`/`oklab()` components, `color()` values, angle units other than `deg`, the `none` keyword and `transparent` [[#5868](https://github.com/plotly/plotly.py/pull/5868)], with thanks to @ShawnNotFound for the contribution!


## [7.1.0] - 2026-09-15
Expand Down
25 changes: 16 additions & 9 deletions _plotly_utils/basevalidators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1138,13 +1138,24 @@ class ColorValidator(BaseValidator):
},
"""

re_spaces_to_remove = re.compile(r"(?<!(\d|%|g)) | (?!\d)")

re_hex = re.compile(
r"#([A-Fa-f0-9]{3}|[A-Fa-f0-9]{4}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})"
)

# A CSS color function argument: a number (which may be signed or use an
# exponent) with an optional percent sign or angle unit, or `none`.
_re_css_value = (
r"(?:[+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?(?:%|deg|grad|rad|turn)?|none)"
)
_re_css_sep = r"(?:\s*,\s*|\s+)"
_re_css_alpha_sep = r"(?:\s*[,/]\s*|\s+)"
re_rgb_etc = re.compile(
r"((rgb|hsl)a?|hwb|(ok)?(lab|lch))\([\d.]+(%|deg)?([ ,] ?[\d.]+%?){2}([ /,] ?[\d.]+%?)?\)|color\([\w-]+([ ,] ?[\d.]+){3,4}\)"
r"(?:(?:rgb|hsl)a?|hwb|(?:ok)?(?:lab|lch))\s*\(\s*"
rf"{_re_css_value}{_re_css_sep}{_re_css_value}{_re_css_sep}{_re_css_value}"
rf"(?:{_re_css_alpha_sep}{_re_css_value})?\s*\)"
r"|color\s*\(\s*[a-z][\w-]*"
rf"(?:{_re_css_sep}{_re_css_value}){{3}}"
rf"(?:{_re_css_alpha_sep}{_re_css_value})?\s*\)"
)
re_ddk = re.compile(r"var\(\-\-.*\)")

Expand Down Expand Up @@ -1290,6 +1301,7 @@ class ColorValidator(BaseValidator):
"teal",
"thistle",
"tomato",
"transparent",
"turquoise",
"violet",
"wheat",
Expand Down Expand Up @@ -1443,12 +1455,7 @@ def perform_validate_coerce(v, allow_number=None):
# If not allow_numbers then value must be a string
return None
else:
# Remove spaces so regexes don't need to bother with them.
# Don't remove spaces between two digits, though.
v_normalized = v.strip()
v_normalized = re.sub(" +", " ", v_normalized)
v_normalized = re.sub(ColorValidator.re_spaces_to_remove, "", v_normalized)
v_normalized = v_normalized.lower()
v_normalized = v.strip().lower()

# if ColorValidator.re_hex.fullmatch(v_normalized):
if fullmatch(ColorValidator.re_hex, v_normalized):
Expand Down
18 changes: 18 additions & 0 deletions tests/test_plotly_utils/validators/test_color_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ def validator_aok_colorscale():
"lab(70 15 50)",
"lch(70 15 180)",
"color(display-p3 1 0 0)",
"color(srgb 1 0 0)",
"color(a98-rgb 1 0 0 / 0.5)",
"lab(50% -40 59.5)",
"oklab(0.6 0.1 -0.05)",
"lch(52% 72 -30)",
"hsl(-30, 50%, 50%)",
"hsl(0.5turn 60% 40%)",
"hsl(1rad 60% 40%)",
"hsl(none 60% 40%)",
"rgb(255 0 0 / none)",
"rgb(1e2, 0, 0)",
"transparent",
]

INVALID_COLORS_WRONG_TYPE = [
Expand All @@ -72,6 +84,12 @@ def validator_aok_colorscale():
"hsl(0, 1%0000%, 50%)",
"hsv(0, 100%, 100%)",
"hsva(0, 100%, 100%, 50%)",
"currentcolor",
"rgb(255, 0)",
"rgb(255 0 0 /)",
"rgb(1-2 3 4)",
"color(1 0 0)",
"color(srgb 1 0)",
]


Expand Down