lcms2.ToneCurve¶
- class ToneCurve(source: float | ArrayLike | Any)[source]¶
Bases:
objectA one-dimensional tone curve mapping 0..1 to 0..1.
Build curves with the class methods
gamma(),parametric(),tabulated()or the presetssrgb(),rec709()andlinear(), 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
uint16or 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]
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
npoints.join(other[, n])Join two curves:
x -> other.inverse()(self(x)), sampled atnpoints.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
CMSErrorif 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 atnpoints.Attributes
True when the curve goes from high to low.
True when the curve is the identity.
True when the curve never decreases (or never increases).
True when the curve is built from several segments.
Parametric type (negative for inverses); 0 for tabulated curves.
The curve as a
uint16table (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:
- 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
npoints.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 atnpoints.This is the curve that, applied after
other, reproducesself; joining a curve with itself gives the identity. To apply one curve after another, usethen().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 atnpoints.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
CMSErrorif 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
LCMSErroris raised.
Examples
>>> from lcms2._curves import ToneCurve >>> round(ToneCurve.gamma(2.2).estimate_gamma(), 1) 2.2