lcms2.spaces

Color spaces defined by conversions to and from CIE XYZ (D50).

Every space is a ColorSpace subclass with a ready-to-use instance named in lower case (srgb, oklab, …). Convert with ColorSpace.to:

>>> import numpy as np
>>> from lcms2.spaces import srgb, oklab
>>> srgb.to(oklab, np.array([1.0, 1.0, 1.0]))

ColorSpace.list returns the registered names, and ICCSpace reads a space from an ICC profile. Colorimetry helpers live in lcms2.colorimetry.

class ColorSpace(n_channels: int | None = None, to_xyz: Callable | None = None, from_xyz: Callable | None = None, name: str | None = None, device_domain: tuple | list | None = None)[source]

Bases: object

A color space defined by conversions between device values and CIE XYZ (D50).

Define one by subclassing and overriding to_xyz/from_xyz, or by passing to_xyz/from_xyz callables.

Parameters:
  • n_channels (int, optional) – Number of device channels. Defaults to the class attribute, and to the length of device_domain when that lists one pair per channel.

  • to_xyz (callable, optional) – Maps (..., n_channels) device values to (..., 3) XYZ. Required unless a subclass overrides to_xyz.

  • from_xyz (callable, optional) – Maps (..., 3) XYZ to (..., n_channels) device values. Required unless a subclass overrides from_xyz.

  • name (str, optional) – Display name. Defaults to the registered name.

  • device_domain (tuple or list of tuple, optional) – Device value range written to ICC profiles: one (lo, hi) pair shared by every channel, or one pair per channel. Defaults to the class attribute, (0.0, 1.0). Values outside it are clamped in a saved profile. Each end must be finite and lo < hi.

Variables:
  • n_channels (int) – Number of device channels.

  • device_domain (list of tuple) – One (lo, hi) pair per channel, whichever form was given.

  • circular_device_channels (frozenset of int) – Device channels holding an angle; they wrap over their device_domain range (0..360 for degrees).

  • icc_colour_space_signature (bytes or None) – ICC data colour space written to profiles; inferred when None.

Raises:
  • TypeError – If a direction has no implementation, or has both an override and a callable, or the channel count is neither declared nor implied by device_domain.

  • ValueError – If n_channels disagrees with device_domain, a range is invalid, or circular_device_channels names a channel the space does not have.

Examples

>>> import numpy as np
>>> from lcms2.spaces import ColorSpace
>>> gray = ColorSpace(
...     n_channels=1,
...     to_xyz=lambda v: np.repeat(v, 3, axis=-1),
...     from_xyz=lambda xyz: xyz[..., 1:2],
...     name="flat-gray",
... )
>>> gray.to_xyz([0.5]).tolist()
[0.5, 0.5, 0.5]
icc_colour_space_signature: bytes | None = None
n_channels: int | None = None
device_domain: tuple | list = (0.0, 1.0)
circular_device_channels: frozenset = frozenset({})
classmethod list() → list[source]

Return the sorted names of all registered color spaces.

Returns:

list of str

transform(data: Any, to: Any, *, intent: Any = 'relative', bpc: bool = False, proof: Any = None, gamut_check: bool = False, optimize: bool = True, precision: str = 'auto', flags: Any = None, out: ndarray | None = None, out_dtype: DTypeLike = None) → ndarray[source]

Convert data from this space to to, exactly like lcms2.Profile.transform().

to may be a colour space, a profile, a built-in profile name or an ICC file; see lcms2.convert() for the keywords (they apply when a profile is involved).

Examples

>>> from lcms2.spaces import oklab
>>> oklab.transform([1.0, 0.0, 0.0], "sRGB").round(3)
array([1., 1., 1.])
to(target: str | ColorSpace, array: Any) → ndarray[source]

Convert values from this space into target.

Parameters:
  • target (str or ColorSpace) – A registered name (see ColorSpace.list) or a color space instance.

  • array (array_like, shape (..., n_channels)) – Values in this space.

Returns:

ndarray, shape (…, target.n_channels)

Raises:
  • KeyError – If target is an unregistered name.

  • TypeError – If target names a space that needs constructor arguments.

Examples

