Coverage for adaptation/tests/test_vonkries.py: 100%
83 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"""Define the unit tests for the :mod:`colour.adaptation.vonkries` module."""
3from __future__ import annotations
5from itertools import product
7import numpy as np
9from colour.adaptation import (
10 chromatic_adaptation_VonKries,
11 matrix_chromatic_adaptation_VonKries,
12)
13from colour.constants import TOLERANCE_ABSOLUTE_TESTS
14from colour.utilities import domain_range_scale, ignore_numpy_errors
16__author__ = "Colour Developers"
17__copyright__ = "Copyright 2013 Colour Developers"
18__license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause"
19__maintainer__ = "Colour Developers"
20__email__ = "colour-developers@colour-science.org"
21__status__ = "Production"
23__all__ = [
24 "TestMatrixChromaticAdaptationVonKries",
25 "TestChromaticAdaptationVonKries",
26]
29class TestMatrixChromaticAdaptationVonKries:
30 """
31 Define :func:`colour.adaptation.vonkries.\
32matrix_chromatic_adaptation_VonKries` definition unit tests methods.
33 """
35 def test_matrix_chromatic_adaptation_VonKries(self) -> None:
36 """
37 Test :func:`colour.adaptation.vonkries.\
38matrix_chromatic_adaptation_VonKries` definition.
39 """
41 np.testing.assert_allclose(
42 matrix_chromatic_adaptation_VonKries(
43 np.array([0.95045593, 1.00000000, 1.08905775]),
44 np.array([0.96429568, 1.00000000, 0.82510460]),
45 ),
46 np.array(
47 [
48 [1.04257389, 0.03089108, -0.05281257],
49 [0.02219345, 1.00185663, -0.02107375],
50 [-0.00116488, -0.00342053, 0.76178907],
51 ]
52 ),
53 atol=TOLERANCE_ABSOLUTE_TESTS,
54 )
56 np.testing.assert_allclose(
57 matrix_chromatic_adaptation_VonKries(
58 np.array([0.95045593, 1.00000000, 1.08905775]),
59 np.array([1.09846607, 1.00000000, 0.35582280]),
60 ),
61 np.array(
62 [
63 [1.17159793, 0.16088780, -0.16158366],
64 [0.11462057, 0.96182051, -0.06497572],
65 [-0.00413024, -0.00912739, 0.33871096],
66 ]
67 ),
68 atol=TOLERANCE_ABSOLUTE_TESTS,
69 )
71 np.testing.assert_allclose(
72 matrix_chromatic_adaptation_VonKries(
73 np.array([0.95045593, 1.00000000, 1.08905775]),
74 np.array([0.99144661, 1.00000000, 0.67315942]),
75 ),
76 np.linalg.inv(
77 matrix_chromatic_adaptation_VonKries(
78 np.array([0.99144661, 1.00000000, 0.67315942]),
79 np.array([0.95045593, 1.00000000, 1.08905775]),
80 )
81 ),
82 atol=TOLERANCE_ABSOLUTE_TESTS,
83 )
85 np.testing.assert_allclose(
86 matrix_chromatic_adaptation_VonKries(
87 np.array([0.95045593, 1.00000000, 1.08905775]),
88 np.array([0.96429568, 1.00000000, 0.82510460]),
89 transform="XYZ Scaling",
90 ),
91 np.array(
92 [
93 [1.01456117, 0.00000000, 0.00000000],
94 [0.00000000, 1.00000000, 0.00000000],
95 [0.00000000, 0.00000000, 0.75763163],
96 ]
97 ),
98 atol=TOLERANCE_ABSOLUTE_TESTS,
99 )
101 np.testing.assert_allclose(
102 matrix_chromatic_adaptation_VonKries(
103 np.array([0.95045593, 1.00000000, 1.08905775]),
104 np.array([0.96429568, 1.00000000, 0.82510460]),
105 transform="Bradford",
106 ),
107 np.array(
108 [
109 [1.04792979, 0.02294687, -0.05019227],
110 [0.02962781, 0.99043443, -0.01707380],
111 [-0.00924304, 0.01505519, 0.75187428],
112 ]
113 ),
114 atol=TOLERANCE_ABSOLUTE_TESTS,
115 )
117 np.testing.assert_allclose(
118 matrix_chromatic_adaptation_VonKries(
119 np.array([0.95045593, 1.00000000, 1.08905775]),
120 np.array([0.96429568, 1.00000000, 0.82510460]),
121 transform="Von Kries",
122 ),
123 np.array(
124 [
125 [1.01611856, 0.05535971, -0.05219186],
126 [0.00608087, 0.99555604, -0.00122642],
127 [0.00000000, 0.00000000, 0.75763163],
128 ]
129 ),
130 atol=TOLERANCE_ABSOLUTE_TESTS,
131 )
133 def test_n_dimensional_matrix_chromatic_adaptation_VonKries(self) -> None:
134 """
135 Test :func:`colour.adaptation.vonkries.\
136matrix_chromatic_adaptation_VonKries` definition n-dimensional arrays support.
137 """
139 XYZ_w = np.array([0.95045593, 1.00000000, 1.08905775])
140 XYZ_wr = np.array([0.96429568, 1.00000000, 0.82510460])
141 M = matrix_chromatic_adaptation_VonKries(XYZ_w, XYZ_wr)
143 XYZ_w = np.tile(XYZ_w, (6, 1))
144 XYZ_wr = np.tile(XYZ_wr, (6, 1))
145 M = np.reshape(np.tile(M, (6, 1)), (6, 3, 3))
146 np.testing.assert_allclose(
147 matrix_chromatic_adaptation_VonKries(XYZ_w, XYZ_wr),
148 M,
149 atol=TOLERANCE_ABSOLUTE_TESTS,
150 )
152 XYZ_w = np.reshape(XYZ_w, (2, 3, 3))
153 XYZ_wr = np.reshape(XYZ_wr, (2, 3, 3))
154 M = np.reshape(M, (2, 3, 3, 3))
155 np.testing.assert_allclose(
156 matrix_chromatic_adaptation_VonKries(XYZ_w, XYZ_wr),
157 M,
158 atol=TOLERANCE_ABSOLUTE_TESTS,
159 )
161 def test_domain_range_scale_matrix_chromatic_adaptation_VonKries(self) -> None:
162 """
163 Test :func:`colour.adaptation.vonkries.\
164matrix_chromatic_adaptation_VonKries` definition domain and range scale
165 support.
166 """
168 XYZ_w = np.array([0.95045593, 1.00000000, 1.08905775])
169 XYZ_wr = np.array([0.96429568, 1.00000000, 0.82510460])
170 M = matrix_chromatic_adaptation_VonKries(XYZ_w, XYZ_wr)
172 d_r = (("reference", 1), ("1", 1), ("100", 100))
173 for scale, factor in d_r:
174 with domain_range_scale(scale):
175 np.testing.assert_allclose(
176 matrix_chromatic_adaptation_VonKries(
177 XYZ_w * factor, XYZ_wr * factor
178 ),
179 M,
180 atol=TOLERANCE_ABSOLUTE_TESTS,
181 )
183 @ignore_numpy_errors
184 def test_nan_matrix_chromatic_adaptation_VonKries(self) -> None:
185 """
186 Test :func:`colour.adaptation.vonkries.\
187matrix_chromatic_adaptation_VonKries` definition nan support.
188 """
190 cases = [-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]
191 cases = np.array(list(set(product(cases, repeat=3))))
192 matrix_chromatic_adaptation_VonKries(cases, cases)
195class TestChromaticAdaptationVonKries:
196 """
197 Define :func:`colour.adaptation.vonkries.chromatic_adaptation_VonKries`
198 definition unit tests methods.
199 """
201 def test_chromatic_adaptation_VonKries(self) -> None:
202 """
203 Test :func:`colour.adaptation.vonkries.chromatic_adaptation_VonKries`
204 definition.
205 """
207 np.testing.assert_allclose(
208 chromatic_adaptation_VonKries(
209 np.array([0.20654008, 0.12197225, 0.05136952]),
210 np.array([0.95045593, 1.00000000, 1.08905775]),
211 np.array([0.96429568, 1.00000000, 0.82510460]),
212 ),
213 np.array([0.21638819, 0.12570000, 0.03847494]),
214 atol=TOLERANCE_ABSOLUTE_TESTS,
215 )
217 np.testing.assert_allclose(
218 chromatic_adaptation_VonKries(
219 np.array([0.14222010, 0.23042768, 0.10495772]),
220 np.array([0.95045593, 1.00000000, 1.08905775]),
221 np.array([1.09846607, 1.00000000, 0.35582280]),
222 ),
223 np.array([0.18673833, 0.23111171, 0.03285972]),
224 atol=TOLERANCE_ABSOLUTE_TESTS,
225 )
227 np.testing.assert_allclose(
228 chromatic_adaptation_VonKries(
229 np.array([0.07818780, 0.06157201, 0.28099326]),
230 np.array([0.95045593, 1.00000000, 1.08905775]),
231 np.array([0.99144661, 1.00000000, 0.67315942]),
232 ),
233 np.array([0.06385467, 0.05509729, 0.17506386]),
234 atol=TOLERANCE_ABSOLUTE_TESTS,
235 )
237 np.testing.assert_allclose(
238 chromatic_adaptation_VonKries(
239 np.array([0.20654008, 0.12197225, 0.05136952]),
240 np.array([0.95045593, 1.00000000, 1.08905775]),
241 np.array([0.96429568, 1.00000000, 0.82510460]),
242 transform="XYZ Scaling",
243 ),
244 np.array([0.20954755, 0.12197225, 0.03891917]),
245 atol=TOLERANCE_ABSOLUTE_TESTS,
246 )
248 np.testing.assert_allclose(
249 chromatic_adaptation_VonKries(
250 np.array([0.20654008, 0.12197225, 0.05136952]),
251 np.array([0.95045593, 1.00000000, 1.08905775]),
252 np.array([0.96429568, 1.00000000, 0.82510460]),
253 transform="Bradford",
254 ),
255 np.array([0.21666003, 0.12604777, 0.03855068]),
256 atol=TOLERANCE_ABSOLUTE_TESTS,
257 )
259 np.testing.assert_allclose(
260 chromatic_adaptation_VonKries(
261 np.array([0.20654008, 0.12197225, 0.05136952]),
262 np.array([0.95045593, 1.00000000, 1.08905775]),
263 np.array([0.96429568, 1.00000000, 0.82510460]),
264 transform="Von Kries",
265 ),
266 np.array([0.21394049, 0.12262315, 0.03891917]),
267 atol=TOLERANCE_ABSOLUTE_TESTS,
268 )
270 def test_n_dimensional_chromatic_adaptation_VonKries(self) -> None:
271 """
272 Test :func:`colour.adaptation.vonkries.chromatic_adaptation_VonKries`
273 definition n-dimensional arrays support.
274 """
276 XYZ = np.array([0.20654008, 0.12197225, 0.05136952])
277 XYZ_w = np.array([0.95045593, 1.00000000, 1.08905775])
278 XYZ_wr = np.array([0.96429568, 1.00000000, 0.82510460])
279 XYZ_a = chromatic_adaptation_VonKries(XYZ, XYZ_w, XYZ_wr)
281 XYZ = np.tile(XYZ, (6, 1))
282 XYZ_w = np.tile(XYZ_w, (6, 1))
283 XYZ_wr = np.tile(XYZ_wr, (6, 1))
284 XYZ_a = np.tile(XYZ_a, (6, 1))
285 np.testing.assert_allclose(
286 chromatic_adaptation_VonKries(XYZ, XYZ_w, XYZ_wr),
287 XYZ_a,
288 atol=TOLERANCE_ABSOLUTE_TESTS,
289 )
291 XYZ = np.reshape(XYZ, (2, 3, 3))
292 XYZ_w = np.reshape(XYZ_w, (2, 3, 3))
293 XYZ_wr = np.reshape(XYZ_wr, (2, 3, 3))
294 XYZ_a = np.reshape(XYZ_a, (2, 3, 3))
295 np.testing.assert_allclose(
296 chromatic_adaptation_VonKries(XYZ, XYZ_w, XYZ_wr),
297 XYZ_a,
298 atol=TOLERANCE_ABSOLUTE_TESTS,
299 )
301 def test_domain_range_scale_chromatic_adaptation_VonKries(self) -> None:
302 """
303 Test :func:`colour.adaptation.vonkries.chromatic_adaptation_VonKries`
304 definition domain and range scale support.
305 """
307 XYZ = np.array([0.20654008, 0.12197225, 0.05136952])
308 XYZ_w = np.array([0.95045593, 1.00000000, 1.08905775])
309 XYZ_wr = np.array([0.96429568, 1.00000000, 0.82510460])
310 XYZ_a = chromatic_adaptation_VonKries(XYZ, XYZ_w, XYZ_wr)
312 d_r = (("reference", 1), ("1", 1), ("100", 100))
313 for scale, factor in d_r:
314 with domain_range_scale(scale):
315 np.testing.assert_allclose(
316 chromatic_adaptation_VonKries(
317 XYZ * factor, XYZ_w * factor, XYZ_wr * factor
318 ),
319 XYZ_a * factor,
320 atol=TOLERANCE_ABSOLUTE_TESTS,
321 )
323 @ignore_numpy_errors
324 def test_nan_chromatic_adaptation_VonKries(self) -> None:
325 """
326 Test :func:`colour.adaptation.vonkries.chromatic_adaptation_VonKries`
327 definition nan support.
328 """
330 cases = [-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]
331 cases = np.array(list(set(product(cases, repeat=3))))
332 chromatic_adaptation_VonKries(cases, cases, cases)