rayoptics.util.colour_system module

converting a spectrum to a colour

code from the web blog:


@author: Christian Hill

xyz_from_xy(x, y)[source]

Return the vector (x, y, 1-x-y).

class ColourSystem(red, green, blue, white)[source]

Bases: object

A class representing a colour system.

A colour system defined by the CIE x, y and z=1-x-y coordinates of its three primary illuminants and its “white point”.

TODO: Implement gamma correction

path = PosixPath('/home/docs/checkouts/readthedocs.org/user_builds/ray-optics/checkouts/latest/src/rayoptics/util')
cmf = array([[1.4000e-03, 0.0000e+00, 6.5000e-03],        [2.2000e-03, 1.0000e-04, 1.0500e-02],        [4.2000e-03, 1.0000e-04, 2.0100e-02],        [7.6000e-03, 2.0000e-04, 3.6200e-02],        [1.4300e-02, 4.0000e-04, 6.7900e-02],        [2.3200e-02, 6.0000e-04, 1.1020e-01],        [4.3500e-02, 1.2000e-03, 2.0740e-01],        [7.7600e-02, 2.2000e-03, 3.7130e-01],        [1.3440e-01, 4.0000e-03, 6.4560e-01],        [2.1480e-01, 7.3000e-03, 1.0391e+00],        [2.8390e-01, 1.1600e-02, 1.3856e+00],        [3.2850e-01, 1.6800e-02, 1.6230e+00],        [3.4830e-01, 2.3000e-02, 1.7471e+00],        [3.4810e-01, 2.9800e-02, 1.7826e+00],        [3.3620e-01, 3.8000e-02, 1.7721e+00],        [3.1870e-01, 4.8000e-02, 1.7441e+00],        [2.9080e-01, 6.0000e-02, 1.6692e+00],        [2.5110e-01, 7.3900e-02, 1.5281e+00],        [1.9540e-01, 9.1000e-02, 1.2876e+00],        [1.4210e-01, 1.1260e-01, 1.0419e+00],        [9.5600e-02, 1.3900e-01, 8.1300e-01],        [5.8000e-02, 1.6930e-01, 6.1620e-01],        [3.2000e-02, 2.0800e-01, 4.6520e-01],        [1.4700e-02, 2.5860e-01, 3.5330e-01],        [4.9000e-03, 3.2300e-01, 2.7200e-01],        [2.4000e-03, 4.0730e-01, 2.1230e-01],        [9.3000e-03, 5.0300e-01, 1.5820e-01],        [2.9100e-02, 6.0820e-01, 1.1170e-01],        [6.3300e-02, 7.1000e-01, 7.8200e-02],        [1.0960e-01, 7.9320e-01, 5.7300e-02],        [1.6550e-01, 8.6200e-01, 4.2200e-02],        [2.2570e-01, 9.1490e-01, 2.9800e-02],        [2.9040e-01, 9.5400e-01, 2.0300e-02],        [3.5970e-01, 9.8030e-01, 1.3400e-02],        [4.3340e-01, 9.9500e-01, 8.7000e-03],        [5.1210e-01, 1.0000e+00, 5.7000e-03],        [5.9450e-01, 9.9500e-01, 3.9000e-03],        [6.7840e-01, 9.7860e-01, 2.7000e-03],        [7.6210e-01, 9.5200e-01, 2.1000e-03],        [8.4250e-01, 9.1540e-01, 1.8000e-03],        [9.1630e-01, 8.7000e-01, 1.7000e-03],        [9.7860e-01, 8.1630e-01, 1.4000e-03],        [1.0263e+00, 7.5700e-01, 1.1000e-03],        [1.0567e+00, 6.9490e-01, 1.0000e-03],        [1.0622e+00, 6.3100e-01, 8.0000e-04],        [1.0456e+00, 5.6680e-01, 6.0000e-04],        [1.0026e+00, 5.0300e-01, 3.0000e-04],        [9.3840e-01, 4.4120e-01, 2.0000e-04],        [8.5440e-01, 3.8100e-01, 2.0000e-04],        [7.5140e-01, 3.2100e-01, 1.0000e-04],        [6.4240e-01, 2.6500e-01, 0.0000e+00],        [5.4190e-01, 2.1700e-01, 0.0000e+00],        [4.4790e-01, 1.7500e-01, 0.0000e+00],        [3.6080e-01, 1.3820e-01, 0.0000e+00],        [2.8350e-01, 1.0700e-01, 0.0000e+00],        [2.1870e-01, 8.1600e-02, 0.0000e+00],        [1.6490e-01, 6.1000e-02, 0.0000e+00],        [1.2120e-01, 4.4600e-02, 0.0000e+00],        [8.7400e-02, 3.2000e-02, 0.0000e+00],        [6.3600e-02, 2.3200e-02, 0.0000e+00],        [4.6800e-02, 1.7000e-02, 0.0000e+00],        [3.2900e-02, 1.1900e-02, 0.0000e+00],        [2.2700e-02, 8.2000e-03, 0.0000e+00],        [1.5800e-02, 5.7000e-03, 0.0000e+00],        [1.1400e-02, 4.1000e-03, 0.0000e+00],        [8.1000e-03, 2.9000e-03, 0.0000e+00],        [5.8000e-03, 2.1000e-03, 0.0000e+00],        [4.1000e-03, 1.5000e-03, 0.0000e+00],        [2.9000e-03, 1.0000e-03, 0.0000e+00],        [2.0000e-03, 7.0000e-04, 0.0000e+00],        [1.4000e-03, 5.0000e-04, 0.0000e+00],        [1.0000e-03, 4.0000e-04, 0.0000e+00],        [7.0000e-04, 2.0000e-04, 0.0000e+00],        [5.0000e-04, 2.0000e-04, 0.0000e+00],        [3.0000e-04, 1.0000e-04, 0.0000e+00],        [2.0000e-04, 1.0000e-04, 0.0000e+00],        [2.0000e-04, 1.0000e-04, 0.0000e+00],        [1.0000e-04, 0.0000e+00, 0.0000e+00],        [1.0000e-04, 0.0000e+00, 0.0000e+00],        [1.0000e-04, 0.0000e+00, 0.0000e+00],        [0.0000e+00, 0.0000e+00, 0.0000e+00]])
xyz_to_rgb(xyz, out_fmt=None)[source]

Transform from xyz to rgb representation of colour.

The output rgb components are normalized on their maximum value. If xyz is out the rgb gamut, it is desaturated until it comes into gamut.

By default, fractional rgb components are returned; if out_fmt=’html’, the HTML hex string ‘#rrggbb’ is returned.


Convert from fractional rgb values to HTML-style hex string.


Convert a spectrum to an xyz point.

The spectrum must be on the same grid of points as the colour-matching function, self.cmf: 380-780 nm in 5 nm steps.


Convert a wavelength (nm) to an xyz point.

The wavelength must be on the same grid of points as the colour-matching function, self.cmf: 380-780 nm in 5 nm steps.

spec_to_rgb(spec, out_fmt=None)[source]

Convert a spectrum to an rgb value.

wvl_to_rgb(wv, out_fmt=None)[source]

Convert a wavelength to an rgb value.


Apply relative colorimetric gamut mapping to XYZ and return RGB

planck(lam, T)[source]

Returns the spectral radiance of a black body at temperature T.

Returns the spectral radiance, B(lam, T), in W.sr-1.m-2 of a black body at temperature T (in K) at a wavelength lam (in nm), using Planck’s law.