dtmm.color

Color conversion functions and utilities.

CMF creation function

Color conversion

Module Contents

dtmm.color.apply_gamma(value, gamma)

apply_gamma(value, gamma)

Applies standard gamma function (transfer function) to the given (linear) data.

Parameters
  • value (float) – Input value

  • gamma (float) – Gamma factor

dtmm.color.apply_srgb_gamma(value)

apply_srgb_gamma(value)

Applies sRGB gamma function (transfer function) to the given (linear) data.

dtmm.color.xyz2srgb(xyz, rgb)

xyz2srgb(xyz)

Converts XYZ value to RGB value based on the sRGB working space with D65 reference white.

dtmm.color.xyz2gray(xyz, gray)

xyz2gray(xyz)

Converts XYZ value to Gray color

dtmm.color.spec2xyz(spec, cmf, xyz)

spec2xyz(spec,cmf)

Converts specter array to xyz value.

Parameters
  • spec (array_like) – Input specter data

  • cmf (array_like) – Color matching function

Returns

xyz – Computed xyz value.

Return type

ndarray

dtmm.color.specter2color(spec, cmf, norm=False, gamma=True, gray=False, out=None)

Converts specter data to RGB data (color or gray).

Specter shape must be […,k], where wavelengths are in the last axis. cmf must be a valid color matchin function array of size [k,3].

Parameters
  • spec (array) – Specter data of shape […, n] where each data element in the array has n wavelength values

  • cmf (array) – A color matching function (array of shape [n,3]) that converts the specter data to a XYZ color.

  • norm (bool or float, optional) – If set to False, no data normalization is performed (default). If True, internally, xyz data is normalized in the range [0,1.], so that no clipping occurs. If it is a float, data is normalized to this value.

  • gamma (bool or float, optional) – If gamma is True srgb gamma function is applied (default). If float is provided, standard gamma factor is applied with a given gamma value. If False, no gamma correction is performed.

  • gray (bool, optional) – Whether gray output is calculated (color by default)

  • out (array, optional) – Output array of shape (…,3)

Returns

rgb – A computed RGB value.

Return type

ndarray

Notes

Numpy broadcasting rules apply to spec and cmf.

Example

>>> cmf = load_tcmf()
>>> specter2color([1]*81, cmf)#should be close to 1,1,1
... 
array([0.99994901, 1.        , 0.99998533])
dtmm.color.srf2cmf(srf, out=None)

Converts spectral response function (Y) to color matching function (XYZ).

Parameters
  • srff (array_like) – Spectral response function of shape (…,n)

  • out (ndarray, optional) – Output array.

Returns

cmf – A color matching function array of shape (…,n,3)

Return type

ndarray

dtmm.color.normalize_specter(spec, cmf, out=None)

Normalizes specter based on the color matching function. (cmf array) so that calculated Y value is 1.

Parameters
  • spec (array_like) – Input illuminant specter data of shape (…,n).

  • cmf (array_like) – Color matching function of shape (…,n,3).

  • out (ndarray, optional) – Output array.

Returns

normalized_spec – A normalized version of the input specter

Return type

ndarray

Notes

Numpy broadcasting rules apply to spec and cmf.

dtmm.color.load_tcmf(wavelengths=None, illuminant='D65', cmf=CMF, norm=True, retx=False, single_wavelength=False)

Loads transmission color matching function.

This functions loads a CIE XYZ color matching function and transforms it to a transmission color matching function for a given illuminant. Resulting CMF matrix will transform unity into white color.

Parameters
  • wavelengths (array_like, optional) – Wavelengths at which data is computed. If not specified (default), original data from the 5nm tabulated data is returned.

  • illuminant (str, optional) – Name of the standard illuminant or path to illuminant data.

  • cmf (str, optional) – Name or path to the cmf function. Can be ‘CIE1931’ for CIE 1931 2-deg 5nm tabulated data, ‘CIE1964’ for CIE1964 10-deg 5nm tabulatd data, or ‘CIE2006-2’ or ‘CIE2006-10’ for a proposed CIE 2006 2- or 10-deg 5nm tabulated data.

  • norm (int, optional) – By default cmf is normalized so that unity transmission value over the full spectral range of the illuminant is converted to XYZ color with Y=1. You can disable this by setting norm = 0. If you set norm = 2, then the cmf is normalized for the interpolated spectra at given wavelengths, and not to the full bandwidth of the spectra (norm = 1).

  • retx (bool, optional) – Should the selected wavelengths be returned as well.

  • single_wavelength (bool, optional) – If specified, color matching function for single wavelengths specter is calculated by interpolation. By default, specter is assumed to be a piece-wise linear function and continuous between the specified wavelengts, and data is integrated instead.

Returns

cmf – Color matching function array of shape [n,3] or a tuple of (x,cmf) if retx is specified.

Return type

array

Example

>>> cmf = load_tcmf()
>>> specter2color([1]*81, cmf) #should be close to 1,1,1
... 
array([0.99994901, 1.        , 0.99998533])
dtmm.color.cmf2tcmf(cmf, spec, norm=True, out=None)

Converts CMF table to specter-normalized transmission CMF table

Parameters
  • cmf (array_like) – Color matchinf function array

  • spec (array_like) – Illuminant specter array

  • norm (bool, optional) – Whether to normalize illuminant specter before constructing the CMF.

  • out (ndarray, optional) – Output array

Returns

out – A transmission color matching function array.

Return type

ndarray

Notes

Numpy broadcasting rules apply to spec and cmf.

dtmm.color.load_specter(wavelengths=None, illuminant='D65', retx=False)

Loads illuminant specter data from file.

Parameters
  • wavelengths (array_like, optional) – Wavelengths at which data is interpolated

  • illuminant (str, or array, optional) – Name of the standard illuminant or filename. If specified as array, it must be an array of shape (n,2). The first column decribes wavelength and the second is the intensity.

  • retx (bool, optional) – Should the selected wavelengths be returned as well.

Returns

specter – Specter array of shape [num] or a tuple of (x,specter) if retx is specified

Return type

array

Examples

#D65 evaluated at three wavelengths >>> spec = load_specter((450,500,550), “D65”)

#illuminant with triangular specter evaluated at three wavelengths >>> spec = load_specter([450,475,500,], illuminant = [[400,0],[500,1],[600,0]])

dtmm.color.load_cmf(wavelengths=None, cmf=CMF, retx=False, single_wavelength=False)

Load XYZ Color Matching function.

This function loads tabulated data and re-calculates xyz array on a given range of wavelength values.

See also load_tcmf.

Parameters
  • wavelengths (array_like, optional) – A 1D array of wavelengths at which data is computed. If not specified (default), original data from the 5nm tabulated data is returned.

  • cmf (str, optional) – Name or path to the cmf function. Can be ‘CIE1931’ for CIE 1931 2-deg 5nm tabulated data, ‘CIE1964’ for CIE1964 10-deg 5nm tabulated data, or ‘CIE2006-2’ or ‘CIE2006-10’ for a proposed CIE 2006 2- or 10-deg 5nm tabulated data. For grayscale cameras, there is a ‘CMOS’ spectral response data. You can also provide ‘FLAT’ for flat (unity) response function.

  • retx (bool, optional) – Should the selected wavelengths be returned as well.

  • single_wavelength (bool, optional) – If specified, color matching function for single wavelengths specter is calculated by interpolation. By default, specter is assumed to be a piece-wise linear function and continuous between the specified wavelengts, and data is integrated instead.

Returns

cmf – Color matching function array of shape [n,3] or a tuple of (x,cmf) if retx is specified.

Return type

array