Coverage for colour/models/rgb/tests/test_hanbury2003.py: 100%

65 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-11-15 19:01 +1300

1"""Defines unit tests for :mod:`colour.models.rgb.hanbury2003` module.""" 

2 

3from __future__ import annotations 

4 

5from itertools import product 

6 

7import numpy as np 

8 

9from colour.constants import TOLERANCE_ABSOLUTE_TESTS 

10from colour.models.rgb import IHLS_to_RGB, RGB_to_IHLS 

11from colour.utilities import domain_range_scale, ignore_numpy_errors 

12 

13__author__ = "Colour Developers" 

14__copyright__ = "Copyright 2013 Colour Developers" 

15__license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause" 

16__maintainer__ = "Colour Developers" 

17__email__ = "colour-developers@colour-science.org" 

18__status__ = "Production" 

19 

20__all__ = [ 

21 "TestRGB_to_IHLS", 

22 "TestIHLS_to_RGB", 

23] 

24 

25 

26class TestRGB_to_IHLS: 

27 """ 

28 Define :func:`colour.models.rgb.hanbury2003.RGB_to_IHLS` definition unit 

29 tests methods. 

30 """ 

31 

32 def test_RGB_to_IHLS(self) -> None: 

33 """Test :func:`colour.models.rgb.hanbury2003.RGB_to_IHLS` definition.""" 

34 

35 np.testing.assert_allclose( 

36 RGB_to_IHLS(np.array([0.45620519, 0.03081071, 0.04091952])), 

37 np.array([6.26236117, 0.12197943, 0.42539448]), 

38 atol=TOLERANCE_ABSOLUTE_TESTS, 

39 ) 

40 

41 np.testing.assert_allclose( 

42 RGB_to_IHLS(np.array([0.00000000, 0.00000000, 0.00000000])), 

43 np.array([0.00000000, 0.00000000, 0.00000000]), 

44 atol=TOLERANCE_ABSOLUTE_TESTS, 

45 ) 

46 

47 np.testing.assert_allclose( 

48 RGB_to_IHLS(np.array([1.00000000, 1.00000000, 1.00000000])), 

49 np.array([0.00000000, 1.00000000, 0.00000000]), 

50 atol=TOLERANCE_ABSOLUTE_TESTS, 

51 ) 

52 

53 def test_n_dimensional_RGB_to_IHLS(self) -> None: 

54 """ 

55 Test :func:`colour.models.rgb.hanbury2003.RGB_to_IHLS` definition 

56 n-dimensional arrays support. 

57 """ 

58 

59 RGB = np.array([0.45620519, 0.03081071, 0.04091952]) 

60 HYS = RGB_to_IHLS(RGB) 

61 

62 RGB = np.tile(RGB, (6, 1)) 

63 HYS = np.tile(HYS, (6, 1)) 

64 np.testing.assert_allclose(RGB_to_IHLS(RGB), HYS, atol=TOLERANCE_ABSOLUTE_TESTS) 

65 

66 RGB = np.reshape(RGB, (2, 3, 3)) 

67 HYS = np.reshape(HYS, (2, 3, 3)) 

68 np.testing.assert_allclose(RGB_to_IHLS(RGB), HYS, atol=TOLERANCE_ABSOLUTE_TESTS) 

69 

70 def test_domain_range_scale_RGB_to_IHLS(self) -> None: 

71 """ 

72 Test :func:`colour.models.rgb.hanbury2003.RGB_to_IHLS` definition 

73 domain and range scale support. 

74 """ 

75 

76 RGB = np.array([0.45620519, 0.03081071, 0.04091952]) 

77 HYS = RGB_to_IHLS(RGB) 

78 

79 d_r = (("reference", 1), ("1", 1), ("100", 100)) 

80 for scale, factor in d_r: 

81 with domain_range_scale(scale): 

82 np.testing.assert_allclose( 

83 RGB_to_IHLS(RGB * factor), 

84 HYS * factor, 

85 atol=TOLERANCE_ABSOLUTE_TESTS, 

86 ) 

