colour.convert

colour.convert(a, source, target, **kwargs)[source]

Converts given object \(a\) from source colour representation to target colour representation using the automatic colour conversion graph.

The conversion is performed by finding the shortest path in a NetworkX DiGraph class instance.

The conversion path adopts the ‘1’ domain-range scale and the object \(a\) is expected to be soft normalised accordingly. For example, CIE XYZ tristimulus values arguments for use with the CAM16 colour appearance model should be in domain [0, 1] instead of the domain [0, 100] used with the ‘Reference’ domain-range scale. The arguments are typically converted as follows:

  • Scalars in domain-range [0, 10], e.g Munsell Value are scaled by 10.

  • Percentages in domain-range [0, 100] are scaled by 100.

  • Degrees in domain-range [0, 360] are scaled by 360.

  • Integers in domain-range [0, 2**n -1] where n is the bit depth are scaled by 2**n -1.

See the Domain-Range Scales page for more information.

Parameters
  • a (array_like or numeric or SpectralDistribution) – Object \(a\) to convert. If \(a\) represents a reflectance, transmittance or absorptance value, the expectation is that it is viewed under CIE Standard Illuminant D Series D65. The illuminant can be changed on a per definition basis along the conversion path.

  • source (unicode) – Source colour representation, i.e. the source node in the automatic colour conversion graph.

  • target (unicode) – Target colour representation, i.e. the target node in the automatic colour conversion graph.

Other Parameters

**kwargs (dict, optional) – {‘*’}, Please refer to the documentation of the supported conversion definitions.

Arguments for the conversion definitions are passed as keyword arguments whose names is those of the conversion definitions and values set as dictionaries. For example, in the conversion from spectral distribution to sRGB colourspace, passing arguments to the colour.sd_to_XYZ() definition is done as follows:

convert(sd, 'Spectral Distribution', 'sRGB', sd_to_XYZ={'illuminant': ILLUMINANTS_SDS['FL2']})

It is also possible to pass keyword arguments directly to the various conversion definitions irrespective of their name. This is dangerous and could cause unexpected behaviour because of unavoidable discrepancies with the underlying colour.utilities.filter_kwargs() definition between Python 2.7 and 3.x. Using this direct keyword arguments passing mechanism might also ends up passing incompatible arguments to a given conversion definition. Consider the following conversion:

convert(sd, 'Spectral Distribution', 'sRGB', 'illuminant': ILLUMINANTS_SDS['FL2'])

Because both the colour.sd_to_XYZ() and colour.XYZ_to_sRGB() definitions have an illuminant argument, ILLUMINANTS_SDS[‘FL2’] will be passed to both of them and will raise an exception in the colour.XYZ_to_sRGB() definition. This will be addressed in the future by either catching the exception and trying a new time without the keyword argument or more elegantly via type checking.

With that in mind, this mechanism offers some good benefits: For example, it allows defining a conversion from CIE XYZ colourspace to n different colour models while passing an illuminant argument but without having to explicitly define all the explicit conversion definition arguments:

a = np.array([0.20654008, 0.12197225, 0.05136952])
illuminant = ILLUMINANTS['CIE 1931 2 Degree Standard Observer']['D65']
for model in ('CIE xyY', 'CIE Lab'):
    convert(a, 'CIE XYZ', model, illuminant=illuminant)

Instead of:

for model in ('CIE xyY', 'CIE Lab'):
    convert(a, 'CIE XYZ', model, XYZ_to_xyY={'illuminant': illuminant}, XYZ_to_Lab={'illuminant': illuminant})

Mixing both approaches is possible for the brevity benefits. It is made possible because the keyword arguments directly passed are filtered first and then the resulting dict is updated with the explicit conversion definition arguments:

illuminant = ILLUMINANTS['CIE 1931 2 Degree Standard Observer']['D65']
 convert(sd, 'Spectral Distribution', 'sRGB', 'illuminant': ILLUMINANTS_SDS['FL2'], XYZ_to_sRGB={'illuminant': illuminant})

For inspection purposes, verbose is enabled by passing arguments to the colour.describe_conversion_path() definition via the verbose keyword argument as follows:

convert(sd, 'Spectral Distribution', 'sRGB', verbose={'mode': 'Long'})
Returns

Converted object \(a\).

Return type

ndarray or numeric or SpectralDistribution

Warning

The domain-range scale is ‘1’ and cannot be changed.

Notes

  • Various defaults have been systematically adopted compared to the low-level Colour API:

    • The default illuminant for the computation is CIE Standard Illuminant D Series D65. It can be changed on a per definition basis along the conversion path.

    • The default RGB colourspace is the sRGB colourspace. It can also be changed on a per definition basis along the conversion path.

    • Most of the colour appearance models have defaults set according to IEC 61966-2-1:1999 viewing conditions, i.e. sRGB 64 Lux ambiant illumination, 80 \(cd/m^2\), adapting field luminance about 20% of a white object in the scene.

  • The RGB colour representation is assumed to be linear and representing scene-referred imagery. To convert such RGB values to output-referred (display-referred) imagery, i.e. encode the RGB values using an encoding colour component transfer function (Encoding CCTF) / opto-electronic transfer function (OETF / OECF), the Output-Referred RGB representation must be used.

Examples

>>> from colour import COLOURCHECKERS_SDS
>>> sd = COLOURCHECKERS_SDS['ColorChecker N Ohta']['dark skin']
>>> convert(sd, 'Spectral Distribution', 'sRGB',
...     verbose={'mode': 'Short', 'width': 75})
... 
===========================================================================
*                                                                         *
*   [ Conversion Path ]                                                   *
*                                                                         *
*   "sd_to_XYZ" --> "XYZ_to_sRGB"                                         *
*                                                                         *
===========================================================================
array([ 0.4567579...,  0.3098698...,  0.2486192...])
>>> illuminant = ILLUMINANTS_SDS['FL2']
>>> convert(sd, 'Spectral Distribution', 'sRGB',
...     sd_to_XYZ={'illuminant': illuminant})
... 
array([ 0.4792457...,  0.3167696...,  0.1736272...])
>>> a = np.array([0.45675795, 0.30986982, 0.24861924])
>>> convert(a, 'Output-Referred RGB', 'CAM16UCS')
... 
array([ 0.3999481...,  0.0920655...,  0.0812752...])
>>> a = np.array([0.39994811, 0.09206558, 0.08127526])
>>> convert(a, 'CAM16UCS', 'sRGB', verbose={'mode': 'Short', 'width': 75})
... 
===========================================================================
*                                                                         *
*   [ Conversion Path ]                                                   *
*                                                                         *
*   "UCS_Luo2006_to_JMh_CIECAM02" --> "JMh_CAM16_to_CAM16" -->            *
*   "CAM16_to_XYZ" --> "XYZ_to_sRGB"                                      *
*                                                                         *
===========================================================================
array([ 0.4567576...,  0.3098826...,  0.2486222...])