Coverage for colour/adaptation/tests/test_fairchild2020.py: 100%
86 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-15 19:01 +1300
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-15 19:01 +1300
1"""
2Define the unit tests for the :mod:`colour.adaptation.fairchild2020` module.
3"""
5from __future__ import annotations
7import unittest
8from itertools import product
10import numpy as np
12from colour.adaptation import (
13 chromatic_adaptation_vK20,
14 matrix_chromatic_adaptation_vk20,
15)
16from colour.adaptation.fairchild2020 import CONDITIONS_DEGREE_OF_ADAPTATION_VK20
17from colour.constants import TOLERANCE_ABSOLUTE_TESTS
18from colour.utilities import domain_range_scale, ignore_numpy_errors
20__author__ = "Colour Developers"
21__copyright__ = "Copyright 2013 Colour Developers"
22__license__ = "New BSD License - https://opensource.org/licenses/BSD-3-Clause"
23__maintainer__ = "Colour Developers"
24__email__ = "colour-developers@colour-science.org"
25__status__ = "Production"
27__all__ = [
28 "TestMatrixChromaticAdaptationVonKries",
29 "TestChromaticAdaptationVonKries",
30]
33class TestMatrixChromaticAdaptationVonKries(unittest.TestCase):
34 """
35 Define :func:`colour.adaptation.fairchild2020.\
36matrix_chromatic_adaptation_vk20` definition unit tests methods.
37 """
39 def test_matrix_chromatic_adaptation_vk20(self) -> None:
40 """
41 Test :func:`colour.adaptation.fairchild2020.\
42matrix_chromatic_adaptation_vk20` definition.
43 """
45 np.testing.assert_array_almost_equal(
46 matrix_chromatic_adaptation_vk20(
47 np.array([0.95045593, 1.00000000, 1.08905775]),
48 np.array([0.96429568, 1.00000000, 0.82510460]),
49 ),
50 np.array(
51 [
52 [1.02791390, 0.02913712, -0.02279407],
53 [0.02070284, 0.99005317, -0.00921435],
54 [-0.00063759, -0.00115773, 0.91296320],
55 ]
56 ),
57 decimal=TOLERANCE_ABSOLUTE_TESTS,
58 )
60 np.testing.assert_array_almost_equal(
61 matrix_chromatic_adaptation_vk20(
62 np.array([0.95045593, 1.00000000, 1.08905775]),
63 np.array([1.09846607, 1.00000000, 0.35582280]),
64 ),
65 np.array(
66 [
67 [0.94760338, -0.05816939, 0.06647414],
68 [-0.04151006, 1.02361127, 0.02667016],
69 [0.00163074, 0.00391656, 1.29341031],
70 ]
71 ),
72 decimal=TOLERANCE_ABSOLUTE_TESTS,
73 )
75 np.testing.assert_array_almost_equal(
76 matrix_chromatic_adaptation_vk20(
77 np.array([0.95045593, 1.00000000, 1.08905775]),
78 np.array([0.96429568, 1.00000000, 0.82510460]),
79 transform="XYZ Scaling",
80 ),
81 np.array(
82 [
83 [1.03217229, 0.00000000, 0.00000000],
84 [0.00000000, 1.00000000, 0.00000000],
85 [0.00000000, 0.00000000, 0.91134516],
86 ]
87 ),
88 decimal=TOLERANCE_ABSOLUTE_TESTS,
89 )
91 np.testing.assert_array_almost_equal(
92 matrix_chromatic_adaptation_vk20(
93 np.array([0.95045593, 1.00000000, 1.08905775]),
94 np.array([0.96429568, 1.00000000, 0.82510460]),
95 transform="Bradford",
96 ),
97 np.array(
98 [
99 [1.03672305, 0.01955802, -0.02193210],
100 [0.02763218, 0.98222961, -0.00824197],
101 [-0.00295083, 0.00406903, 0.91024305],
102 ]
103 ),
104 decimal=TOLERANCE_ABSOLUTE_TESTS,
105 )
107 np.testing.assert_array_almost_equal(
108 matrix_chromatic_adaptation_vk20(
109 np.array([0.95045593, 1.00000000, 1.08905775]),
110 np.array([0.96429568, 1.00000000, 0.82510460]),
111 transform="Von Kries",
112 coefficients=CONDITIONS_DEGREE_OF_ADAPTATION_VK20["Simple Von Kries"],
113 ),
114 np.array(
115 [
116 [0.98446157, -0.05474538, 0.06773143],
117 [-0.00601339, 1.00479590, 0.00121235],
118 [0.00000000, 0.00000000, 1.31990977],
119 ]
120 ),
121 decimal=TOLERANCE_ABSOLUTE_TESTS,
122 )
124 def test_n_dimensional_matrix_chromatic_adaptation_vk20(self) -> None:
125 """
126 Test :func:`colour.adaptation.fairchild2020.\
127matrix_chromatic_adaptation_vk20` definition n-dimensional arrays support.
128 """
130 XYZ_p = np.array([0.95045593, 1.00000000, 1.08905775])
131 XYZ_n = np.array([0.96429568, 1.00000000, 0.82510460])
132 M = matrix_chromatic_adaptation_vk20(XYZ_p, XYZ_n)
134 XYZ_p = np.tile(XYZ_p, (6, 1))
135 XYZ_n = np.tile(XYZ_n, (6, 1))
136 M = np.reshape(np.tile(M, (6, 1)), (6, 3, 3))
137 np.testing.assert_array_almost_equal(
138 matrix_chromatic_adaptation_vk20(XYZ_p, XYZ_n),
139 M,
140 decimal=TOLERANCE_ABSOLUTE_TESTS,
141 )
143 XYZ_p = np.reshape(XYZ_p, (2, 3, 3))
144 XYZ_n = np.reshape(XYZ_n, (2, 3, 3))
145 M = np.reshape(M, (2, 3, 3, 3))
146 np.testing.assert_array_almost_equal(
147 matrix_chromatic_adaptation_vk20(XYZ_p, XYZ_n),
148 M,
149 decimal=TOLERANCE_ABSOLUTE_TESTS,
150 )
152 def test_domain_range_scale_matrix_chromatic_adaptation_vk20(self) -> None:
153 """
154 Test :func:`colour.adaptation.fairchild2020.\
155matrix_chromatic_adaptation_vk20` definition domain and range scale
156 support.
157 """
159 XYZ_p = np.array([0.95045593, 1.00000000, 1.08905775])
160 XYZ_n = np.array([0.96429568, 1.00000000, 0.82510460])
161 XYZ_r = np.array([0.97941176, 1.00000000, 1.73235294])
162 M = matrix_chromatic_adaptation_vk20(XYZ_p, XYZ_n)
164 d_r = (("reference", 1), ("1", 1), ("100", 1))
165 for scale, factor in d_r:
166 with domain_range_scale(scale):
167 np.testing.assert_array_almost_equal(
168 matrix_chromatic_adaptation_vk20(
169 XYZ_p * factor, XYZ_n * factor, XYZ_r * factor
170 ),
171 M,
172 decimal=TOLERANCE_ABSOLUTE_TESTS,
173 )
175 @ignore_numpy_errors
176 def test_nan_matrix_chromatic_adaptation_vk20(self) -> None:
177 """
178 Test :func:`colour.adaptation.fairchild2020.\
179matrix_chromatic_adaptation_vk20` definition nan support.
180 """
182 cases = [-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]
183 cases = np.array(list(set(product(cases, repeat=3))))
184 matrix_chromatic_adaptation_vk20(cases, cases)
187class TestChromaticAdaptationVonKries(unittest.TestCase):
188 """
189 Define :func:`colour.adaptation.fairchild2020.chromatic_adaptation_vK20`
190 definition unit tests methods.
191 """
193 def test_chromatic_adaptation_vK20(self) -> None:
194 """
195 Test :func:`colour.adaptation.fairchild2020.chromatic_adaptation_vK20`
196 definition.
197 """
199 np.testing.assert_array_almost_equal(
200 chromatic_adaptation_vK20(
201 np.array([0.20654008, 0.12197225, 0.05136952]),
202 np.array([0.95045593, 1.00000000, 1.08905775]),
203 np.array([0.96429568, 1.00000000, 0.82510460]),
204 ),
205 np.array([0.21468842, 0.12456164, 0.04662558]),
206 decimal=TOLERANCE_ABSOLUTE_TESTS,
207 )
209 np.testing.assert_array_almost_equal(
210 chromatic_adaptation_vK20(
211 np.array([0.14222010, 0.23042768, 0.10495772]),
212 np.array([0.95045593, 1.00000000, 1.08905775]),
213 np.array([1.09846607, 1.00000000, 0.35582280]),
214 ),
215 np.array([0.12834138, 0.23276404, 0.13688781]),
216 decimal=TOLERANCE_ABSOLUTE_TESTS,
217 )
219 np.testing.assert_array_almost_equal(
220 chromatic_adaptation_vK20(
221 np.array([0.07818780, 0.06157201, 0.28099326]),
222 np.array([0.95045593, 1.00000000, 1.08905775]),
223 np.array([0.99144661, 1.00000000, 0.67315942]),
224 ),
225 np.array([0.07908008, 0.06167829, 0.28354175]),
226 decimal=TOLERANCE_ABSOLUTE_TESTS,
227 )
229 np.testing.assert_array_almost_equal(
230 chromatic_adaptation_vK20(
231 np.array([0.20654008, 0.12197225, 0.05136952]),
232 np.array([0.95045593, 1.00000000, 1.08905775]),
233 np.array([0.96429568, 1.00000000, 0.82510460]),
234 transform="XYZ Scaling",
235 ),
236 np.array([0.21318495, 0.12197225, 0.04681536]),
237 decimal=TOLERANCE_ABSOLUTE_TESTS,
238 )
240 np.testing.assert_array_almost_equal(
241 chromatic_adaptation_vK20(
242 np.array([0.20654008, 0.12197225, 0.05136952]),
243 np.array([0.95045593, 1.00000000, 1.08905775]),
244 np.array([0.96429568, 1.00000000, 0.82510460]),
245 transform="Bradford",
246 ),
247 np.array([0.21538376, 0.12508852, 0.04664559]),
248 decimal=TOLERANCE_ABSOLUTE_TESTS,
249 )
251 np.testing.assert_array_almost_equal(
252 chromatic_adaptation_vK20(
253 np.array([0.20654008, 0.12197225, 0.05136952]),
254 np.array([0.95045593, 1.00000000, 1.08905775]),
255 np.array([0.96429568, 1.00000000, 0.82510460]),
256 transform="Von Kries",
257 coefficients=CONDITIONS_DEGREE_OF_ADAPTATION_VK20["Simple Von Kries"],
258 ),
259 np.array([0.20013269, 0.12137749, 0.06780313]),
260 decimal=TOLERANCE_ABSOLUTE_TESTS,
261 )
263 def test_n_dimensional_chromatic_adaptation_vK20(self) -> None:
264 """
265 Test :func:`colour.adaptation.fairchild2020.chromatic_adaptation_vK20`
266 definition n-dimensional arrays support.
267 """
269 XYZ = np.array([0.20654008, 0.12197225, 0.05136952])
270 XYZ_p = np.array([0.95045593, 1.00000000, 1.08905775])
271 XYZ_n = np.array([0.96429568, 1.00000000, 0.82510460])
272 XYZ_a = chromatic_adaptation_vK20(XYZ, XYZ_p, XYZ_n)
274 XYZ = np.tile(XYZ, (6, 1))
275 XYZ_p = np.tile(XYZ_p, (6, 1))
276 XYZ_n = np.tile(XYZ_n, (6, 1))
277 XYZ_a = np.tile(XYZ_a, (6, 1))
278 np.testing.assert_array_almost_equal(
279 chromatic_adaptation_vK20(XYZ, XYZ_p, XYZ_n),
280 XYZ_a,
281 decimal=TOLERANCE_ABSOLUTE_TESTS,
282 )
284 XYZ = np.reshape(XYZ, (2, 3, 3))
285 XYZ_p = np.reshape(XYZ_p, (2, 3, 3))
286 XYZ_n = np.reshape(XYZ_n, (2, 3, 3))
287 XYZ_a = np.reshape(XYZ_a, (2, 3, 3))
288 np.testing.assert_array_almost_equal(
289 chromatic_adaptation_vK20(XYZ, XYZ_p, XYZ_n),
290 XYZ_a,
291 decimal=TOLERANCE_ABSOLUTE_TESTS,
292 )
294 def test_domain_range_scale_chromatic_adaptation_vK20(self) -> None:
295 """
296 Test :func:`colour.adaptation.fairchild2020.chromatic_adaptation_vK20`
297 definition domain and range scale support.
298 """
300 XYZ = np.array([0.20654008, 0.12197225, 0.05136952])
301 XYZ_p = np.array([0.95045593, 1.00000000, 1.08905775])
302 XYZ_n = np.array([0.96429568, 1.00000000, 0.82510460])
303 XYZ_r = np.array([0.97941176, 1.00000000, 1.73235294])
304 XYZ_a = chromatic_adaptation_vK20(XYZ, XYZ_p, XYZ_n)
306 d_r = (("reference", 1), ("1", 1), ("100", 100))
307 for scale, factor in d_r:
308 with domain_range_scale(scale):
309 np.testing.assert_array_almost_equal(
310 chromatic_adaptation_vK20(
311 XYZ * factor,
312 XYZ_p * factor,
313 XYZ_n * factor,
314 XYZ_r * factor,
315 ),
316 XYZ_a * factor,
317 decimal=TOLERANCE_ABSOLUTE_TESTS,
318 )
320 @ignore_numpy_errors
321 def test_nan_chromatic_adaptation_vK20(self) -> None:
322 """
323 Test :func:`colour.adaptation.fairchild2020.chromatic_adaptation_vK20`
324 definition nan support.
325 """
327 cases = [-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]
328 cases = np.array(list(set(product(cases, repeat=3))))
329 chromatic_adaptation_vK20(cases, cases, cases)