Skip to content

Commit

Permalink
Rename get_color to interpolate_color
Browse files Browse the repository at this point in the history
  • Loading branch information
tpvasconcelos committed Oct 16, 2024
1 parent f8a0342 commit 4ecfe5e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/ridgeplot/_colormodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/ridgeplot/_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,16 @@ 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:
validate_colorscale(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,
Expand Down
18 changes: 9 additions & 9 deletions tests/unit/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
_Color,
_colormap_loader,
apply_alpha,
get_color,
get_colorscale,
interpolate_color,
list_all_colorscale_names,
validate_colorscale,
)
Expand Down Expand Up @@ -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]


# ==============================================================
Expand Down

0 comments on commit 4ecfe5e

Please sign in to comment.