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

16 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-11-16 22:49 +1300

1""" 

2Common RGB Colour Models Utilities 

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

4 

5Define utilities for RGB colour models including transformations, 

6chromaticity coordinates, and primaries matrices. 

7""" 

8 

9from __future__ import annotations 

10 

11import typing 

12 

13from colour.colorimetry import CCS_ILLUMINANTS 

14 

15if typing.TYPE_CHECKING: 

16 from colour.hints import LiteralChromaticAdaptationTransform 

17 

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 

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 

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. 

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 

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. 

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 )