Coverage for models/rgb/common.py: 12%

1"""

2 Common RGB Colour Models Utilities

3==================================

4

5 Define utilities for RGB colour models including transformations,

6 chromaticity coordinates, and primaries matrices.

7"""

8

9 from __future__ import annotations

10

11 import typing

12

13 from colour.colorimetry import CCS_ILLUMINANTS

14

15 if typing.TYPE_CHECKING:

16 from colour.hints import LiteralChromaticAdaptationTransform

17

18 from colour.hints import ( # noqa: TC001

19 ArrayLike,

20 Domain1,

21 Range1,

22)

23 from colour.models.rgb import RGB_COLOURSPACES, RGB_to_XYZ, XYZ_to_RGB

24

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"

31

32 __all__ = [

33 "XYZ_to_sRGB",

34 "sRGB_to_XYZ",

35]

36

37

38 def 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.

50

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.

62

63 Returns

64 -------

65 :class:`numpy.ndarray`

66 *sRGB* colour array.

67

68 Notes

69 -----

70 +------------+-----------------------+---------------+

71 | **Domain** | **Scale - Reference** | **Scale - 1** |

72 +============+=======================+===============+

73 | ``XYZ`` | 1 | 1 |

74 +------------+-----------------------+---------------+

75

76 +------------+-----------------------+---------------+

77 | **Range** | **Scale - Reference** | **Scale - 1** |

78 +============+=======================+===============+

79 | ``RGB`` | 1 | 1 |

80 +------------+-----------------------+---------------+

81

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 """

89

90 return XYZ_to_RGB(

91 XYZ,

92 RGB_COLOURSPACES["sRGB"],

93 illuminant,

94 chromatic_adaptation_transform,

95 apply_cctf_encoding,

96 )

97

98

99 def 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.

111

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.

123

124 Returns

125 -------

126 :class:`numpy.ndarray`

127 *CIE XYZ* tristimulus values.

128

129 Notes

130 -----

131 +------------+-----------------------+---------------+

132 | **Domain** | **Scale - Reference** | **Scale - 1** |

133 +============+=======================+===============+

134 | ``RGB`` | 1 | 1 |

135 +------------+-----------------------+---------------+

136

137 +------------+-----------------------+---------------+

138 | **Range** | **Scale - Reference** | **Scale - 1** |

139 +============+=======================+===============+

140 | ``XYZ`` | 1 | 1 |

141 +------------+-----------------------+---------------+

142

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 """

150

151 return RGB_to_XYZ(

152 RGB,

153 RGB_COLOURSPACES["sRGB"],

154 illuminant,

155 chromatic_adaptation_transform,

156 apply_cctf_decoding,

157 )