diff --git a/src/ridgeplot/_colormodes.py b/src/ridgeplot/_colormodes.py index 0d3680e..9d41a9a 100644 --- a/src/ridgeplot/_colormodes.py +++ b/src/ridgeplot/_colormodes.py @@ -3,7 +3,7 @@ from dataclasses import dataclass from typing import TYPE_CHECKING, Literal, Protocol -from ridgeplot._colors import ColorScale, apply_alpha, get_color, normalise_colorscale +from ridgeplot._colors import ColorScale, apply_alpha, interpolate_color, normalise_colorscale from ridgeplot._types import CollectionL2 from ridgeplot._utils import get_xy_extrema, normalise_min_max @@ -134,7 +134,7 @@ def compute_trace_colors( coloralpha = float(coloralpha) def _get_color(mp: float) -> str: - color = get_color(colorscale, midpoint=mp) + color = interpolate_color(colorscale, midpoint=mp) if coloralpha is not None: color = apply_alpha(color, alpha=coloralpha) return color diff --git a/src/ridgeplot/_colors.py b/src/ridgeplot/_colors.py index 2d59309..31b420a 100644 --- a/src/ridgeplot/_colors.py +++ b/src/ridgeplot/_colors.py @@ -143,6 +143,8 @@ def get_colorscale(name: str) -> ColorScale: def normalise_colorscale(colorscale: ColorScale | str) -> ColorScale: + """Convert mixed colorscale representations to the canonical + :data:`ColorScale` format.""" if isinstance(colorscale, str): colorscale = get_colorscale(name=colorscale) else: @@ -150,7 +152,7 @@ def normalise_colorscale(colorscale: ColorScale | str) -> ColorScale: return colorscale -def get_color(colorscale: ColorScale, midpoint: float) -> str: +def interpolate_color(colorscale: ColorScale, midpoint: float) -> str: """Get a color from a colorscale at a given midpoint. Given a colorscale, it interpolates the expected color at a given midpoint, diff --git a/tests/unit/test_colors.py b/tests/unit/test_colors.py index 7d2b985..ce67692 100644 --- a/tests/unit/test_colors.py +++ b/tests/unit/test_colors.py @@ -12,8 +12,8 @@ _Color, _colormap_loader, apply_alpha, - get_color, get_colorscale, + interpolate_color, list_all_colorscale_names, validate_colorscale, ) @@ -194,24 +194,24 @@ def test_get_colorscale_fails_for_unknown_colorscale_name() -> None: # ============================================================== -# --- get_color() +# --- interpolate_color() # ============================================================== -def test_get_color_midpoint_in_scale() -> None: - assert get_color(colorscale=VIRIDIS, midpoint=0) == VIRIDIS[0][1] - assert get_color(colorscale=VIRIDIS, midpoint=1) == VIRIDIS[-1][1] +def test_interpolate_color_midpoint_in_scale() -> None: + assert interpolate_color(colorscale=VIRIDIS, midpoint=0) == VIRIDIS[0][1] + assert interpolate_color(colorscale=VIRIDIS, midpoint=1) == VIRIDIS[-1][1] -def test_get_color_midpoint_not_in_scale() -> None: +def test_interpolate_color_midpoint_not_in_scale() -> None: # Hard-coded test case. - assert get_color(colorscale=VIRIDIS, midpoint=0.5) == "rgb(34.5, 144.0, 139.5)" + assert interpolate_color(colorscale=VIRIDIS, midpoint=0.5) == "rgb(34.5, 144.0, 139.5)" @pytest.mark.parametrize("midpoint", [-10.0, -1.3, 1.9, 100.0]) -def test_get_color_fails_for_midpoint_out_of_bounds(midpoint: float) -> None: +def test_interpolate_color_fails_for_midpoint_out_of_bounds(midpoint: float) -> None: with pytest.raises(ValueError, match="should be a float value between 0 and 1"): - get_color(colorscale=..., midpoint=midpoint) # type: ignore[arg-type] + interpolate_color(colorscale=..., midpoint=midpoint) # type: ignore[arg-type] # ==============================================================