lcms2.colorimetry

Colorimetry: color differences, white points, CIECAM02 and basic color space conversions.

Every function works on NumPy arrays whose last axis holds the three color components, broadcasts between operands, and never modifies its inputs. XYZ values use the scale where the white point has Y = 1 (so D50 is about (0.9642, 1, 0.8249)).

class CIECAM02(white_point: ArrayLike, adapting_luminance: float = 64.0, background: float = 20.0, surround: str = 'average', degree: float | str = 'auto')[source]

Bases: object

CIECAM02 color appearance model under fixed viewing conditions.

Parameters:
  • white_point (array_like) – XYZ of the adopted white, with Y = 1 (for example D65).

  • adapting_luminance (float) – Luminance of the adapting field La in cd/m2 (typically 20% of the white’s).

  • background (float) – Relative luminance of the background Yb in percent of the white (20 is typical).

  • surround ({"average", "dim", "dark", "cutsheet"}) – Viewing surround.

  • degree (float or "auto") – Degree of adaptation D in 0..1, or "auto" to compute it from La and the surround.

Examples

>>> from lcms2.colorimetry import CIECAM02, D65
>>> cam = CIECAM02(D65, adapting_luminance=64, background=20)
>>> round(float(cam.forward(D65)[0]), 2)
100.0
forward(xyz: ArrayLike) → ndarray[source]

XYZ (Y = 1 scale) to JCh (lightness, chroma, hue in degrees).

Examples

>>> from lcms2.colorimetry import CIECAM02, D65
>>> CIECAM02(D65, 64).forward([[0.2, 0.2, 0.2], D65]).shape
(2, 3)
reverse(jch: ArrayLike) → ndarray[source]

JCh back to XYZ (Y = 1 scale).

Examples

>>> import numpy as np
>>> from lcms2.colorimetry import CIECAM02, D65
>>> cam = CIECAM02(D65, 64)
>>> bool(np.allclose(cam.reverse(cam.forward([0.2, 0.3, 0.4])), [0.2, 0.3, 0.4], atol=1e-4))
True
adapt(xyz: ArrayLike, src_white: ArrayLike, dst_white: ArrayLike, method: str = 'bradford') → ndarray[source]

Chromatic adaptation of XYZ colors from one white point to another.

Parameters:
  • xyz (array_like) – XYZ of shape (..., 3).

  • src_white (array_like) – XYZ of the source and destination whites.

  • dst_white (array_like) – XYZ of the source and destination whites.

  • method ({"bradford", "von_kries", "xyz_scaling"}) – Adaptation transform.

Examples

>>> from lcms2.colorimetry import D50, D65, adapt
>>> adapt(D65, D65, D50).round(4).tolist()
[0.9642, 1.0, 0.8249]
adaptation_matrix(src_white: ArrayLike, dst_white: ArrayLike, method: str = 'bradford') → ndarray[source]

The 3x3 chromatic adaptation matrix between two white points.

Parameters:
  • src_white (array_like) – XYZ of the source and destination whites.

  • dst_white (array_like) – XYZ of the source and destination whites.

  • method ({"bradford", "von_kries", "xyz_scaling"}) – Adaptation transform.

Returns:

numpy.ndarray – Matrix M of shape (3, 3) such that M @ xyz adapts a single color from src_white to dst_white.

Raises:

ValueError – If method is unknown.

Examples

>>> from lcms2.colorimetry import D50, D65, adaptation_matrix
>>> (adaptation_matrix(D65, D50) @ D65).round(4).tolist()
[0.9642, 1.0, 0.8249]
delta_e(lab1: ArrayLike, lab2: ArrayLike, method: str = '2000', **params: float) → ndarray[source]

Color difference between CIELAB colors.

Parameters:
  • lab1 (array_like) – Lab values of shape (..., 3); the two operands broadcast against each other.

  • lab2 (array_like) – Lab values of shape (..., 3); the two operands broadcast against each other.

  • method ({"2000", "76", "94", "cmc", "bfd"}) – CIEDE2000 (default), CIE76 (Euclidean), CIE94, CMC l:c or BFD. A leading “de” or “cie” is accepted (“de2000”).

  • **params – Kl, Kc, Kh (all default 1) for "2000"; l (2) and c (1) for "cmc".

