lcms2.ToneCurve

class ToneCurve(source: float | ArrayLike | Any)[source]

Bases: object

A one-dimensional tone curve mapping 0..1 to 0..1.

Build curves with the class methods gamma(), parametric(), tabulated() or the presets srgb(), rec709() and linear(), then evaluate them by calling the object on any array.

Parameters:

source (float or array_like) – A float builds a pure gamma curve; a 1-D uint16 or float array builds a tabulated curve. (Internal code also passes an existing curve handle.)

Examples

>>> import numpy as np
>>> from lcms2._curves import ToneCurve
>>> g = ToneCurve.gamma(2.0)
>>> g(np.array([0.0, 0.5, 1.0])).round(4).tolist()
[0.0, 0.25, 1.0]
__init__(source: float | ArrayLike | Any)[source]

Methods

__init__(source)

copy()

Independent duplicate of the curve.

estimate_gamma([precision])

Estimate the gamma that best matches the curve.

gamma(gamma)

Pure power-law curve y = x ** gamma.

inverse([n])

Inverse curve, analytic when possible, otherwise sampled at n points.

join(other[, n])

Join two curves: x -> other.inverse()(self(x)), sampled at n points.

linear()

The identity curve.

parametric(type, params)

Parametric curve of one of the ICC / LittleCMS types.

rec709()

The Rec.

smooth(lam)

Smoothed copy of the curve, for noisy tabulated curves (raises CMSError if the result would not be monotonic).

srgb()

The sRGB transfer curve (encoded to linear).

tabulated(values)

Curve interpolated from a table of samples evenly spaced on 0..1.

then(other[, n])

Compose two curves: x -> other(self(x)), sampled at n points.

Attributes

is_descending

True when the curve goes from high to low.

is_linear

True when the curve is the identity.

is_monotonic

True when the curve never decreases (or never increases).

is_multisegment

True when the curve is built from several segments.

parametric_type

Parametric type (negative for inverses); 0 for tabulated curves.

table

The curve as a uint16 table (4096 entries for parametric curves).

classmethod gamma(gamma: float) → ToneCurve[source]

Pure power-law curve y = x ** gamma.

Parameters:

gamma (float) – The exponent (2.2 is a typical display gamma).

Examples

>>> from lcms2._curves import ToneCurve
>>> round(float(ToneCurve.gamma(2.2)(0.5)), 4)
0.2176
classmethod parametric(type: int, params: ArrayLike) → ToneCurve[source]

Parametric curve of one of the ICC / LittleCMS types.

Parameters:
  • type (int) – 1 (gamma), 2 (CIE 122-1966), 3 (IEC 61966-3), 4 (sRGB-style), 5, 6, 7, 8, 108 or 109 (sigmoids). A negative type is the inverse curve.

  • params (sequence of float) – Parameters for the type; the required count is 1, 3, 4, 5, 7, 4, 5, 5, 1, 1 for types 1-8, 108, 109 respectively.

Raises:

ValueError – For an unknown type or the wrong number of parameters.

Examples

>>> from lcms2._curves import ToneCurve
>>> ToneCurve.parametric(1, [2.0])(0.5)
np.float64(0.25)
classmethod tabulated(values: ArrayLike) → ToneCurve[source]

Curve interpolated from a table of samples evenly spaced on 0..1.

Parameters:

values (array_like) – 1-D uint16 (0..65535) or floating point (0..1) samples, at least 2.

Examples

>>> import numpy as np
>>> from lcms2._curves import ToneCurve
>>> ToneCurve.tabulated(np.array([0.0, 0.25, 1.0]))(0.5)
np.float64(0.25)
classmethod srgb() → ToneCurve[source]

The sRGB transfer curve (encoded to linear).

Examples

>>> from lcms2._curves import ToneCurve
>>> round(float(ToneCurve.srgb()(0.5)), 4)
0.214
classmethod rec709() → ToneCurve[source]

The Rec. 709 transfer curve (encoded to linear).

Examples

>>> from lcms2._curves import ToneCurve
>>> round(float(ToneCurve.rec709()(1.0)), 4)
1.0
classmethod linear() → ToneCurve[source]

The identity curve.

Examples

>>> from lcms2._curves import ToneCurve
>>> ToneCurve.linear().is_linear
True
inverse(n: int = 4096) → ToneCurve[source]

Inverse curve, analytic when possible, otherwise sampled at n points.

Examples

>>> from lcms2._curves import ToneCurve
>>> round(float(ToneCurve.gamma(2.0).inverse()(0.25)), 4)
0.5
join(other: ToneCurve, n: int = 4096) → ToneCurve[source]

Join two curves: x -> other.inverse()(self(x)), sampled at n points.

This is the curve that, applied after other, reproduces self; joining a curve with itself gives the identity. To apply one curve after another, use then().

Examples

>>> from lcms2._curves import ToneCurve
>>> round(float(ToneCurve.gamma(2.0).join(ToneCurve.gamma(2.0))(0.3)), 3)
0.3
then(other: ToneCurve, n: int = 4096) → ToneCurve[source]

Compose two curves: x -> other(self(x)), sampled at n points.

Examples

>>> from lcms2._curves import ToneCurve
>>> c = ToneCurve.gamma(2.0).then(ToneCurve.gamma(1.5))
>>> round(float(c(0.5)), 3)
0.125
smooth(lam: float) → ToneCurve[source]

Smoothed copy of the curve, for noisy tabulated curves (raises CMSError if the result would not be monotonic).

Parameters:

lam (float) – Smoothing strength; larger is smoother (0.1 to 1000 is typical).

Examples

>>> import numpy as np
>>> from lcms2._curves import ToneCurve
>>> table = (np.linspace(0, 1, 64) ** 2.2 * 65535).astype(np.uint16)
>>> ToneCurve.tabulated(table).smooth(1.0).is_monotonic
True
copy() → ToneCurve[source]

Independent duplicate of the curve.

Examples

>>> from lcms2._curves import ToneCurve
>>> c = ToneCurve.gamma(2.0)
>>> c.copy() is c
False
estimate_gamma(precision: float = 0.01) → float[source]

Estimate the gamma that best matches the curve.

Parameters:

precision (float) – Maximum acceptable mean error. When no single gamma reaches it (as for the sRGB curve), the tolerance is relaxed step by step up to 0.5 before LCMSError is raised.

Examples

>>> from lcms2._curves import ToneCurve
>>> round(ToneCurve.gamma(2.2).estimate_gamma(), 1)
2.2
property is_linear: bool

True when the curve is the identity.

property is_monotonic: bool

True when the curve never decreases (or never increases).

property is_descending: bool

True when the curve goes from high to low.

property is_multisegment: bool

True when the curve is built from several segments.

property parametric_type: int

Parametric type (negative for inverses); 0 for tabulated curves.

property table: ndarray

The curve as a uint16 table (4096 entries for parametric curves).