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

1"""

2 Define the unit tests for the :mod:`colour.models.rgb.transfer_functions.sRGB`

3 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 eotf_inverse_sRGB, eotf_sRGB

10 from colour.utilities import domain_range_scale, 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__ = "Production"

18

19 __all__ = [

20 "TestEotf_inverse_sRGB",

21 "TestEotf_sRGB",

22]

23

24

25 class TestEotf_inverse_sRGB:

26 """

27 Define :func:`colour.models.rgb.transfer_functions.sRGB.eotf_inverse_sRGB`

28 definition unit tests methods.

29 """

30

31 def test_eotf_inverse_sRGB(self) -> None:

32 """

33 Test :func:`colour.models.rgb.transfer_functions.sRGB.\

34 eotf_inverse_sRGB` definition.

35 """

36

37 np.testing.assert_allclose(

38 eotf_inverse_sRGB(0.0), 0.0, atol=TOLERANCE_ABSOLUTE_TESTS

39 )

40

41 np.testing.assert_allclose(

42 eotf_inverse_sRGB(0.18),

43 0.461356129500442,

44 atol=TOLERANCE_ABSOLUTE_TESTS,

45 )

46

47 np.testing.assert_allclose(

48 eotf_inverse_sRGB(1.0), 1.0, atol=TOLERANCE_ABSOLUTE_TESTS

49 )

50

51 def test_n_dimensional_eotf_inverse_sRGB(self) -> None:

52 """

53 Test :func:`colour.models.rgb.transfer_functions.sRGB.\

54 eotf_inverse_sRGB` definition n-dimensional arrays support.

55 """

56

57 L = 0.18

58 V = eotf_inverse_sRGB(L)

59

60 L = np.tile(L, 6)

61 V = np.tile(V, 6)

62 np.testing.assert_allclose(

63 eotf_inverse_sRGB(L), V, atol=TOLERANCE_ABSOLUTE_TESTS

64 )

65

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

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

68 np.testing.assert_allclose(

69 eotf_inverse_sRGB(L), V, atol=TOLERANCE_ABSOLUTE_TESTS

70 )

71

72 L = np.reshape(L, (2, 3, 1))

73 V = np.reshape(V, (2, 3, 1))

74 np.testing.assert_allclose(

75 eotf_inverse_sRGB(L), V, atol=TOLERANCE_ABSOLUTE_TESTS

76 )

77

78 def test_domain_range_scale_eotf_inverse_sRGB(self) -> None:

79 """

80 Test :func:`colour.models.rgb.transfer_functions.sRGB.\

81 eotf_inverse_sRGB` definition domain and range scale support.

82 """

83

84 L = 0.18

85 V = eotf_inverse_sRGB(L)

86

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

88 for scale, factor in d_r:

89 with domain_range_scale(scale):

90 np.testing.assert_allclose(

91 eotf_inverse_sRGB(L * factor),

92 V * factor,

93 atol=TOLERANCE_ABSOLUTE_TESTS,

94 )

95

96 @ignore_numpy_errors

97 def test_nan_eotf_inverse_sRGB(self) -> None:

98 """

99 Test :func:`colour.models.rgb.transfer_functions.sRGB.\

100 eotf_inverse_sRGB` definition nan support.

101 """

102

103 eotf_inverse_sRGB(np.array([-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]))

104

105

106 class TestEotf_sRGB:

107 """

108 Define :func:`colour.models.rgb.transfer_functions.sRGB.eotf_sRGB`

109 definition unit tests methods.

110 """

111

112 def test_eotf_sRGB(self) -> None:

113 """

114 Test :func:`colour.models.rgb.transfer_functions.sRGB.\

115 eotf_sRGB` definition.

116 """

117

118 np.testing.assert_allclose(eotf_sRGB(0.0), 0.0, atol=TOLERANCE_ABSOLUTE_TESTS)

119

120 np.testing.assert_allclose(

121 eotf_sRGB(0.461356129500442), 0.18, atol=TOLERANCE_ABSOLUTE_TESTS

122 )

123

124 np.testing.assert_allclose(eotf_sRGB(1.0), 1.0, atol=TOLERANCE_ABSOLUTE_TESTS)

125

126 def test_n_dimensional_eotf_sRGB(self) -> None:

127 """

128 Test :func:`colour.models.rgb.transfer_functions.sRGB.\

129 eotf_sRGB` definition n-dimensional arrays support.

130 """

131

132 V = 0.461356129500442

133 L = eotf_sRGB(V)

134

135 V = np.tile(V, 6)

136 L = np.tile(L, 6)

137 np.testing.assert_allclose(eotf_sRGB(V), L, atol=TOLERANCE_ABSOLUTE_TESTS)

138

139 V = np.reshape(V, (2, 3))

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

141 np.testing.assert_allclose(eotf_sRGB(V), L, atol=TOLERANCE_ABSOLUTE_TESTS)

142

143 V = np.reshape(V, (2, 3, 1))

144 L = np.reshape(L, (2, 3, 1))

145 np.testing.assert_allclose(eotf_sRGB(V), L, atol=TOLERANCE_ABSOLUTE_TESTS)

146

147 def test_domain_range_scale_eotf_sRGB(self) -> None:

148 """

149 Test :func:`colour.models.rgb.transfer_functions.sRGB.\

150 eotf_sRGB` definition domain and range scale support.

151 """

152

153 V = 0.461356129500442

154 L = eotf_sRGB(V)

155

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

157 for scale, factor in d_r:

158 with domain_range_scale(scale):

159 np.testing.assert_allclose(

160 eotf_sRGB(V * factor),

161 L * factor,

162 atol=TOLERANCE_ABSOLUTE_TESTS,

163 )

164

165 @ignore_numpy_errors

166 def test_nan_eotf_sRGB(self) -> None:

167 """

168 Test :func:`colour.models.rgb.transfer_functions.sRGB.\

169 eotf_sRGB` definition nan support.

170 """

171

172 eotf_sRGB(np.array([-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]))