>>> import numpy as np
>>> from lcms2.spaces import srgb, xyz_d50
>>> srgb.to(xyz_d50, np.array([1.0, 1.0, 1.0])).round(4)
array([0.9642, 1.    , 0.8249])
to_xyz(values: Any) → ndarray[source]

Convert device values to CIE XYZ (D50).

Parameters:

values (array_like, shape (..., n_channels))

Returns:

ndarray, shape (…, 3)

Raises:

ValueError – If the input’s last dimension is not n_channels, or the result does not have shape (..., 3).

from_xyz(xyz: Any) → ndarray[source]

Convert CIE XYZ (D50) to device values.

Parameters:

xyz (array_like, shape (..., 3))

Returns:

ndarray, shape (…, n_channels)

Raises:

ValueError – If the input’s last dimension is not 3, or the result does not have shape (..., n_channels).

property profile: Profile

This space as an lcms2.Profile (built once, exactly when possible).

Examples

>>> from lcms2.spaces import display_p3
>>> display_p3.profile.color_space == "RGB"
True
to_icc_profile(path: str | None = None, *, version: int | tuple = 4, rendering_intent: int = 1, grid_size: int | None = None, description: str | None = None, copyright: str = 'No copyright, generated by python-lcms2', force_clut: bool = False, strict: bool = False) → dict[source]

Write this space as an ICC profile.

Parameters:
  • path (str, optional) – File to write the profile to.

  • version (int or tuple of int, optional) – 2, 4, 5 or a published (major, minor, bugfix) revision such as (4, 2, 0). Default 4.

  • rendering_intent (int, optional) – Header rendering intent. Default 1 (relative colorimetric).

  • grid_size (int, optional) – Grid points per axis of sampled lookup tables.

  • description (str, optional) – Profile description text.

  • copyright (str, optional) – Profile copyright text.

  • force_clut (bool, optional) – Always write sampled lookup tables.

  • strict (bool, optional) – Raise ICCWriteError instead of falling back to sampled lookup tables when to_xyz or from_xyz has no exact representation, for example because it branches on values or calls a non-NumPy library. The message names the operation that could not be traced.

Returns:

dict – {'bytes': bytes, 'method': str, 'notes': list of str} where method is the representation written:

  • 'exact_matrix_trc': v2/v4 rXYZ/gXYZ/bXYZ matrix and TRC curves.

  • 'exact_gray_trc': v2/v4 kTRC gray curve.

  • 'exact_lut': v2/v4 A2B0/B2A0 lookup tables holding the curves and linear map exactly.

  • 'exact_mpet': v5 chain of matrix and curve elements.

  • 'exact_calc': v5 calculator elements.

  • 'clut_fallback': sampled lookup tables; notes says why.

Raises:

ICCWriteError – If the version or device_domain is invalid, if to_xyz or from_xyz fails on every input, or with strict=True if there is no exact representation.

get(name: str) → ColorSpace[source]

The registered color space called name (case-insensitive; _ and - match), as an instance.

The instance is shared: repeated calls return the same object until another class is registered under that name.

Raises:

KeyError – If no space is registered under name.

Examples

>>> from lcms2.spaces import get
>>> get("OKLab").n_channels
3
class RGBSpace(**kwargs: Any)[source]

Bases: ColorSpace

An RGB space: per-channel transfer function, then a 3x3 matrix to XYZ (D50).

Subclasses set matrix and optionally decode/encode.

Variables:
  • matrix (ndarray, shape (3, 3)) – Maps linear RGB column vectors to XYZ (D50).

  • decode (staticmethod) – Transfer function from device values to linear light.

  • encode (staticmethod) – Transfer function from linear light to device values.

n_channels: int | None = 3
matrix: ndarray = array([[1., 0., 0.],        [0., 1., 0.],        [0., 0., 1.]])
static decode(v: ndarray, /) → ndarray

Identity transfer function.

static encode(v: ndarray, /) → ndarray

Identity transfer function.

to_xyz(rgb: ndarray) → ndarray[source]

Convert device RGB to CIE XYZ (D50).

from_xyz(xyz: ndarray) → ndarray[source]

Convert CIE XYZ (D50) to device RGB.

class SRGBSpace(**kwargs: Any)[source]

Bases: RGBSpace

