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, ExportMixin

An 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:
  • colors (array_like, shape (n, 3)) – sRGB floats in 0..1 (values outside are kept only with gamut="none").

  • name (str, optional) – Display name.

  • space (str, optional) – Name of the interpolation space the colours were built in (informational).

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 < 2 or 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 _r to 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'
property colors: ndarray

Read-only (n, 3) array of sRGB colours.

property name: str

The display name.

property space: str

Name of the interpolation space.

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']
reversed() → Colormap[source]

A copy with the colours in reverse order.

Examples

>>> from lcms2.colormap import Colormap
>>> Colormap.from_colors(["red", "blue"], n=2).reversed().hex()
['#0000ff', '#ff0000']
resampled(n: int) → Colormap[source]

Linearly resample to n entries (endpoints preserved).

Examples

>>> from lcms2.colormap import Colormap
>>> len(Colormap.from_colors(["red", "blue"], n=2).resampled(10))
10
get(name: str, cls: type | None = None) → Any[source]

Build the named colormap (a _r suffix reverses it).

Examples

>>> from lcms2.colormap import get
>>> get("glacier").name
'glacier'
list() → list[str]

Sorted names of the built-in colormaps.