cutcutcodec.core.colorspace

Manage color spaces.

\[\begin{split}\begin{pmatrix} y' \\ p_b \\ p_r \end{pmatrix}_{p,t} \overset{T_1}\longleftrightarrow \begin{pmatrix} r' \\ g' \\ b' \end{pmatrix}_{p,t} \overset{T_2}\longleftrightarrow \begin{pmatrix} r \\ g \\ b \end{pmatrix}_{p} \overset{T_3}\longleftrightarrow \begin{pmatrix} x \\ y \\ z \end{pmatrix}\end{split}\]
With:
  • \(p\) is the color primaries (gamut), tristimulus values defined in the cst module cutcutcodec.core.colorspace.cst as the PRIMARIES constant.

  • \(t\) is the transfere function (gamma), defined in the cst module cutcutcodec.core.colorspace.cst as the TRC constant.

  • \(\begin{pmatrix} y' \\ p_b \\ p_r \end{pmatrix}_{p,t}\) is the color space in which video is encoded in yuv pixel format. \(y' \in [0, 1]\) is the luma, \(p_b \in \left[-\frac{1}{2}, \frac{1}{2}\right]\) the blue difference and \(p_r \in \left[-\frac{1}{2}, \frac{1}{2}\right]\) the red difference. As the human eye perceives differences in luminosity better in dark values than in bright ones, \(y'\) stretches low values in order to encode dark pixels more accurately The \(u\) and \(v\) values encode chrominance, which is less important than luminance for the human eye. So they are encoded with fewer bits than \(y'\)

  • \(\begin{pmatrix} r' \\ g' \\ b' \end{pmatrix}_{p,t}\) is the gamma corrected color space expected by terminals (screen, TV, printer, etc…). For example, this is the space expected by matplotlib’s imshow function. \(r' \in [0, 1]\) is the non-linear red, \(g' \in [0, 1]\) the non-linear green and \(b' \in [0, 1]\) the non-linear blue. As this space depends on a primaries \(p\) and a transfer function \(t\), we have to choose the one expected by the terminal to obtain a faithful color representation.

  • \(\begin{pmatrix} r \\ g \\ b \end{pmatrix}_{p}\) is the linear workspace in which all cutcutcodec operations are performed. \(r \in [0, 1]\) is the linear red, \(g \in [0, 1]\) the linear green and \(b \in [0, 1]\) the linear blue. Unlike the non-linear rgb space, color mixing (and many other physics laws) are correct in this space only.

  • \(\begin{pmatrix} x \\ y \\ z \end{pmatrix}_{p}\) is the absolute CIE 1931 reference space, independent of any transfer function \(t\) and any primaries \(p\). As soon as you change your transfer function or color primaries, you pass through this space. \((x, y, z) \in \mathbb{R}^3\)

  • \(T_1\) is a linear transformation that depends on the primaries \(p\), implemented in cutcutcodec.core.colorspace.func.rgb2yuv_matrix_from_kr_kb().

  • \(T_2\) is the transformation that applies the non-linear transfer function \(t\) to each red, green and blue component.

  • \(T_3\) is a linear transformation that depends on the primaries \(p\), implemented in cutcutcodec.core.colorspace.func.rgb2xyz_matrix_from_chroma().

Here are some other sources that talk about color spaces:

And here’s a list of color calculators:

Details

Modules

cst

Regroup all standard color space constants.

func

Mathematical functions to switching from a color space to another.

heuristic

Estimate missing information.