sRGB: IEC 61966-2-1 primaries and transfer function, D65 adapted to D50.

name = 'srgb'
matrix: ndarray = array([[0.43608787, 0.38507687, 0.14303526],        [0.22251054, 0.71688636, 0.0606032 ],        [0.01392599, 0.09707958, 0.71389443]])
static decode(v: ndarray) → ndarray

sRGB transfer function, device to linear; linear extension below 0.

static encode(v: ndarray) → ndarray

sRGB transfer function, linear to device; linear extension below 0.

class SRGBLinearSpace(**kwargs: Any)[source]

Bases: RGBSpace

sRGB primaries with linear-light values (CSS srgb-linear).

name = 'srgb-linear'
matrix: ndarray = array([[0.43608787, 0.38507687, 0.14303526],        [0.22251054, 0.71688636, 0.0606032 ],        [0.01392599, 0.09707958, 0.71389443]])
class DisplayP3Space(**kwargs: Any)[source]

Bases: RGBSpace

Display P3: DCI-P3 primaries, D65 white and the sRGB transfer function.

name = 'display-p3'
matrix: ndarray = array([[ 0.51509907,  0.29195737,  0.15714356],        [ 0.24117775,  0.69224359,  0.06657867],        [-0.00104976,  0.04187436,  0.7840754 ]])
static decode(v: ndarray) → ndarray

sRGB transfer function, device to linear; linear extension below 0.

static encode(v: ndarray) → ndarray

sRGB transfer function, linear to device; linear extension below 0.

class DisplayP3LinearSpace(**kwargs: Any)[source]

Bases: RGBSpace

Display P3 primaries with linear-light values.

name = 'display-p3-linear'
matrix: ndarray = array([[ 0.51509907,  0.29195737,  0.15714356],        [ 0.24117775,  0.69224359,  0.06657867],        [-0.00104976,  0.04187436,  0.7840754 ]])
class A98RGBSpace(**kwargs: Any)[source]

Bases: RGBSpace

Adobe RGB (1998) (CSS a98-rgb). Negative values give NaN.

name = 'a98-rgb'
matrix: ndarray = array([[0.60971825, 0.20525703, 0.14922471],        [0.31109966, 0.62567669, 0.06322365],        [0.01946608, 0.06087   , 0.74456392]])
static decode(v: ndarray) → ndarray

Adobe RGB transfer function, device to linear (v ** 2.19921875).

static encode(v: ndarray) → ndarray

Adobe RGB transfer function, linear to device.

class ProPhotoRGBSpace(**kwargs: Any)[source]

Bases: RGBSpace

ProPhoto RGB / ROMM RGB (CSS prophoto-rgb), D50 white.

name = 'prophoto-rgb'
matrix: ndarray = array([[7.976749e-01, 1.351917e-01, 3.135340e-02],        [2.880402e-01, 7.118741e-01, 8.570000e-05],        [0.000000e+00, 0.000000e+00, 8.252100e-01]])
static decode(v: ndarray) → ndarray

ProPhoto transfer function, device to linear.

static encode(v: ndarray) → ndarray

ProPhoto transfer function, linear to device.

class Rec2020Space(**kwargs: Any)[source]

Bases: RGBSpace

ITU-R BT.2020 primaries and transfer function (CSS rec2020).

name = 'rec2020'
matrix: ndarray = array([[ 0.67345523,  0.16565394,  0.12509083],        [ 0.27902729,  0.67534701,  0.0456257 ],        [-0.00193223,  0.02997746,  0.79685477]])
static decode(v: ndarray) → ndarray

BT.2020 transfer function, device to linear.

static encode(v: ndarray) → ndarray

BT.2020 transfer function, linear to device.

class Rec2100LinearSpace(**kwargs: Any)[source]

Bases: RGBSpace

BT.2020 primaries with linear light; 1.0 is the reference white.

name = 'rec2100-linear'
matrix: ndarray = array([[ 0.67345523,  0.16565394,  0.12509083],        [ 0.27902729,  0.67534701,  0.0456257 ],        [-0.00193223,  0.02997746,  0.79685477]])
class Rec2100PQSpace(**kwargs: Any)[source]

Bases: RGBSpace

BT.2020 primaries with the PQ curve; signal 1.0 maps to the reference white. Negative values give NaN.

