Coverage for colorimetry/tests/test_whiteness.py: 100%

1"""Define the unit tests for the :mod:`colour.colorimetry.whiteness` module."""

2

3 from __future__ import annotations

4

5 from itertools import product

6

7 import numpy as np

8

9 from colour.colorimetry import (

10 whiteness_ASTME313,

11 whiteness_Berger1959,

12 whiteness_CIE2004,

13 whiteness_Ganz1979,

14 whiteness_Stensby1968,

15 whiteness_Taube1960,

16)

17 from colour.colorimetry.whiteness import whiteness

18 from colour.constants import TOLERANCE_ABSOLUTE_TESTS

19 from colour.utilities import domain_range_scale, ignore_numpy_errors

20

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"

27

28 __all__ = [

29 "TestWhitenessBerger1959",

30 "TestWhitenessTaube1960",

31 "TestWhitenessStensby1968",

32 "TestWhitenessASTM313",

33 "TestWhitenessGanz1979",

34 "TestWhitenessCIE2004",

35 "TestWhiteness",

36]

37

38

39 class TestWhitenessBerger1959:

40 """

41 Define :func:`colour.colorimetry.whiteness.whiteness_Berger1959`

42 definition unit tests methods.

43 """

44

45 def test_whiteness_Berger1959(self) -> None:

46 """

47 Test :func:`colour.colorimetry.whiteness.whiteness_Berger1959`

48 definition.

49 """

50

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 )

59

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 )

68

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 )

77

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 """

83

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)

87

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 )

94

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 )

101

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 """

107

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)

111

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 )

120

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 """

127

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)

131

132

133 class TestWhitenessTaube1960:

134 """

135 Define :func:`colour.colorimetry.whiteness.whiteness_Taube1960`

136 definition unit tests methods.

137 """

138

139 def test_whiteness_Taube1960(self) -> None:

140 """

141 Test :func:`colour.colorimetry.whiteness.whiteness_Taube1960`

142 definition.

143 """

144

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 )

153

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 )

162

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 )

171

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 """

177

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)

181

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 )

188

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 )

195

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 """

201

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)

205

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 )

214

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 """

221

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)

225

226

227 class TestWhitenessStensby1968:

228 """

229 Define :func:`colour.colorimetry.whiteness.whiteness_Stensby1968`

230 definition unit tests methods.

231 """

232

233 def test_whiteness_Stensby1968(self) -> None:

234 """

235 Test :func:`colour.colorimetry.whiteness.whiteness_Stensby1968`

236 definition.

237 """

238

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 )

244

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 )

250

251 np.testing.assert_allclose(

252 whiteness_Stensby1968(np.array([1, 1, 1])),

253 1.00000000,

254 atol=TOLERANCE_ABSOLUTE_TESTS,

255 )

256

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 """

262

263 Lab = np.array([100.00000000, -2.46875131, -16.72486654])

264 WI = whiteness_Stensby1968(Lab)

265

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 )

271

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 )

277

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 """

283

284 Lab = np.array([100.00000000, -2.46875131, -16.72486654])

285 WI = whiteness_Stensby1968(Lab)

286

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 )

295

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 """

302

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)

306

307

308 class TestWhitenessASTM313:

309 """

310 Define :func:`colour.colorimetry.whiteness.whiteness_ASTME313`

311 definition unit tests methods.

312 """

313

314 def test_whiteness_ASTME313(self) -> None:

315 """

316 Test :func:`colour.colorimetry.whiteness.whiteness_ASTME313`

317 definition.

318 """

319

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 )

325

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 )

331

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 )

337

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 """

343

344 XYZ = np.array([95.00000000, 100.00000000, 105.00000000])

345 WI = whiteness_ASTME313(XYZ)

346

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 )

352

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 )

358

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 """

364

365 XYZ = np.array([95.00000000, 100.00000000, 105.00000000])

366 WI = whiteness_ASTME313(XYZ)

367

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 )

376

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 """

383

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)

387

388

389 class TestWhitenessGanz1979:

390 """

391 Define :func:`colour.colorimetry.whiteness.whiteness_Ganz1979`

392 definition unit tests methods.

393 """

394

395 def test_whiteness_Ganz1979(self) -> None:

396 """

397 Test :func:`colour.colorimetry.whiteness.whiteness_Ganz1979`

398 definition.

399 """

400

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 )

406

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 )

412

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 )

418

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 """

424

425 xy = np.array([0.3167, 0.3334])

426 Y = 100

427 WT = whiteness_Ganz1979(xy, Y)

428

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 )

434

435 Y = np.tile(Y, 6)

436 np.testing.assert_allclose(

437 whiteness_Ganz1979(xy, Y), WT, atol=TOLERANCE_ABSOLUTE_TESTS

438 )

439

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 )

446

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 """

452

453 xy = np.array([0.3167, 0.3334])

454 Y = 100

455 WT = whiteness_Ganz1979(xy, Y)

456

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 )

465

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 """

472

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])

476

477

478 class TestWhitenessCIE2004:

479 """

480 Define :func:`colour.colorimetry.whiteness.whiteness_CIE2004`

481 definition unit tests methods.

482 """

483

484 def test_whiteness_CIE2004(self) -> None:

485 """

486 Test :func:`colour.colorimetry.whiteness.whiteness_CIE2004`

487 definition.

488 """

489

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 )

497

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 )

505

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 )

513

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 """

519

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)

524

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 )

530

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 )

536

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 )

544

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 """

550

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)

555

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 )

564

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 """

571

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])

575

576

577 class TestWhiteness:

578 """

579 Define :func:`colour.colorimetry.whiteness.whiteness` definition unit

580 tests methods.

581 """

582

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 """

588

589 XYZ = np.array([95.00000000, 100.00000000, 105.00000000])

590 XYZ_0 = np.array([94.80966767, 100.00000000, 107.30513595])

591

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]

601

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 )