Returns:

numpy.ndarray – Differences with the broadcast shape minus the last axis (a scalar for one pair).

Raises:
  • ValueError – For an unknown method or an array whose last axis is not 3.

  • TypeError – For a parameter the chosen method does not take.

Examples

>>> from lcms2.colorimetry import delta_e
>>> round(float(delta_e([50, 2.6772, -79.7751], [50, 0, -82.7485])), 4)
2.0425
>>> float(delta_e([50, 0, 0], [53, 4, 0], method="76"))
5.0
lab_to_lch(lab: ArrayLike) → ndarray[source]

CIELAB to LCh (hue in degrees, 0..360).

Examples

>>> from lcms2.colorimetry import lab_to_lch
>>> lab_to_lch([50, 0, 10]).round(3).tolist()
[50.0, 10.0, 90.0]
lab_to_xyz(lab: ArrayLike, white: ArrayLike | None = None) → ndarray[source]

CIELAB to XYZ; inverse of xyz_to_lab().

Examples

>>> from lcms2.colorimetry import lab_to_xyz
>>> lab_to_xyz([100, 0, 0]).round(4).tolist()
[0.9642, 1.0, 0.8249]
lch_to_lab(lch: ArrayLike) → ndarray[source]

LCh to CIELAB; inverse of lab_to_lch().

Examples

>>> from lcms2.colorimetry import lch_to_lab
>>> lch_to_lab([50, 10, 90]).round(3).tolist()
[50.0, 0.0, 10.0]
temperature_from_white_point(xyy: ArrayLike) → ndarray[source]

Correlated color temperature of a white point.

Parameters:

xyy (array_like) – xyY of shape (..., 3), or just (x, y) of shape (..., 2).

Returns:

numpy.ndarray – Temperature in kelvin (a scalar for a single white point).

Examples

>>> from lcms2.colorimetry import D65_xyY, temperature_from_white_point
>>> round(float(temperature_from_white_point(D65_xyY)))
6504
white_point_from_temperature(kelvin: ArrayLike) → ndarray[source]

Chromaticity of the CIE daylight illuminant at a correlated color temperature.

Parameters:

kelvin (float or array_like) – Temperature in kelvin, 4000 to 25000.

Returns:

numpy.ndarray – xyY with Y = 1; shape (3,) for a scalar, (..., 3) for an array.

Raises:

ValueError – If a temperature is outside 4000-25000 K.

Examples

>>> from lcms2.colorimetry import white_point_from_temperature
>>> white_point_from_temperature(6504).round(3).tolist()
[0.313, 0.329, 1.0]
xyy_to_xyz(xyy: ArrayLike) → ndarray[source]

xyY to XYZ. A zero y gives black.

Examples

>>> from lcms2.colorimetry import D65_xyY, xyy_to_xyz
>>> xyy_to_xyz(D65_xyY).round(4).tolist()
[0.9505, 1.0, 1.0891]
xyz_to_lab(xyz: ArrayLike, white: ArrayLike | None = None) → ndarray[source]

XYZ to CIELAB (same formulas as LittleCMS).

Parameters:
  • xyz (array_like) – XYZ of shape (..., 3) with Y = 1 at the white.

  • white (array_like, optional) – Reference white XYZ; defaults to D50.

Examples

>>> from lcms2.colorimetry import D50, xyz_to_lab
>>> xyz_to_lab(D50).round(6).tolist()
[100.0, 0.0, 0.0]
xyz_to_xyy(xyz: ArrayLike) → ndarray[source]

XYZ to xyY. Black (XYZ all zero) gets the D50 chromaticity.

Examples

>>> from lcms2.colorimetry import D65, xyz_to_xyy
>>> xyz_to_xyy(D65).round(4).tolist()
[0.3127, 0.329, 1.0]