Skip to content

Commit a540089

Browse files
authored
feat: add top level to_mpl convenience (#91)
* feat: add top level to_mpl convenience * add test * move test
1 parent 00ceed1 commit a540089

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

src/cmap/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from ._color import HSLA, HSVA, RGBA, RGBA8, Color, ColorLike
1313
from ._colormap import Colormap, ColormapLike, ColorStop, ColorStops
14+
from ._util import to_mpl
1415

1516
if TYPE_CHECKING:
1617
from ._catalog import CatalogItem
@@ -74,4 +75,5 @@ def resolve(self, name: str) -> str:
7475
"ColorStops",
7576
"Colormap",
7677
"ColormapLike",
78+
"to_mpl",
7779
]

src/cmap/_util.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@
99
gradient = np.vstack((gradient, gradient))
1010

1111
if TYPE_CHECKING:
12+
import matplotlib.colors
1213
from matplotlib.figure import Figure as MplFigure
1314
from numpy.typing import ArrayLike, NDArray
1415

1516
from cmap import Colormap
1617

18+
from ._color import ColorLike
19+
from ._colormap import ColorStopsLike, Interpolation
20+
1721

1822
def _ensure_cmap(cmap: Colormap | str) -> Colormap:
1923
from cmap import Colormap
@@ -498,6 +502,40 @@ def report(cm: Colormap, n: int = 256, uniform_space: str = "CAM02-UCS") -> Repo
498502
# H -> hue composition
499503

500504

505+
def to_mpl(
506+
value: ColorStopsLike,
507+
/,
508+
*,
509+
name: str | None = None,
510+
identifier: str | None = None,
511+
category: str | None = None,
512+
interpolation: Interpolation | bool | None = None,
513+
under: ColorLike | None = None,
514+
over: ColorLike | None = None,
515+
bad: ColorLike | None = None,
516+
N: int = 256,
517+
gamma: float = 1.0,
518+
) -> matplotlib.colors.Colormap:
519+
"""Convienence function to create a matplotlib colormap.
520+
521+
Arguments are the same as for [cmap.Colormap][]. This simply creates the
522+
colormap instance and returns the value of `to_matplotlib()`.
523+
"""
524+
from cmap import Colormap
525+
526+
cm = Colormap(
527+
value,
528+
name=name,
529+
identifier=identifier,
530+
category=category,
531+
interpolation=interpolation,
532+
under=under,
533+
over=over,
534+
bad=bad,
535+
)
536+
return cm.to_matplotlib(N=N, gamma=gamma)
537+
538+
501539
if __name__ == "__main__": # pragma: no cover
502540
import matplotlib.pyplot as plt
503541

tests/test_third_party.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import numpy as np
88
import pytest
99

10+
import cmap
1011
from cmap import Color, Colormap
1112

1213
try:
@@ -184,8 +185,7 @@ def test_pyqtgraph() -> None:
184185
cm.reverse()
185186

186187

187-
# def microvis_imshow(img_data: np.ndarray, cmap: cmap.Colormap) -> None:
188-
# from microvis import _util, imshow
188+
def test_to_mpl() -> None:
189+
colors = pytest.importorskip("matplotlib.colors")
189190

190-
# with _util.exec_if_new_qt_app():
191-
# imshow(img_data, cmap=cmap)
191+
assert isinstance(cmap.to_mpl("viridis"), colors.Colormap)

0 commit comments

Comments
 (0)