Coverage for colorimetry/tests/test_whiteness.py: 100%
203 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.colorimetry.whiteness` module."""
3from __future__ import annotations
5from itertools import product
7import numpy as np
9from colour.colorimetry import (
10 whiteness_ASTME313,
11 whiteness_Berger1959,
12 whiteness_CIE2004,
13 whiteness_Ganz1979,
14 whiteness_Stensby1968,
15 whiteness_Taube1960,
16)
17from colour.colorimetry.whiteness import whiteness
18from colour.constants import TOLERANCE_ABSOLUTE_TESTS
19from colour.utilities import domain_range_scale, ignore_numpy_errors
21__author__ = "Colour Developers"
22__copyright__ = "Copyright 2013 Colour Developers"
23__license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause"
24__maintainer__ = "Colour Developers"
25__email__ = "colour-developers@colour-science.org"
26__status__ = "Production"
28__all__ = [
29 "TestWhitenessBerger1959",
30 "TestWhitenessTaube1960",
31 "TestWhitenessStensby1968",
32 "TestWhitenessASTM313",
33 "TestWhitenessGanz1979",
34 "TestWhitenessCIE2004",
35 "TestWhiteness",
36]
39class TestWhitenessBerger1959:
40 """
41 Define :func:`colour.colorimetry.whiteness.whiteness_Berger1959`
42 definition unit tests methods.
43 """
45 def test_whiteness_Berger1959(self) -> None:
46 """
47 Test :func:`colour.colorimetry.whiteness.whiteness_Berger1959`
48 definition.
49 """
51 np.testing.assert_allclose(
52 whiteness_Berger1959(
53 np.array([95.00000000, 100.00000000, 105.00000000]),
54 np.array([94.80966767, 100.00000000, 107.30513595]),
55 ),
56 30.36380179,
57 atol=TOLERANCE_ABSOLUTE_TESTS,
58 )
60 np.testing.assert_allclose(
61 whiteness_Berger1959(
62 np.array([105.00000000, 100.00000000, 95.00000000]),
63 np.array([94.80966767, 100.00000000, 107.30513595]),
64 ),
65 5.530469280673941,
66 atol=TOLERANCE_ABSOLUTE_TESTS,
67 )
69 np.testing.assert_allclose(
70 whiteness_Berger1959(
71 np.array([100.00000000, 100.00000000, 100.00000000]),
72 np.array([100.00000000, 100.00000000, 100.00000000]),
73 ),
74 33.300000000000011,
75 atol=TOLERANCE_ABSOLUTE_TESTS,
76 )
78 def test_n_dimensional_whiteness_Berger1959(self) -> None:
79 """
80 Test :func:`colour.colorimetry.whiteness.whiteness_Berger1959`
81 definition n_dimensional arrays support.
82 """
84 XYZ = np.array([95.00000000, 100.00000000, 105.00000000])
85 XYZ_0 = np.array([94.80966767, 100.00000000, 107.30513595])
86 W = whiteness_Berger1959(XYZ, XYZ_0)
88 XYZ = np.tile(XYZ, (6, 1))
89 XYZ_0 = np.tile(XYZ_0, (6, 1))
90 W = np.tile(W, 6)
91 np.testing.assert_allclose(
92 whiteness_Berger1959(XYZ, XYZ_0), W, atol=TOLERANCE_ABSOLUTE_TESTS
93 )
95 XYZ = np.reshape(XYZ, (2, 3, 3))
96 XYZ_0 = np.reshape(XYZ_0, (2, 3, 3))
97 W = np.reshape(W, (2, 3))
98 np.testing.assert_allclose(
99 whiteness_Berger1959(XYZ, XYZ_0), W, atol=TOLERANCE_ABSOLUTE_TESTS
100 )
102 def test_domain_range_scale_whiteness_Berger1959(self) -> None:
103 """
104 Test :func:`colour.colorimetry.whiteness.whiteness_Berger1959`
105 definition domain and range scale support.
106 """
108 XYZ = np.array([95.00000000, 100.00000000, 105.00000000])
109 XYZ_0 = np.array([94.80966767, 100.00000000, 107.30513595])
110 W = whiteness_Berger1959(XYZ, XYZ_0)
112 d_r = (("reference", 1), ("1", 0.01), ("100", 1))
113 for scale, factor in d_r:
114 with domain_range_scale(scale):
115 np.testing.assert_allclose(
116 whiteness_Berger1959(XYZ * factor, XYZ_0 * factor),
117 W * factor,
118 atol=TOLERANCE_ABSOLUTE_TESTS,
119 )
121 @ignore_numpy_errors
122 def test_nan_whiteness_Berger1959(self) -> None:
123 """
124 Test :func:`colour.colorimetry.whiteness.whiteness_Berger1959`
125 definition nan support.
126 """
128 cases = [-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]
129 cases = np.array(list(set(product(cases, repeat=3))))
130 whiteness_Berger1959(cases, cases)
133class TestWhitenessTaube1960:
134 """
135 Define :func:`colour.colorimetry.whiteness.whiteness_Taube1960`
136 definition unit tests methods.
137 """
139 def test_whiteness_Taube1960(self) -> None:
140 """
141 Test :func:`colour.colorimetry.whiteness.whiteness_Taube1960`
142 definition.
143 """
145 np.testing.assert_allclose(
146 whiteness_Taube1960(
147 np.array([95.00000000, 100.00000000, 105.00000000]),
148 np.array([94.80966767, 100.00000000, 107.30513595]),
149 ),
150 91.407173833416152,
151 atol=TOLERANCE_ABSOLUTE_TESTS,
152 )
154 np.testing.assert_allclose(
155 whiteness_Taube1960(
156 np.array([105.00000000, 100.00000000, 95.00000000]),
157 np.array([94.80966767, 100.00000000, 107.30513595]),
158 ),
159 54.130300134995593,
160 atol=TOLERANCE_ABSOLUTE_TESTS,
161 )
163 np.testing.assert_allclose(
164 whiteness_Taube1960(
165 np.array([100.00000000, 100.00000000, 100.00000000]),
166 np.array([100.00000000, 100.00000000, 100.00000000]),
167 ),
168 100.0,
169 atol=TOLERANCE_ABSOLUTE_TESTS,
170 )
172 def test_n_dimensional_whiteness_Taube1960(self) -> None:
173 """
174 Test :func:`colour.colorimetry.whiteness.whiteness_Taube1960`
175 definition n_dimensional arrays support.
176 """
178 XYZ = np.array([95.00000000, 100.00000000, 105.00000000])
179 XYZ_0 = np.array([94.80966767, 100.00000000, 107.30513595])
180 WI = whiteness_Taube1960(XYZ, XYZ_0)
182 XYZ = np.tile(XYZ, (6, 1))
183 XYZ_0 = np.tile(XYZ_0, (6, 1))
184 WI = np.tile(WI, 6)
185 np.testing.assert_allclose(
186 whiteness_Taube1960(XYZ, XYZ_0), WI, atol=TOLERANCE_ABSOLUTE_TESTS
187 )
189 XYZ = np.reshape(XYZ, (2, 3, 3))
190 XYZ_0 = np.reshape(XYZ_0, (2, 3, 3))
191 WI = np.reshape(WI, (2, 3))
192 np.testing.assert_allclose(
193 whiteness_Taube1960(XYZ, XYZ_0), WI, atol=TOLERANCE_ABSOLUTE_TESTS
194 )
196 def test_domain_range_scale_whiteness_Taube1960(self) -> None:
197 """
198 Test :func:`colour.colorimetry.whiteness.whiteness_Taube1960`
199 definition domain and range scale support.
200 """
202 XYZ = np.array([95.00000000, 100.00000000, 105.00000000])
203 XYZ_0 = np.array([94.80966767, 100.00000000, 107.30513595])
204 WI = whiteness_Taube1960(XYZ, XYZ_0)
206 d_r = (("reference", 1), ("1", 0.01), ("100", 1))
207 for scale, factor in d_r:
208 with domain_range_scale(scale):
209 np.testing.assert_allclose(
210 whiteness_Taube1960(XYZ * factor, XYZ_0 * factor),
211 WI * factor,
212 atol=TOLERANCE_ABSOLUTE_TESTS,
213 )
215 @ignore_numpy_errors
216 def test_nan_whiteness_Berger1959(self) -> None:
217 """
218 Test :func:`colour.colorimetry.whiteness.whiteness_Berger1959`
219 definition nan support.
220 """
222 cases = [-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]
223 cases = np.array(list(set(product(cases, repeat=3))))
224 whiteness_Berger1959(cases, cases)
227class TestWhitenessStensby1968:
228 """
229 Define :func:`colour.colorimetry.whiteness.whiteness_Stensby1968`
230 definition unit tests methods.
231 """
233 def test_whiteness_Stensby1968(self) -> None:
234 """
235 Test :func:`colour.colorimetry.whiteness.whiteness_Stensby1968`
236 definition.
237 """
239 np.testing.assert_allclose(
240 whiteness_Stensby1968(np.array([100.00000000, -2.46875131, -16.72486654])),
241 142.76834569,
242 atol=TOLERANCE_ABSOLUTE_TESTS,
243 )
245 np.testing.assert_allclose(
246 whiteness_Stensby1968(np.array([100.00000000, 14.40943727, -9.61394885])),
247 172.07015836,
248 atol=TOLERANCE_ABSOLUTE_TESTS,
249 )
251 np.testing.assert_allclose(
252 whiteness_Stensby1968(np.array([1, 1, 1])),
253 1.00000000,
254 atol=TOLERANCE_ABSOLUTE_TESTS,
255 )
257 def test_n_dimensional_whiteness_Stensby1968(self) -> None:
258 """
259 Test :func:`colour.colorimetry.whiteness.whiteness_Stensby1968`
260 definition n_dimensional arrays support.
261 """
263 Lab = np.array([100.00000000, -2.46875131, -16.72486654])
264 WI = whiteness_Stensby1968(Lab)
266 Lab = np.tile(Lab, (6, 1))
267 WI = np.tile(WI, 6)
268 np.testing.assert_allclose(
269 whiteness_Stensby1968(Lab), WI, atol=TOLERANCE_ABSOLUTE_TESTS
270 )
272 Lab = np.reshape(Lab, (2, 3, 3))
273 WI = np.reshape(WI, (2, 3))
274 np.testing.assert_allclose(
275 whiteness_Stensby1968(Lab), WI, atol=TOLERANCE_ABSOLUTE_TESTS
276 )
278 def test_domain_range_scale_whiteness_Stensby1968(self) -> None:
279 """
280 Test :func:`colour.colorimetry.whiteness.whiteness_Stensby1968`
281 definition domain and range scale support.
282 """
284 Lab = np.array([100.00000000, -2.46875131, -16.72486654])
285 WI = whiteness_Stensby1968(Lab)
287 d_r = (("reference", 1), ("1", 0.01), ("100", 1))
288 for scale, factor in d_r:
289 with domain_range_scale(scale):
290 np.testing.assert_allclose(
291 whiteness_Stensby1968(Lab * factor),
292 WI * factor,
293 atol=TOLERANCE_ABSOLUTE_TESTS,
294 )
296 @ignore_numpy_errors
297 def test_nan_whiteness_Stensby1968(self) -> None:
298 """
299 Test :func:`colour.colorimetry.whiteness.whiteness_Stensby1968`
300 definition nan support.
301 """
303 cases = [-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]
304 cases = np.array(list(set(product(cases, repeat=3))))
305 whiteness_Stensby1968(cases)
308class TestWhitenessASTM313:
309 """
310 Define :func:`colour.colorimetry.whiteness.whiteness_ASTME313`
311 definition unit tests methods.
312 """
314 def test_whiteness_ASTME313(self) -> None:
315 """
316 Test :func:`colour.colorimetry.whiteness.whiteness_ASTME313`
317 definition.
318 """
320 np.testing.assert_allclose(
321 whiteness_ASTME313(np.array([95.00000000, 100.00000000, 105.00000000])),
322 55.740000000000009,
323 atol=TOLERANCE_ABSOLUTE_TESTS,
324 )
326 np.testing.assert_allclose(
327 whiteness_ASTME313(np.array([105.00000000, 100.00000000, 95.00000000])),
328 21.860000000000014,
329 atol=TOLERANCE_ABSOLUTE_TESTS,
330 )
332 np.testing.assert_allclose(
333 whiteness_ASTME313(np.array([100.00000000, 100.00000000, 100.00000000])),
334 38.800000000000011,
335 atol=TOLERANCE_ABSOLUTE_TESTS,
336 )
338 def test_n_dimensional_whiteness_ASTME313(self) -> None:
339 """
340 Test :func:`colour.colorimetry.whiteness.whiteness_ASTME313`
341 definition n_dimensional arrays support.
342 """
344 XYZ = np.array([95.00000000, 100.00000000, 105.00000000])
345 WI = whiteness_ASTME313(XYZ)
347 XYZ = np.tile(XYZ, (6, 1))
348 WI = np.tile(WI, 6)
349 np.testing.assert_allclose(
350 whiteness_ASTME313(XYZ), WI, atol=TOLERANCE_ABSOLUTE_TESTS
351 )
353 XYZ = np.reshape(XYZ, (2, 3, 3))
354 WI = np.reshape(WI, (2, 3))
355 np.testing.assert_allclose(
356 whiteness_ASTME313(XYZ), WI, atol=TOLERANCE_ABSOLUTE_TESTS
357 )
359 def test_domain_range_scale_whiteness_ASTME313(self) -> None:
360 """
361 Test :func:`colour.colorimetry.whiteness.whiteness_ASTME313`
362 definition domain and range scale support.
363 """
365 XYZ = np.array([95.00000000, 100.00000000, 105.00000000])
366 WI = whiteness_ASTME313(XYZ)
368 d_r = (("reference", 1), ("1", 0.01), ("100", 1))
369 for scale, factor in d_r:
370 with domain_range_scale(scale):
371 np.testing.assert_allclose(
372 whiteness_ASTME313(XYZ * factor),
373 WI * factor,
374 atol=TOLERANCE_ABSOLUTE_TESTS,
375 )
377 @ignore_numpy_errors
378 def test_nan_whiteness_ASTME313(self) -> None:
379 """
380 Test :func:`colour.colorimetry.whiteness.whiteness_ASTME313`
381 definition nan support.
382 """
384 cases = [-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]
385 cases = np.array(list(set(product(cases, repeat=3))))
386 whiteness_ASTME313(cases)
389class TestWhitenessGanz1979:
390 """
391 Define :func:`colour.colorimetry.whiteness.whiteness_Ganz1979`
392 definition unit tests methods.
393 """
395 def test_whiteness_Ganz1979(self) -> None:
396 """
397 Test :func:`colour.colorimetry.whiteness.whiteness_Ganz1979`
398 definition.
399 """
401 np.testing.assert_allclose(
402 whiteness_Ganz1979(np.array([0.3139, 0.3311]), 100),
403 np.array([99.33176520, 1.76108290]),
404 atol=TOLERANCE_ABSOLUTE_TESTS,
405 )
407 np.testing.assert_allclose(
408 whiteness_Ganz1979(np.array([0.3500, 0.3334]), 100),
409 np.array([23.38525400, -32.66182560]),
410 atol=TOLERANCE_ABSOLUTE_TESTS,
411 )
413 np.testing.assert_allclose(
414 whiteness_Ganz1979(np.array([0.3334, 0.3334]), 100),
415 np.array([54.39939920, -16.04152380]),
416 atol=TOLERANCE_ABSOLUTE_TESTS,
417 )
419 def test_n_dimensional_whiteness_Ganz1979(self) -> None:
420 """
421 Test :func:`colour.colorimetry.whiteness.whiteness_Ganz1979`
422 definition n_dimensional arrays support.
423 """
425 xy = np.array([0.3167, 0.3334])
426 Y = 100
427 WT = whiteness_Ganz1979(xy, Y)
429 xy = np.tile(xy, (6, 1))
430 WT = np.tile(WT, (6, 1))
431 np.testing.assert_allclose(
432 whiteness_Ganz1979(xy, Y), WT, atol=TOLERANCE_ABSOLUTE_TESTS
433 )
435 Y = np.tile(Y, 6)
436 np.testing.assert_allclose(
437 whiteness_Ganz1979(xy, Y), WT, atol=TOLERANCE_ABSOLUTE_TESTS
438 )
440 xy = np.reshape(xy, (2, 3, 2))
441 Y = np.reshape(Y, (2, 3))
442 WT = np.reshape(WT, (2, 3, 2))
443 np.testing.assert_allclose(
444 whiteness_Ganz1979(xy, Y), WT, atol=TOLERANCE_ABSOLUTE_TESTS
445 )
447 def test_domain_range_scale_whiteness_Ganz1979(self) -> None:
448 """
449 Test :func:`colour.colorimetry.whiteness.whiteness_Ganz1979`
450 definition domain and range scale support.
451 """
453 xy = np.array([0.3167, 0.3334])
454 Y = 100
455 WT = whiteness_Ganz1979(xy, Y)
457 d_r = (("reference", 1), ("1", 0.01), ("100", 1))
458 for scale, factor in d_r:
459 with domain_range_scale(scale):
460 np.testing.assert_allclose(
461 whiteness_Ganz1979(xy, Y * factor),
462 WT * factor,
463 atol=TOLERANCE_ABSOLUTE_TESTS,
464 )
466 @ignore_numpy_errors
467 def test_nan_whiteness_Ganz1979(self) -> None:
468 """
469 Test :func:`colour.colorimetry.whiteness.whiteness_Ganz1979`
470 definition nan support.
471 """
473 cases = [-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]
474 cases = np.array(list(set(product(cases, repeat=3))))
475 whiteness_Ganz1979(cases[..., 0:2], cases[..., 0])
478class TestWhitenessCIE2004:
479 """
480 Define :func:`colour.colorimetry.whiteness.whiteness_CIE2004`
481 definition unit tests methods.
482 """
484 def test_whiteness_CIE2004(self) -> None:
485 """
486 Test :func:`colour.colorimetry.whiteness.whiteness_CIE2004`
487 definition.
488 """
490 np.testing.assert_allclose(
491 whiteness_CIE2004(
492 np.array([0.3139, 0.3311]), 100, np.array([0.3139, 0.3311])
493 ),
494 np.array([100.00000000, 0.00000000]),
495 atol=TOLERANCE_ABSOLUTE_TESTS,
496 )
498 np.testing.assert_allclose(
499 whiteness_CIE2004(
500 np.array([0.3500, 0.3334]), 100, np.array([0.3139, 0.3311])
501 ),
502 np.array([67.21000000, -34.60500000]),
503 atol=TOLERANCE_ABSOLUTE_TESTS,
504 )
506 np.testing.assert_allclose(
507 whiteness_CIE2004(
508 np.array([0.3334, 0.3334]), 100, np.array([0.3139, 0.3311])
509 ),
510 np.array([80.49000000, -18.00500000]),
511 atol=TOLERANCE_ABSOLUTE_TESTS,
512 )
514 def test_n_dimensional_whiteness_CIE2004(self) -> None:
515 """
516 Test :func:`colour.colorimetry.whiteness.whiteness_CIE2004`
517 definition n_dimensional arrays support.
518 """
520 xy = np.array([0.3167, 0.3334])
521 Y = 100
522 xy_n = np.array([0.3139, 0.3311])
523 WT = whiteness_CIE2004(xy, Y, xy_n)
525 xy = np.tile(xy, (6, 1))
526 WT = np.tile(WT, (6, 1))
527 np.testing.assert_allclose(
528 whiteness_CIE2004(xy, Y, xy_n), WT, atol=TOLERANCE_ABSOLUTE_TESTS
529 )
531 Y = np.tile(Y, 6)
532 xy_n = np.tile(xy_n, (6, 1))
533 np.testing.assert_allclose(
534 whiteness_CIE2004(xy, Y, xy_n), WT, atol=TOLERANCE_ABSOLUTE_TESTS
535 )
537 xy = np.reshape(xy, (2, 3, 2))
538 Y = np.reshape(Y, (2, 3))
539 xy_n = np.reshape(xy_n, (2, 3, 2))
540 WT = np.reshape(WT, (2, 3, 2))
541 np.testing.assert_allclose(
542 whiteness_CIE2004(xy, Y, xy_n), WT, atol=TOLERANCE_ABSOLUTE_TESTS
543 )
545 def test_domain_range_scale_whiteness_CIE2004(self) -> None:
546 """
547 Test :func:`colour.colorimetry.whiteness.whiteness_CIE2004`
548 definition domain and range scale support.
549 """
551 xy = np.array([0.3167, 0.3334])
552 Y = 100
553 xy_n = np.array([0.3139, 0.3311])
554 WT = whiteness_CIE2004(xy, Y, xy_n)
556 d_r = (("reference", 1), ("1", 0.01), ("100", 1))
557 for scale, factor in d_r:
558 with domain_range_scale(scale):
559 np.testing.assert_allclose(
560 whiteness_CIE2004(xy, Y * factor, xy_n),
561 WT * factor,
562 atol=TOLERANCE_ABSOLUTE_TESTS,
563 )
565 @ignore_numpy_errors
566 def test_nan_whiteness_CIE2004(self) -> None:
567 """
568 Test :func:`colour.colorimetry.whiteness.whiteness_CIE2004`
569 definition nan support.
570 """
572 cases = [-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]
573 cases = np.array(list(set(product(cases, repeat=3))))
574 whiteness_CIE2004(cases[..., 0:2], cases[..., 0], cases[..., 0:2])
577class TestWhiteness:
578 """
579 Define :func:`colour.colorimetry.whiteness.whiteness` definition unit
580 tests methods.
581 """
583 def test_domain_range_scale_whiteness(self) -> None:
584 """
585 Test :func:`colour.colorimetry.whiteness.whiteness` definition domain
586 and range scale support.
587 """
589 XYZ = np.array([95.00000000, 100.00000000, 105.00000000])
590 XYZ_0 = np.array([94.80966767, 100.00000000, 107.30513595])
592 m = (
593 "Berger 1959",
594 "Taube 1960",
595 "Stensby 1968",
596 "ASTM E313",
597 "Ganz 1979",
598 "CIE 2004",
599 )
600 v = [whiteness(XYZ, XYZ_0, method) for method in m]
602 d_r = (("reference", 1), ("1", 0.01), ("100", 1))
603 for method, value in zip(m, v, strict=True):
604 for scale, factor in d_r:
605 with domain_range_scale(scale):
606 np.testing.assert_allclose(
607 whiteness(XYZ * factor, XYZ_0 * factor, method),
608 value * factor,
609 atol=TOLERANCE_ABSOLUTE_TESTS,
610 )