lcms2.colormap¶
Perceptually designed colormaps, gamut-mapped to sRGB.
Get a map in one line and use it in matplotlib in two:
>>> from lcms2.colormap import Colormap
>>> cm = Colormap.get("glacier")
>>> cm(0.5)
>>> plt.imshow(data, cmap=cm.to_matplotlib())
Colormap.from_colors interpolates your own colours in OKLab (or any colour space), the generators (Colormap.sequential, Colormap.diverging, Colormap.cyclic, Colormap.qualitative) design maps from lightness and hue, and list names the built-in set.
- class Colormap(colors: Any, name: str | None = None, space: Any = 'oklab')[source]¶
Bases:
GeneratorsMixin,DiagnosticsMixin,ExportMixinAn immutable colormap: sampled sRGB colours from 0 to 1.
Build one with Colormap.from_colors, a generator (Colormap.sequential, Colormap.diverging, Colormap.cyclic, Colormap.qualitative) or by name with Colormap.get. Call it on data to get colours.
- Parameters:
Examples
>>> import numpy as np >>> from lcms2.colormap import Colormap >>> cm = Colormap.from_colors(["black", "white"], n=5, name="ramp") >>> len(cm), cm.name (5, 'ramp') >>> cm(np.array([0.0, 0.5, 1.0])).round(2) array([[0. , 0. , 0. ], [0.39, 0.39, 0.39], [1. , 1. , 1. ]]) >>> cm(np.array([0, 5, 10]), vmin=0, vmax=10, bytes=True) array([[ 0, 0, 0], [ 99, 99, 99], [255, 255, 255]], dtype=uint8)
- classmethod from_colors(colors: Any, *, space: Any = 'oklab', n: int = 256, positions: Any = None, name: str | None = None, gamut: str = 'chroma') Colormap[source]¶
Interpolate control colours into a colormap.
- Parameters:
colors (sequence) – Two or more colours in any form lcms2._parse.parse_color accepts.
space (str or ColorSpace, default "oklab") – Space the colours are interpolated in. Cylindrical spaces (
oklch,lch,hsv) interpolate hue along the shorter arc.n (int, default 256) – Number of samples.
positions (sequence of float or "uniform", optional) – Increasing positions of the control colours; rescaled to span 0..1. Default: evenly spaced (stops at 0, 1/(k-1), …, 1).
"uniform"places each stop at its cumulative OKLab distance from the first, so the perceived colour change per step is the same along the whole map.name (str, optional)
gamut ({"chroma", "clip", "none"}, default "chroma") –
"chroma"reduces OKLCh chroma at constant lightness and hue until the colour fits sRGB;"clip"clips channels;"none"leaves them.
- Returns:
Colormap
- Raises:
ValueError – For fewer than two colours, bad positions,
n < 2or an unknown gamut mode.
Examples
>>> from lcms2.colormap import Colormap >>> cm = Colormap.from_colors(["#000", "#f80", "#fff"], n=3) >>> cm.hex() ['#000000', '#ff8800', '#ffffff'] >>> cm = Colormap.from_colors(["#000", "#f80", "#fff"], positions="uniform")
- classmethod get(name: str) Colormap[source]¶
A built-in colormap by name (append
_rto reverse); see lcms2.colormap.list.- Raises:
KeyError – For an unknown name; the message lists the available ones.
Examples
>>> from lcms2.colormap import Colormap >>> Colormap.get("glacier_r").name 'glacier_r'
- hex() list[str][source]¶
The colours as
"#rrggbb"strings.Examples
>>> from lcms2.colormap import Colormap >>> Colormap.from_colors(["red", "blue"], n=2).hex() ['#ff0000', '#0000ff']