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:
objectA 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 andlo < 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]
- 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
datafrom this space toto, exactly likelcms2.Profile.transform().tomay be a colour space, a profile, a built-in profile name or an ICC file; seelcms2.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:
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,5or 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
ICCWriteErrorinstead 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=Trueif 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:
ColorSpaceAn 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.
- class SRGBSpace(**kwargs: Any)[source]¶
Bases:
RGBSpacesRGB: 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]])¶
- class SRGBLinearSpace(**kwargs: Any)[source]¶
Bases:
RGBSpacesRGB primaries with linear-light values (CSS
srgb-linear).- name = 'srgb-linear'¶
- class DisplayP3Space(**kwargs: Any)[source]¶
Bases:
RGBSpaceDisplay 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 ]])¶
- class DisplayP3LinearSpace(**kwargs: Any)[source]¶
Bases:
RGBSpaceDisplay P3 primaries with linear-light values.
- name = 'display-p3-linear'¶
- class A98RGBSpace(**kwargs: Any)[source]¶
Bases:
RGBSpaceAdobe RGB (1998) (CSS
a98-rgb). Negative values give NaN.- name = 'a98-rgb'¶
- class ProPhotoRGBSpace(**kwargs: Any)[source]¶
Bases:
RGBSpaceProPhoto RGB / ROMM RGB (CSS
prophoto-rgb), D50 white.- name = 'prophoto-rgb'¶
- class Rec2020Space(**kwargs: Any)[source]¶
Bases:
RGBSpaceITU-R BT.2020 primaries and transfer function (CSS
rec2020).- name = 'rec2020'¶
- class Rec2100LinearSpace(**kwargs: Any)[source]¶
Bases:
RGBSpaceBT.2020 primaries with linear light; 1.0 is the reference white.
- name = 'rec2100-linear'¶
- class Rec2100PQSpace(**kwargs: Any)[source]¶
Bases:
RGBSpaceBT.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]])¶
- class Rec2100HLGSpace(**kwargs: Any)[source]¶
Bases:
RGBSpaceBT.2020 primaries with the HLG OETF (no OOTF); signal 1.0 maps to the reference white.
- name = 'rec2100-hlg'¶
- class XYZD50Space(**kwargs: Any)[source]¶
Bases:
RGBSpaceCIE XYZ relative to D50 (CSS
xyz-d50); identical to the PCS.- name = 'xyz-d50'¶
- class XYZD65Space(**kwargs: Any)[source]¶
Bases:
RGBSpaceCIE XYZ relative to D65 (CSS
xyz-d65), Bradford-adapted to D50.- name = 'xyz-d65'¶
- class GraySpace(gamma: float = 2.2, white: Any = None, **kwargs: Any)[source]¶
Bases:
ColorSpaceGrayscale with luminance
Y = g ** gammaalong 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'¶
- class LogGraySpace(black: float = 0.001, white: Any = None, **kwargs: Any)[source]¶
Bases:
ColorSpaceGrayscale with luminance
Y = black * (1 / black) ** galong 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'¶
- 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:
ColorSpaceOKLab (Ottosson 2020) on linear sRGB primaries, adapted to D50.
- name = '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:
ColorSpaceCylindrical OKLab (CSS
oklch); hue in degrees.- name = '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:
ColorSpaceCIE LCh(ab) relative to D50 (CSS
lch); hue in degrees.- name = '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:
ColorSpaceHSV over sRGB; hue in degrees (circular), saturation and value in 0..1.
- name = '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:
ColorSpaceHue (degrees), whiteness, blackness over sRGB (CSS Color 4); whiteness plus blackness above 1 is normalized to 1.
- name = '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:
ColorSpaceCMYK 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'¶