87 

88 @ignore_numpy_errors 

89 def test_nan_RGB_to_IHLS(self) -> None: 

90 """ 

91 Test :func:`colour.models.rgb.hanbury2003.RGB_to_IHLS` definition nan 

92 support. 

93 """ 

94 

95 cases = [-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan] 

96 cases = np.array(list(set(product(cases, repeat=3)))) 

97 RGB_to_IHLS(cases) 

98 

99 

100class TestIHLS_to_RGB: 

101 """ 

102 Define :func:`colour.models.rgb.hanbury2003.RGB_to_IHLS` definition unit 

103 tests methods. 

104 """ 

105 

106 def test_IHLS_to_RGB(self) -> None: 

107 """Test :func:`colour.models.rgb.hanbury2003.IHLS_to_RGB` definition.""" 

108 

109 np.testing.assert_allclose( 

110 IHLS_to_RGB(np.array([6.26236117, 0.12197943, 0.42539448])), 

111 np.array([0.45620519, 0.03081071, 0.04091952]), 

112 atol=TOLERANCE_ABSOLUTE_TESTS, 

113 ) 

114 

115 np.testing.assert_allclose( 

116 IHLS_to_RGB(np.array([0.00000000, 0.00000000, 0.00000000])), 

117 np.array([0.00000000, 0.00000000, 0.00000000]), 

118 atol=TOLERANCE_ABSOLUTE_TESTS, 

119 ) 

120 

121 np.testing.assert_allclose( 

122 IHLS_to_RGB(np.array([0.00000000, 1.00000000, 0.00000000])), 

123 np.array([1.00000000, 1.00000000, 1.00000000]), 

124 atol=TOLERANCE_ABSOLUTE_TESTS, 

125 ) 

126 

127 def test_n_dimensional_IHLS_to_RGB(self) -> None: 

128 """ 

129 Test :func:`colour.models.rgb.hanbury2003.IHLS_to_RGB` definition 

130 n-dimensional arrays support. 

131 """ 

132 

133 HYS = np.array([6.26236117, 0.12197943, 0.42539448]) 

134 RGB = IHLS_to_RGB(HYS) 

135 

136 HYS = np.tile(HYS, (6, 1)) 

137 RGB = np.tile(RGB, (6, 1)) 

138 np.testing.assert_allclose(IHLS_to_RGB(HYS), RGB, atol=TOLERANCE_ABSOLUTE_TESTS) 

139 

140 HYS = np.reshape(HYS, (2, 3, 3)) 

141 RGB = np.reshape(RGB, (2, 3, 3)) 

142 np.testing.assert_allclose(IHLS_to_RGB(HYS), RGB, atol=TOLERANCE_ABSOLUTE_TESTS) 

143 

144 def test_domain_range_scale_IHLS_to_RGB(self) -> None: 

145 """ 

146 Test :func:`colour.models.rgb.hanbury2003.IHLS_to_RGB` definition 

147 domain and range scale support. 

148 """ 

149 

150 HYS = np.array([6.26236117, 0.12197943, 0.42539448]) 

151 RGB = IHLS_to_RGB(HYS) 

152 

153 d_r = (("reference", 1), ("1", 1), ("100", 100)) 

154 for scale, factor in d_r: 

155 with domain_range_scale(scale): 

156 np.testing.assert_allclose( 

157 IHLS_to_RGB(HYS * factor), 

158 RGB * factor, 

159 atol=TOLERANCE_ABSOLUTE_TESTS, 

160 ) 

161 

162 @ignore_numpy_errors 

163 def test_nan_IHLS_to_RGB(self) -> None: 

164 """ 

165 Test :func:`colour.models.rgb.hanbury2003.IHLS_to_RGB` definition nan 

166 support. 

167 """ 

168 

169 cases = [-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan] 

170 cases = np.array(list(set(product(cases, repeat=3)))) 

171 IHLS_to_RGB(cases)