name = 'rec2100-pq'
matrix: ndarray = array([[ 0.67345523,  0.16565394,  0.12509083],        [ 0.27902729,  0.67534701,  0.0456257 ],        [-0.00193223,  0.02997746,  0.79685477]])
static decode(e: ndarray) → ndarray

SMPTE ST 2084 EOTF: signal to normalized linear light (1.0 = 10000 cd/m2).

static encode(y: ndarray) → ndarray

SMPTE ST 2084 inverse EOTF: normalized linear light to signal.

class Rec2100HLGSpace(**kwargs: Any)[source]

Bases: RGBSpace

BT.2020 primaries with the HLG OETF (no OOTF); signal 1.0 maps to the reference white.

name = 'rec2100-hlg'
matrix: ndarray = array([[ 0.67345523,  0.16565394,  0.12509083],        [ 0.27902729,  0.67534701,  0.0456257 ],        [-0.00193223,  0.02997746,  0.79685477]])
static decode(e: ndarray) → ndarray

BT.2100 HLG inverse OETF: signal to scene linear light.

static encode(lin: ndarray) → ndarray

BT.2100 HLG OETF: scene linear light to signal.

class XYZD50Space(**kwargs: Any)[source]

Bases: RGBSpace

CIE XYZ relative to D50 (CSS xyz-d50); identical to the PCS.

name = 'xyz-d50'
icc_colour_space_signature: bytes | None = b'XYZ '
matrix: ndarray = array([[1., 0., 0.],        [0., 1., 0.],        [0., 0., 1.]])
class XYZD65Space(**kwargs: Any)[source]

Bases: RGBSpace

CIE XYZ relative to D65 (CSS xyz-d65), Bradford-adapted to D50.

name = 'xyz-d65'
icc_colour_space_signature: bytes | None = b'XYZ '
matrix: ndarray = array([[ 1.04783995,  0.02289792, -0.0501808 ],        [ 0.02955369,  0.99049242, -0.01706631],        [-0.00924592,  0.01506326,  0.75183886]])
class GraySpace(gamma: float = 2.2, white: Any = None, **kwargs: Any)[source]

Bases: ColorSpace

Grayscale with luminance Y = g ** gamma along a white point.

Parameters:
  • gamma (float, optional) – Exponent from device value to luminance. Default 2.2.

  • white (array_like, shape (3,), optional) – XYZ of device value 1. Default D50 (copied, never aliased).

  • **kwargs – Passed to ColorSpace.

Raises:

ValueError – If gamma is not positive and finite, or white does not have shape (3,).

name = 'gray'
n_channels: int | None = 1
to_xyz(gray: ndarray) → ndarray[source]

Convert gray values to CIE XYZ (D50). Negative values give NaN.

from_xyz(xyz: ndarray) → ndarray[source]

Convert CIE XYZ (D50) to gray values from the Y channel.

class LogGraySpace(black: float = 0.001, white: Any = None, **kwargs: Any)[source]

Bases: ColorSpace

Grayscale with luminance Y = black * (1 / black) ** g along a white point.

Parameters:
  • black (float, optional) – Luminance of device value 0. Default 0.001.

  • white (array_like, shape (3,), optional) – XYZ of device value 1. Default D50 (copied, never aliased).

  • **kwargs – Passed to ColorSpace.

Raises:

ValueError – If black is not in (0, 1) or white does not have shape (3,).

name = 'loggray'
n_channels: int | None = 1
to_xyz(gray: ndarray) → ndarray[source]

Convert gray values to CIE XYZ (D50).

from_xyz(xyz: ndarray) → ndarray[source]

Convert CIE XYZ (D50) to gray values. Non-positive Y gives NaN or -inf.

class OKLabSpace(n_channels: int | None = None, to_xyz: Callable | None = None, from_xyz: Callable | None = None, name: str | None = None, device_domain: tuple | list | None = None)[source]

Bases: ColorSpace

OKLab (Ottosson 2020) on linear sRGB primaries, adapted to D50.

name = 'oklab'
icc_colour_space_signature: bytes | None = b'3CLR'
device_domain: tuple | list = [(0.0, 1.0), (-0.4, 0.4), (-0.4, 0.4)]
to_xyz(oklab: ndarray) → ndarray[source]

