diff --git a/README.md b/README.md index 79915bba2..ec8307f57 100644 --- a/README.md +++ b/README.md @@ -92,8 +92,17 @@ to an integer array (like an image) you must apply any contrast limits and resca ## Third Party Library Support -The `cmap.Colormap` object has convenience methods that allow it to be used with a number of third-party colormap objects (like -`matplotlib`, `vispy`, `napari`, `plotly`, etc...). +The `cmap.Colormap` object has convenience methods that export it to a number of known +third-party colormap objects, including: + +- [matplotlib](https://matplotlib.org/) +- [napari](https://napari.org/) +- [vispy](https://vispy.org/) +- [pygfx](https://pygfx.readthedocs.io/en/latest/) (& [fastplotlib](https://github.com/fastplotlib/fastplotlib)) +- [plotly](https://plotly.com/python/) +- [bokeh](https://docs.bokeh.org/en/latest/) +- [altair](https://altair-viz.github.io/) +- [pyqtgraph](https://www.pyqtgraph.org/) See [documentation](https://cmap-docs.readthedocs.io/en/latest/colormaps/#usage-with-external-visualization-libraries) for details. @@ -104,7 +113,6 @@ If you would like to see support added for a particular library, please open an Other libraries providing colormaps: - - [matplotlib](https://matplotlib.org/stable/tutorials/colors/colormaps.html) - [seaborn](https://seaborn.pydata.org/tutorial/color_palettes.html) (subclasses matplotlib) - [proplot](https://proplot.readthedocs.io/en/latest/colormaps.html) (subclasses matplotlib) @@ -128,4 +136,4 @@ Other libraries providing colormaps: - [Color Map Advice for Scientific Visualization](https://www.kennethmoreland.com/color-advice/) - , Peter Kovesi - [Kovesi: Good Colour Maps: How to Design Them.](https://arxiv.org/abs/1509.03700) -- https://www.fabiocrameri.ch/colourmaps/ +- diff --git a/docs/colormaps.md b/docs/colormaps.md index 48b3aaee6..22719d147 100644 --- a/docs/colormaps.md +++ b/docs/colormaps.md @@ -234,6 +234,15 @@ external visualization libraries. To that end, `cmap.Colormap` provides Returns a list of hexadecimal color strings. +- [pyqtgraph](https://pyqtgraph.org/) + + ```python + Colormap("viridis").to_pyqtgraph() + ``` + + Returns an instance of + [`pyqtgraph.ColorMap`](https://pyqtgraph.readthedocs.io/en/latest/api_reference/colormap.html#pyqtgraph.ColorMap) + ## Usage with pydantic `Colormap` can be used as a field type in diff --git a/pyproject.toml b/pyproject.toml index 87285f0af..2914635bf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -59,6 +59,7 @@ thirdparty = [ "rich", "viscm", "vispy", + "pyqtgraph", ] dev = [ "black", diff --git a/src/cmap/_colormap.py b/src/cmap/_colormap.py index 28405d2ad..6d80560aa 100644 --- a/src/cmap/_colormap.py +++ b/src/cmap/_colormap.py @@ -21,6 +21,7 @@ import matplotlib.figure import napari.utils.colormaps import pygfx + import pyqtgraph import vispy.color from numpy.typing import ArrayLike, NDArray from pydantic import GetCoreSchemaHandler @@ -546,6 +547,10 @@ def to_viscm( """ return _external.viscm_plot(self, dpi, dest) + def to_pyqtgraph(self) -> pyqtgraph.ColorMap: + """Return a `pyqtgraph.ColorMap`.""" + return _external.to_pyqtgraph(self) + class ColorStop(NamedTuple): """A color stop in a color gradient.""" diff --git a/src/cmap/_external.py b/src/cmap/_external.py index c25626cfe..af46cdd7f 100644 --- a/src/cmap/_external.py +++ b/src/cmap/_external.py @@ -14,6 +14,7 @@ from matplotlib.colors import LinearSegmentedColormap as MplLinearSegmentedColormap from matplotlib.figure import Figure as MplFigure from napari.utils.colormaps import Colormap as NapariColormap + from pyqtgraph import ColorMap as PyqtgraphColorMap from vispy.color import Colormap as VispyColormap from ._color import Color @@ -97,6 +98,17 @@ def to_altair(cm: Colormap, N: int = 256) -> list[str]: return [color.hex for color in cm.iter_colors(N)] +def to_pyqtgraph(cm: Colormap) -> PyqtgraphColorMap: + """Return a `pyqtgraph.Colormap`.""" + from pyqtgraph import ColorMap + + colors = (cm.color_stops.color_array * 255).astype(np.uint8) + return ColorMap(cm.color_stops.stops, colors, name=cm.name) + + +# ========================================== + + def viscm_plot( cmap: Colormap | MplColormap, dpi: int = 100, dest: str | None = None ) -> MplFigure: diff --git a/tests/test_third_party.py b/tests/test_third_party.py index e66228294..490f72180 100644 --- a/tests/test_third_party.py +++ b/tests/test_third_party.py @@ -141,6 +141,16 @@ def test_viscm(tmp_path: Path) -> None: assert out.is_file() +def test_pyqtgraph() -> None: + pytest.importorskip("pyqtgraph") + cmap1 = Colormap(["red", "green", "blue"]) + cm = cmap1.to_pyqtgraph() + + cm.getGradient() + cm.linearize() + cm.reverse() + + # def microvis_imshow(img_data: np.ndarray, cmap: cmap.Colormap) -> None: # from microvis import _util, imshow