-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ceb1800
commit 61a5202
Showing
19 changed files
with
313 additions
and
275 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Any, cast | ||
|
||
import plotly.express as px | ||
from _plotly_utils.basevalidators import ColorscaleValidator as _ColorscaleValidator | ||
|
||
from ridgeplot._types import Color, ColorScale | ||
|
||
if TYPE_CHECKING: | ||
from collections.abc import Collection | ||
|
||
|
||
def list_all_colorscale_names() -> list[str]: | ||
"""Get a list with all available colorscale names. | ||
.. versionadded:: 0.1.21 | ||
Replaced the deprecated function ``get_all_colorscale_names()``. | ||
Returns | ||
------- | ||
list[str] | ||
A list with all available colorscale names. | ||
""" | ||
# Add 'default' for backwards compatibility | ||
px_colorscales = px.colors.named_colorscales() | ||
return sorted({"default", *px_colorscales, *(f"{name}_r" for name in px_colorscales)}) | ||
|
||
|
||
class ColorscaleValidator(_ColorscaleValidator): # type: ignore[misc] | ||
def __init__(self) -> None: | ||
super().__init__("colorscale", "ridgeplot") | ||
|
||
@property | ||
def named_colorscales(self) -> dict[str, list[str]]: | ||
named_colorscales = cast(dict[str, list[str]], super().named_colorscales) | ||
if "default" not in named_colorscales: | ||
# Add 'default' for backwards compatibility | ||
named_colorscales["default"] = px.colors.DEFAULT_PLOTLY_COLORS | ||
return named_colorscales | ||
|
||
def validate_coerce(self, v: Any) -> ColorScale: | ||
coerced = super().validate_coerce(v) | ||
if coerced is None: | ||
self.raise_invalid_val(coerced) | ||
return cast(ColorScale, [tuple(c) for c in coerced]) | ||
|
||
|
||
def validate_and_coerce_colorscale(colorscale: ColorScale | Collection[Color] | str) -> ColorScale: | ||
"""Convert mixed colorscale representations to the canonical | ||
:data:`ColorScale` format.""" | ||
return ColorscaleValidator().validate_coerce(colorscale) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, cast | ||
|
||
from plotly import express as px | ||
|
||
from ridgeplot._color.css_colors import CSS_NAMED_COLORS, CssNamedColor | ||
|
||
if TYPE_CHECKING: | ||
from ridgeplot._types import Color | ||
|
||
|
||
def to_rgb(color: Color) -> str: | ||
if not isinstance(color, (str, tuple)): | ||
raise TypeError(f"Expected str or tuple for color, got {type(color)} instead.") | ||
if isinstance(color, tuple): | ||
r, g, b = color | ||
rgb = f"rgb({r}, {g}, {b})" | ||
elif color.startswith("#"): | ||
return to_rgb(cast(str, px.colors.hex_to_rgb(color))) | ||
elif color.startswith(("rgb(", "rgba(")): | ||
rgb = color | ||
elif color in CSS_NAMED_COLORS: | ||
color = cast(CssNamedColor, color) | ||
return to_rgb(CSS_NAMED_COLORS[color]) | ||
else: | ||
raise ValueError( | ||
f"color should be a tuple or a str representation " | ||
f"of a hex or rgb color, got {color!r} instead." | ||
) | ||
px.colors.validate_colors(rgb) | ||
return rgb | ||
|
||
|
||
def unpack_rgb(rgb: str) -> tuple[float, float, float, float] | tuple[float, float, float]: | ||
prefix = rgb.split("(")[0] + "(" | ||
values_str = map(str.strip, rgb.removeprefix(prefix).removesuffix(")").split(",")) | ||
values_num = tuple(int(v) if v.isdecimal() else float(v) for v in values_str) | ||
return values_num # type: ignore[return-value] | ||
|
||
|
||
def apply_alpha(color: Color, alpha: float) -> str: | ||
values = unpack_rgb(to_rgb(color)) | ||
return f"rgba({', '.join(map(str, values[:3]))}, {alpha})" | ||
|
||
|
||
def round_color(color: Color, ndigits: int) -> str: | ||
color = to_rgb(color) | ||
prefix = color.split("(")[0] + "(" | ||
values_round = tuple(v if isinstance(v, int) else round(v, ndigits) for v in unpack_rgb(color)) | ||
return f"{prefix}{', '.join(map(str, values_round))})" |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.