Convert OKLab to CIE XYZ (D50).

from_xyz(xyz: ndarray) → ndarray[source]

Convert CIE XYZ (D50) to OKLab.

class OKLChSpace(n_channels: int | None = None, to_xyz: Callable | None = None, from_xyz: Callable | None = None, name: str | None = None, device_domain: tuple | list | None = None)[source]

Bases: ColorSpace

Cylindrical OKLab (CSS oklch); hue in degrees.

name = 'oklch'
icc_colour_space_signature: bytes | None = b'3CLR'
circular_device_channels: frozenset = frozenset({2})
device_domain: tuple | list = [(0.0, 1.0), (0.0, 0.5), (0.0, 360.0)]
to_xyz(oklch: ndarray) → ndarray[source]

Convert OKLCh to CIE XYZ (D50).

from_xyz(xyz: ndarray) → ndarray[source]

Convert CIE XYZ (D50) to OKLCh.

class LChSpace(n_channels: int | None = None, to_xyz: Callable | None = None, from_xyz: Callable | None = None, name: str | None = None, device_domain: tuple | list | None = None)[source]

Bases: ColorSpace

CIE LCh(ab) relative to D50 (CSS lch); hue in degrees.

name = 'lch'
icc_colour_space_signature: bytes | None = b'3CLR'
circular_device_channels: frozenset = frozenset({2})
device_domain: tuple | list = [(0.0, 100.0), (0.0, 150.0), (0.0, 360.0)]
to_xyz(lch: ndarray) → ndarray[source]

Convert LCh to CIE XYZ (D50).

from_xyz(xyz: ndarray) → ndarray[source]

Convert CIE XYZ (D50) to LCh.

class HSVSpace(n_channels: int | None = None, to_xyz: Callable | None = None, from_xyz: Callable | None = None, name: str | None = None, device_domain: tuple | list | None = None)[source]

Bases: ColorSpace

HSV over sRGB; hue in degrees (circular), saturation and value in 0..1.

name = 'hsv'
n_channels: int | None = 3
icc_colour_space_signature: bytes | None = b'HSV '
circular_device_channels: frozenset = frozenset({0})
device_domain: tuple | list = [(0.0, 360.0), (0.0, 1.0), (0.0, 1.0)]
to_xyz(hsv: ndarray) → ndarray[source]

Convert HSV to CIE XYZ (D50).

from_xyz(xyz: ndarray) → ndarray[source]

Convert CIE XYZ (D50) to HSV.

class HWBSpace(n_channels: int | None = None, to_xyz: Callable | None = None, from_xyz: Callable | None = None, name: str | None = None, device_domain: tuple | list | None = None)[source]

Bases: ColorSpace

Hue (degrees), whiteness, blackness over sRGB (CSS Color 4); whiteness plus blackness above 1 is normalized to 1.

name = 'hwb'
n_channels: int | None = 3
icc_colour_space_signature: bytes | None = b'3CLR'
circular_device_channels: frozenset = frozenset({0})
device_domain: tuple | list = [(0.0, 360.0), (0.0, 1.0), (0.0, 1.0)]
to_xyz(hwb: ndarray) → ndarray[source]

Convert HWB to CIE XYZ (D50).

from_xyz(xyz: ndarray) → ndarray[source]

Convert CIE XYZ (D50) to HWB.

class CMYKSpace(n_channels: int | None = None, to_xyz: Callable | None = None, from_xyz: Callable | None = None, name: str | None = None, device_domain: tuple | list | None = None)[source]

Bases: ColorSpace

CMYK ink in percent (0..100, as in Photoshop and Little CMS) from the complement of sRGB with K = min(C, M, Y); not an ink model.

name = 'cmyk'
n_channels: int | None = 4
icc_colour_space_signature: bytes | None = b'CMYK'
device_domain: tuple | list = [(0.0, 100.0), (0.0, 100.0), (0.0, 100.0), (0.0, 100.0)]
to_xyz(cmyk: ndarray) → ndarray[source]

Convert CMYK to CIE XYZ (D50).

from_xyz(xyz: ndarray) → ndarray[source]

Convert CIE XYZ (D50) to CMYK.