Coverage for colour/models/rgb/common.py: 100%
16 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-15 19:01 +1300
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-15 19:01 +1300
1"""
2Common RGB Colour Models Utilities
3==================================
5Define utilities for RGB colour models including transformations,
6chromaticity coordinates, and primaries matrices.
7"""
9from __future__ import annotations
11import typing
13from colour.colorimetry import CCS_ILLUMINANTS
15if typing.TYPE_CHECKING:
16 from colour.hints import LiteralChromaticAdaptationTransform
18from colour.hints import ( # noqa: TC001
19 ArrayLike,
20 Domain1,
21 Range1,
22)
23from colour.models.rgb import RGB_COLOURSPACES, RGB_to_XYZ, XYZ_to_RGB
25__author__ = "Colour Developers"
26__copyright__ = "Copyright 2013 Colour Developers"
27__license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause"
28__maintainer__ = "Colour Developers"
29__email__ = "colour-developers@colour-science.org"
30__status__ = "Production"
32__all__ = [
33 "XYZ_to_sRGB",
34 "sRGB_to_XYZ",
35]
38def XYZ_to_sRGB(
39 XYZ: Domain1,
40 illuminant: ArrayLike = CCS_ILLUMINANTS["CIE 1931 2 Degree Standard Observer"][
41 "D65"
42 ],
43 chromatic_adaptation_transform: (
44 LiteralChromaticAdaptationTransform | str | None
45 ) = "CAT02",
46 apply_cctf_encoding: bool = True,
47) -> Range1:
48 """
49 Convert from *CIE XYZ* tristimulus values to *sRGB* colourspace.
51 Parameters
52 ----------
53 XYZ
54 *CIE XYZ* tristimulus values.
55 illuminant
56 Source illuminant chromaticity coordinates.
57 chromatic_adaptation_transform
58 *Chromatic adaptation* transform.
59 apply_cctf_encoding
60 Whether to apply the *sRGB* encoding colour component transfer
61 function / inverse electro-optical transfer function.
63 Returns
64 -------
65 :class:`numpy.ndarray`
66 *sRGB* colour array.
68 Notes
69 -----
70 +------------+-----------------------+---------------+
71 | **Domain** | **Scale - Reference** | **Scale - 1** |
72 +============+=======================+===============+
73 | ``XYZ`` | 1 | 1 |
74 +------------+-----------------------+---------------+
76 +------------+-----------------------+---------------+
77 | **Range** | **Scale - Reference** | **Scale - 1** |
78 +============+=======================+===============+
79 | ``RGB`` | 1 | 1 |
80 +------------+-----------------------+---------------+
82 Examples
83 --------
84 >>> import numpy as np
85 >>> XYZ = np.array([0.20654008, 0.12197225, 0.05136952])
86 >>> XYZ_to_sRGB(XYZ) # doctest: +ELLIPSIS
87 array([ 0.7057393..., 0.1924826..., 0.2235416...])
88 """
90 return XYZ_to_RGB(
91 XYZ,
92 RGB_COLOURSPACES["sRGB"],
93 illuminant,
94 chromatic_adaptation_transform,
95 apply_cctf_encoding,
96 )
99def sRGB_to_XYZ(
100 RGB: Domain1,
101 illuminant: ArrayLike = CCS_ILLUMINANTS["CIE 1931 2 Degree Standard Observer"][
102 "D65"
103 ],
104 chromatic_adaptation_transform: (
105 LiteralChromaticAdaptationTransform | str | None
106 ) = "CAT02",
107 apply_cctf_decoding: bool = True,
108) -> Range1:
109 """
110 Convert from *sRGB* colourspace to *CIE XYZ* tristimulus values.
112 Parameters
113 ----------
114 RGB
115 *sRGB* colourspace array.
116 illuminant
117 Source illuminant chromaticity coordinates.
118 chromatic_adaptation_transform
119 *Chromatic adaptation* transform.
120 apply_cctf_decoding
121 Whether to apply the *sRGB* decoding colour component transfer
122 function / electro-optical transfer function.
124 Returns
125 -------
126 :class:`numpy.ndarray`
127 *CIE XYZ* tristimulus values.
129 Notes
130 -----
131 +------------+-----------------------+---------------+
132 | **Domain** | **Scale - Reference** | **Scale - 1** |
133 +============+=======================+===============+
134 | ``RGB`` | 1 | 1 |
135 +------------+-----------------------+---------------+
137 +------------+-----------------------+---------------+
138 | **Range** | **Scale - Reference** | **Scale - 1** |
139 +============+=======================+===============+
140 | ``XYZ`` | 1 | 1 |
141 +------------+-----------------------+---------------+
143 Examples
144 --------
145 >>> import numpy as np
146 >>> RGB = np.array([0.70573936, 0.19248266, 0.22354169])
147 >>> sRGB_to_XYZ(RGB) # doctest: +ELLIPSIS
148 array([ 0.2065429..., 0.1219794..., 0.0513714...])
149 """
151 return RGB_to_XYZ(
152 RGB,
153 RGB_COLOURSPACES["sRGB"],
154 illuminant,
155 chromatic_adaptation_transform,
156 apply_cctf_decoding,
157 )