Coverage for models/rgb/transfer_functions/tests/test_gamma.py: 100%
77 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-16 22:49 +1300
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-16 22:49 +1300
1"""
2Define the unit tests for the
3:mod:`colour.models.rgb.transfer_functions.gamma` module.
4"""
6import numpy as np
8from colour.constants import TOLERANCE_ABSOLUTE_TESTS
9from colour.models.rgb.transfer_functions import gamma_function
10from colour.utilities import ignore_numpy_errors
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"
19__all__ = [
20 "TestGammaFunction",
21]
24class TestGammaFunction:
25 """
26 Define :func:`colour.models.rgb.transfer_functions.gamma.gamma_function`
27 definition unit tests methods.
28 """
30 def test_gamma_function(self) -> None:
31 """
32 Test :func:`colour.models.rgb.transfer_functions.gamma.\
33gamma_function` definition.
34 """
36 np.testing.assert_allclose(
37 gamma_function(0.0, 2.2), 0.0, atol=TOLERANCE_ABSOLUTE_TESTS
38 )
40 np.testing.assert_allclose(
41 gamma_function(0.18, 2.2),
42 0.022993204992707,
43 atol=TOLERANCE_ABSOLUTE_TESTS,
44 )
46 np.testing.assert_allclose(
47 gamma_function(0.022993204992707, 1.0 / 2.2),
48 0.18,
49 atol=TOLERANCE_ABSOLUTE_TESTS,
50 )
52 np.testing.assert_allclose(
53 gamma_function(-0.18, 2.0),
54 0.0323999999999998,
55 atol=TOLERANCE_ABSOLUTE_TESTS,
56 )
58 np.testing.assert_array_equal(gamma_function(-0.18, 2.2), np.nan)
60 np.testing.assert_allclose(
61 gamma_function(-0.18, 2.2, "Mirror"),
62 -0.022993204992707,
63 atol=TOLERANCE_ABSOLUTE_TESTS,
64 )
66 np.testing.assert_allclose(
67 gamma_function(-0.18, 2.2, "Preserve"),
68 -0.18,
69 atol=TOLERANCE_ABSOLUTE_TESTS,
70 )
72 np.testing.assert_allclose(
73 gamma_function(-0.18, 2.2, "Clamp"),
74 0,
75 atol=TOLERANCE_ABSOLUTE_TESTS,
76 )
78 np.testing.assert_array_equal(gamma_function(-0.18, -2.2), np.nan)
80 np.testing.assert_allclose(
81 gamma_function(0.0, -2.2, "Mirror"),
82 0.0,
83 atol=TOLERANCE_ABSOLUTE_TESTS,
84 )
86 np.testing.assert_allclose(
87 gamma_function(0.0, 2.2, "Preserve"),
88 0.0,
89 atol=TOLERANCE_ABSOLUTE_TESTS,
90 )
92 np.testing.assert_allclose(
93 gamma_function(0.0, 2.2, "Clamp"), 0, atol=TOLERANCE_ABSOLUTE_TESTS
94 )
96 def test_n_dimensional_gamma_function(self) -> None:
97 """
98 Test :func:`colour.models.rgb.transfer_functions.gamma.\
99gamma_function` definition n-dimensional arrays support.
100 """
102 a = 0.18
103 a_p = gamma_function(a, 2.2)
105 a = np.tile(a, 6)
106 a_p = np.tile(a_p, 6)
107 np.testing.assert_allclose(
108 gamma_function(a, 2.2), a_p, atol=TOLERANCE_ABSOLUTE_TESTS
109 )
111 a = np.reshape(a, (2, 3))
112 a_p = np.reshape(a_p, (2, 3))
113 np.testing.assert_allclose(
114 gamma_function(a, 2.2), a_p, atol=TOLERANCE_ABSOLUTE_TESTS
115 )
117 a = np.reshape(a, (2, 3, 1))
118 a_p = np.reshape(a_p, (2, 3, 1))
119 np.testing.assert_allclose(
120 gamma_function(a, 2.2), a_p, atol=TOLERANCE_ABSOLUTE_TESTS
121 )
123 a = -0.18
124 a_p = -0.022993204992707
125 np.testing.assert_allclose(
126 gamma_function(a, 2.2, "Mirror"),
127 a_p,
128 atol=TOLERANCE_ABSOLUTE_TESTS,
129 )
131 a = np.tile(a, 6)
132 a_p = np.tile(a_p, 6)
133 np.testing.assert_allclose(
134 gamma_function(a, 2.2, "Mirror"),
135 a_p,
136 atol=TOLERANCE_ABSOLUTE_TESTS,
137 )
139 a = np.reshape(a, (2, 3))
140 a_p = np.reshape(a_p, (2, 3))
141 np.testing.assert_allclose(
142 gamma_function(a, 2.2, "Mirror"),
143 a_p,
144 atol=TOLERANCE_ABSOLUTE_TESTS,
145 )
147 a = np.reshape(a, (2, 3, 1))
148 a_p = np.reshape(a_p, (2, 3, 1))
149 np.testing.assert_allclose(
150 gamma_function(a, 2.2, "Mirror"),
151 a_p,
152 atol=TOLERANCE_ABSOLUTE_TESTS,
153 )
155 a = -0.18
156 a_p = -0.18
157 np.testing.assert_allclose(
158 gamma_function(a, 2.2, "Preserve"),
159 a_p,
160 atol=TOLERANCE_ABSOLUTE_TESTS,
161 )
163 a = np.tile(a, 6)
164 a_p = np.tile(a_p, 6)
165 np.testing.assert_allclose(
166 gamma_function(a, 2.2, "Preserve"),
167 a_p,
168 atol=TOLERANCE_ABSOLUTE_TESTS,
169 )
171 a = np.reshape(a, (2, 3))
172 a_p = np.reshape(a_p, (2, 3))
173 np.testing.assert_allclose(
174 gamma_function(a, 2.2, "Preserve"),
175 a_p,
176 atol=TOLERANCE_ABSOLUTE_TESTS,
177 )
179 a = np.reshape(a, (2, 3, 1))
180 a_p = np.reshape(a_p, (2, 3, 1))
181 np.testing.assert_allclose(
182 gamma_function(a, 2.2, "Preserve"),
183 a_p,
184 atol=TOLERANCE_ABSOLUTE_TESTS,
185 )
187 a = -0.18
188 a_p = 0.0
189 np.testing.assert_allclose(
190 gamma_function(a, 2.2, "Clamp"), a_p, atol=TOLERANCE_ABSOLUTE_TESTS
191 )
193 a = np.tile(a, 6)
194 a_p = np.tile(a_p, 6)
195 np.testing.assert_allclose(
196 gamma_function(a, 2.2, "Clamp"), a_p, atol=TOLERANCE_ABSOLUTE_TESTS
197 )
199 a = np.reshape(a, (2, 3))
200 a_p = np.reshape(a_p, (2, 3))
201 np.testing.assert_allclose(
202 gamma_function(a, 2.2, "Clamp"), a_p, atol=TOLERANCE_ABSOLUTE_TESTS
203 )
205 a = np.reshape(a, (2, 3, 1))
206 a_p = np.reshape(a_p, (2, 3, 1))
207 np.testing.assert_allclose(
208 gamma_function(a, 2.2, "Clamp"), a_p, atol=TOLERANCE_ABSOLUTE_TESTS
209 )
211 @ignore_numpy_errors
212 def test_nan_gamma_function(self) -> None:
213 """
214 Test :func:`colour.models.rgb.transfer_functions.gamma.\
215gamma_function` definition nan support.
216 """
218 cases = [-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]
219 gamma_function(cases, cases)