Coverage for models/rgb/transfer_functions/tests/test_common.py: 100%

1"""

2 Define the unit tests for the

3:mod:`colour.models.rgb.transfer_functions.common` module.

4"""

5

6 import numpy as np

7

8 from colour.constants import TOLERANCE_ABSOLUTE_TESTS

9 from colour.models.rgb.transfer_functions import CV_range, full_to_legal, legal_to_full

10 from colour.utilities import ignore_numpy_errors

11

12 __author__ = "Colour Developers"

13 __copyright__ = "Copyright 2013 Colour Developers"

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

15 __maintainer__ = "Colour Developers"

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

17 __status__ = "Development"

18

19 __all__ = [

20 "TestCV_range",

21 "TestLegalToFull",

22 "TestFullToLegal",

23]

24

25

26 class TestCV_range:

27 """

28 Define :func:`colour.models.rgb.transfer_functions.common.CV_range`

29 definition unit tests methods.

30 """

31

32 def test_CV_range(self) -> None:

33 """

34 Test :func:`colour.models.rgb.transfer_functions.common.CV_range`

35 definition.

36 """

37

38 np.testing.assert_array_equal(CV_range(8, True, True), np.array([16, 235]))

39

40 np.testing.assert_array_equal(CV_range(8, False, True), np.array([0, 255]))

41

42 np.testing.assert_allclose(

43 CV_range(8, True, False),

44 np.array([0.06274510, 0.92156863]),

45 atol=TOLERANCE_ABSOLUTE_TESTS,

46 )

47

48 np.testing.assert_array_equal(CV_range(8, False, False), np.array([0, 1]))

49

50 np.testing.assert_array_equal(CV_range(10, True, True), np.array([64, 940]))

51

52 np.testing.assert_array_equal(CV_range(10, False, True), np.array([0, 1023]))

53

54 np.testing.assert_allclose(

55 CV_range(10, True, False),

56 np.array([0.06256109, 0.91886608]),

57 atol=TOLERANCE_ABSOLUTE_TESTS,

58 )

59

60 np.testing.assert_array_equal(CV_range(10, False, False), np.array([0, 1]))

61

62

63 class TestLegalToFull:

64 """

65 Define :func:`colour.models.rgb.transfer_functions.common.legal_to_full`

66 definition unit tests methods.

67 """

68

69 def test_legal_to_full(self) -> None:

70 """

71 Test :func:`colour.models.rgb.transfer_functions.common.legal_to_full`

72 definition.

73 """

74

75 np.testing.assert_allclose(legal_to_full(64 / 1023), 0.0)

76

77 np.testing.assert_allclose(legal_to_full(940 / 1023), 1.0)

78

79 np.testing.assert_allclose(legal_to_full(64 / 1023, out_int=True), 0)

80

81 np.testing.assert_allclose(legal_to_full(940 / 1023, out_int=True), 1023)

82

83 np.testing.assert_allclose(legal_to_full(64, in_int=True), 0.0)

84

85 np.testing.assert_allclose(legal_to_full(940, in_int=True), 1.0)

86

87 np.testing.assert_allclose(legal_to_full(64, in_int=True, out_int=True), 0)

88

89 np.testing.assert_allclose(legal_to_full(940, in_int=True, out_int=True), 1023)

90

91 def test_n_dimensional_legal_to_full(self) -> None:

92 """

93 Test :func:`colour.models.rgb.transfer_functions.common.legal_to_full`

94 definition n-dimensional arrays support.

95 """

96

97 CV_l = 0.918866080156403

98 CV_f = legal_to_full(CV_l, 10)

99

100 CV_l = np.tile(CV_l, 6)

101 CV_f = np.tile(CV_f, 6)

102 np.testing.assert_allclose(

103 legal_to_full(CV_l, 10), CV_f, atol=TOLERANCE_ABSOLUTE_TESTS

104 )

105

106 CV_l = np.reshape(CV_l, (2, 3))

107 CV_f = np.reshape(CV_f, (2, 3))

108 np.testing.assert_allclose(

109 legal_to_full(CV_l, 10), CV_f, atol=TOLERANCE_ABSOLUTE_TESTS

110 )

111

112 CV_l = np.reshape(CV_l, (2, 3, 1))

113 CV_f = np.reshape(CV_f, (2, 3, 1))

114 np.testing.assert_allclose(

115 legal_to_full(CV_l, 10), CV_f, atol=TOLERANCE_ABSOLUTE_TESTS

116 )

117

118 @ignore_numpy_errors

119 def test_nan_legal_to_full(self) -> None:

120 """

121 Test :func:`colour.models.rgb.transfer_functions.common.legal_to_full`

122 definition nan support.

123 """

124

125 legal_to_full(np.array([-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]), 10)

126

127

128 class TestFullToLegal:

129 """

130 Define :func:`colour.models.rgb.transfer_functions.common.full_to_legal`

131 definition unit tests methods.

132 """

133

134 def test_full_to_legal(self) -> None:

135 """

136 Test :func:`colour.models.rgb.transfer_functions.common.full_to_legal`

137 definition.

138 """

139

140 np.testing.assert_allclose(full_to_legal(0.0), 0.062561094819159)

141

142 np.testing.assert_allclose(full_to_legal(1.0), 0.918866080156403)

143

144 np.testing.assert_allclose(full_to_legal(0.0, out_int=True), 64)

145

146 np.testing.assert_allclose(full_to_legal(1.0, out_int=True), 940)

147

148 np.testing.assert_allclose(full_to_legal(0, in_int=True), 0.062561094819159)

149

150 np.testing.assert_allclose(full_to_legal(1023, in_int=True), 0.918866080156403)

151

152 np.testing.assert_allclose(full_to_legal(0, in_int=True, out_int=True), 64)

153

154 np.testing.assert_allclose(full_to_legal(1023, in_int=True, out_int=True), 940)

155

156 def test_n_dimensional_full_to_legal(self) -> None:

157 """

158 Test :func:`colour.models.rgb.transfer_functions.common.full_to_legal`

159 definition n-dimensional arrays support.

160 """

161

162 CF_f = 1.0

163 CV_l = full_to_legal(CF_f, 10)

164

165 CF_f = np.tile(CF_f, 6)

166 CV_l = np.tile(CV_l, 6)

167 np.testing.assert_allclose(

168 full_to_legal(CF_f, 10), CV_l, atol=TOLERANCE_ABSOLUTE_TESTS

169 )

170

171 CF_f = np.reshape(CF_f, (2, 3))

172 CV_l = np.reshape(CV_l, (2, 3))

173 np.testing.assert_allclose(

174 full_to_legal(CF_f, 10), CV_l, atol=TOLERANCE_ABSOLUTE_TESTS

175 )

176

177 CF_f = np.reshape(CF_f, (2, 3, 1))

178 CV_l = np.reshape(CV_l, (2, 3, 1))

179 np.testing.assert_allclose(

180 full_to_legal(CF_f, 10), CV_l, atol=TOLERANCE_ABSOLUTE_TESTS

181 )

182

183 @ignore_numpy_errors

184 def test_nan_full_to_legal(self) -> None:

185 """

186 Test :func:`colour.models.rgb.transfer_functions.common.full_to_legal`

187 definition nan support.

188 """

189

190 full_to_legal(np.array([-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]), 10)