Coverage for colour/colorimetry/tests/test_transformations.py: 100%

117 statements  

« 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.colorimetry.transformations` 

3module. 

4""" 

5 

6from __future__ import annotations 

7 

8import numpy as np 

9 

10from colour.colorimetry import ( 

11 MSDS_CMFS, 

12 LMS_2_degree_cmfs_to_XYZ_2_degree_cmfs, 

13 LMS_10_degree_cmfs_to_XYZ_10_degree_cmfs, 

14 RGB_2_degree_cmfs_to_XYZ_2_degree_cmfs, 

15 RGB_10_degree_cmfs_to_LMS_10_degree_cmfs, 

16 RGB_10_degree_cmfs_to_XYZ_10_degree_cmfs, 

17) 

18from colour.constants import TOLERANCE_ABSOLUTE_TESTS 

19from colour.utilities import 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 "TestRGB_2_degree_cmfs_to_XYZ_2_degree_cmfs", 

30 "TestRGB_10_degree_cmfs_to_XYZ_10_degree_cmfs", 

31 "TestRGB_10_degree_cmfs_to_LMS_10_degree_cmfs", 

32 "TestLMS_2_degree_cmfs_to_XYZ_2_degree_cmfs", 

33 "TestLMS_10_degree_cmfs_to_XYZ_10_degree_cmfs", 

34] 

35 

36 

37class TestRGB_2_degree_cmfs_to_XYZ_2_degree_cmfs: 

38 """ 

39 Define :func:`colour.colorimetry.transformations.\ 

40RGB_2_degree_cmfs_to_XYZ_2_degree_cmfs` definition unit tests methods. 

41 """ 

42 

43 def test_RGB_2_degree_cmfs_to_XYZ_2_degree_cmfs(self) -> None: 

44 """ 

45 Test :func:`colour.colorimetry.transformations.\ 

46RGB_2_degree_cmfs_to_XYZ_2_degree_cmfs` definition. 

47 """ 

48 

49 cmfs = MSDS_CMFS["CIE 1931 2 Degree Standard Observer"] 

50 np.testing.assert_allclose( 

51 RGB_2_degree_cmfs_to_XYZ_2_degree_cmfs(435), cmfs[435], atol=0.0025 

52 ) 

53 

54 np.testing.assert_allclose( 

55 RGB_2_degree_cmfs_to_XYZ_2_degree_cmfs(545), cmfs[545], atol=0.0025 

56 ) 

57 

58 np.testing.assert_allclose( 

59 RGB_2_degree_cmfs_to_XYZ_2_degree_cmfs(700), cmfs[700], atol=0.0025 

60 ) 

61 

62 def test_n_dimensional_RGB_2_degree_cmfs_to_XYZ_2_degree_cmfs(self) -> None: 

63 """ 

64 Test :func:`colour.colorimetry.transformations.\ 

65RGB_2_degree_cmfs_to_XYZ_2_degree_cmfs` definition n-dimensional arrays 

66 support. 

67 """ 

68 

69 wl = 700 

70 XYZ = RGB_2_degree_cmfs_to_XYZ_2_degree_cmfs(wl) 

71 

72 wl = np.tile(wl, 6) 

73 XYZ = np.tile(XYZ, (6, 1)) 

74 np.testing.assert_allclose( 

75 RGB_2_degree_cmfs_to_XYZ_2_degree_cmfs(wl), 

76 XYZ, 

77 atol=TOLERANCE_ABSOLUTE_TESTS, 

78 ) 

79 

80 wl = np.reshape(wl, (2, 3)) 

81 XYZ = np.reshape(XYZ, (2, 3, 3)) 

82 np.testing.assert_allclose( 

83 RGB_2_degree_cmfs_to_XYZ_2_degree_cmfs(wl), 

84 XYZ, 

85 atol=TOLERANCE_ABSOLUTE_TESTS, 

86 ) 

87 

88 wl = np.reshape(wl, (2, 3, 1)) 

89 XYZ = np.reshape(XYZ, (2, 3, 1, 3)) 

90 np.testing.assert_allclose( 

91 RGB_2_degree_cmfs_to_XYZ_2_degree_cmfs(wl), 

92 XYZ, 

93 atol=TOLERANCE_ABSOLUTE_TESTS, 

94 ) 

95 

96 @ignore_numpy_errors 

97 def test_nan_RGB_2_degree_cmfs_to_XYZ_2_degree_cmfs(self) -> None: 

98 """ 

99 Test :func:`colour.colorimetry.transformations.\ 

100RGB_2_degree_cmfs_to_XYZ_2_degree_cmfs` definition nan support. 

101 """ 

102 

103 RGB_2_degree_cmfs_to_XYZ_2_degree_cmfs( 

104 np.array([-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]) 

105 ) 

106 

107 

108class TestRGB_10_degree_cmfs_to_XYZ_10_degree_cmfs: 

109 """ 

110 Define :func:`colour.colorimetry.transformations.\ 

111RGB_10_degree_cmfs_to_XYZ_10_degree_cmfs` definition unit tests methods. 

112 """ 

113 

114 def test_RGB_10_degree_cmfs_to_XYZ_10_degree_cmfs(self) -> None: 

115 """ 

116 Test :func:`colour.colorimetry.transformations.\ 

117RGB_10_degree_cmfs_to_XYZ_10_degree_cmfs` definition. 

118 """ 

119 

120 cmfs = MSDS_CMFS["CIE 1964 10 Degree Standard Observer"] 

121 np.testing.assert_allclose( 

122 RGB_10_degree_cmfs_to_XYZ_10_degree_cmfs(435), 

123 cmfs[435], 

124 atol=0.025, 

125 ) 

126 

127 np.testing.assert_allclose( 

128 RGB_10_degree_cmfs_to_XYZ_10_degree_cmfs(545), 

129 cmfs[545], 

130 atol=0.025, 

131 ) 

132 

133 np.testing.assert_allclose( 

134 RGB_10_degree_cmfs_to_XYZ_10_degree_cmfs(700), 

135 cmfs[700], 

136 atol=0.025, 

137 ) 

138 

139 def test_n_dimensional_RGB_10_degree_cmfs_to_XYZ_10_degree_cmfs(self) -> None: 

140 """ 

141 Test :func:`colour.colorimetry.transformations.\ 

142RGB_10_degree_cmfs_to_XYZ_10_degree_cmfs` definition n-dimensional arrays 

143 support. 

144 """ 

145 

146 wl = 700 

147 XYZ = RGB_10_degree_cmfs_to_XYZ_10_degree_cmfs(wl) 

148 

149 wl = np.tile(wl, 6) 

150 XYZ = np.tile(XYZ, (6, 1)) 

151 np.testing.assert_allclose( 

152 RGB_10_degree_cmfs_to_XYZ_10_degree_cmfs(wl), 

153 XYZ, 

154 atol=TOLERANCE_ABSOLUTE_TESTS, 

155 ) 

156 

157 wl = np.reshape(wl, (2, 3)) 

158 XYZ = np.reshape(XYZ, (2, 3, 3)) 

159 np.testing.assert_allclose( 

160 RGB_10_degree_cmfs_to_XYZ_10_degree_cmfs(wl), 

161 XYZ, 

162 atol=TOLERANCE_ABSOLUTE_TESTS, 

163 ) 

164 

165 wl = np.reshape(wl, (2, 3, 1)) 

166 XYZ = np.reshape(XYZ, (2, 3, 1, 3)) 

167 np.testing.assert_allclose( 

168 RGB_10_degree_cmfs_to_XYZ_10_degree_cmfs(wl), 

169 XYZ, 

170 atol=TOLERANCE_ABSOLUTE_TESTS, 

171 ) 

172 

173 @ignore_numpy_errors 

174 def test_nan_RGB_10_degree_cmfs_to_XYZ_10_degree_cmfs(self) -> None: 

175 """ 

176 Test :func:`colour.colorimetry.transformations.\ 

177RGB_10_degree_cmfs_to_XYZ_10_degree_cmfs` definition nan support. 

178 """ 

179 

180 RGB_10_degree_cmfs_to_XYZ_10_degree_cmfs( 

181 np.array([-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]) 

182 ) 

183 

184 

185class TestRGB_10_degree_cmfs_to_LMS_10_degree_cmfs: 

186 """ 

187 Define :func:`colour.colorimetry.transformations.\ 

188RGB_10_degree_cmfs_to_LMS_10_degree_cmfs` definition unit tests methods. 

189 """ 

190 

191 def test_RGB_10_degree_cmfs_to_LMS_10_degree_cmfs(self) -> None: 

192 """ 

193 Test :func:`colour.colorimetry.transformations.\ 

194RGB_10_degree_cmfs_to_LMS_10_degree_cmfs` definition. 

195 """ 

196 

197 cmfs = MSDS_CMFS["Stockman & Sharpe 10 Degree Cone Fundamentals"] 

198 np.testing.assert_allclose( 

199 RGB_10_degree_cmfs_to_LMS_10_degree_cmfs(435), 

200 cmfs[435], 

201 atol=0.0025, 

202 ) 

203 

204 np.testing.assert_allclose( 

205 RGB_10_degree_cmfs_to_LMS_10_degree_cmfs(545), 

206 cmfs[545], 

207 atol=0.0025, 

208 ) 

209 

210 np.testing.assert_allclose( 

211 RGB_10_degree_cmfs_to_LMS_10_degree_cmfs(700), 

212 cmfs[700], 

213 atol=0.0025, 

214 ) 

215 

216 def test_n_dimensional_RGB_10_degree_cmfs_to_LMS_10_degree_cmfs(self) -> None: 

217 """ 

218 Test :func:`colour.colorimetry.transformations.\ 

219RGB_10_degree_cmfs_to_LMS_10_degree_cmfs` definition n-dimensional arrays 

220 support. 

221 """ 

222 

223 wl = 700 

224 LMS = RGB_10_degree_cmfs_to_LMS_10_degree_cmfs(wl) 

225 

226 wl = np.tile(wl, 6) 

227 LMS = np.tile(LMS, (6, 1)) 

228 np.testing.assert_allclose( 

229 RGB_10_degree_cmfs_to_LMS_10_degree_cmfs(wl), 

230 LMS, 

231 atol=TOLERANCE_ABSOLUTE_TESTS, 

232 ) 

233 

234 wl = np.reshape(wl, (2, 3)) 

235 LMS = np.reshape(LMS, (2, 3, 3)) 

236 np.testing.assert_allclose( 

237 RGB_10_degree_cmfs_to_LMS_10_degree_cmfs(wl), 

238 LMS, 

239 atol=TOLERANCE_ABSOLUTE_TESTS, 

240 ) 

241 

242 wl = np.reshape(wl, (2, 3, 1)) 

243 LMS = np.reshape(LMS, (2, 3, 1, 3)) 

244 np.testing.assert_allclose( 

245 RGB_10_degree_cmfs_to_LMS_10_degree_cmfs(wl), 

246 LMS, 

247 atol=TOLERANCE_ABSOLUTE_TESTS, 

248 ) 

249 

250 @ignore_numpy_errors 

251 def test_nan_RGB_10_degree_cmfs_to_LMS_10_degree_cmfs(self) -> None: 

252 """ 

253 Test :func:`colour.colorimetry.transformations.\ 

254RGB_10_degree_cmfs_to_LMS_10_degree_cmfs` definition nan support. 

255 """ 

256 

257 RGB_10_degree_cmfs_to_LMS_10_degree_cmfs( 

258 np.array([-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]) 

259 ) 

260 

261 

262class TestLMS_2_degree_cmfs_to_XYZ_2_degree_cmfs: 

263 """ 

264 Define :func:`colour.colorimetry.transformations.\ 

265LMS_2_degree_cmfs_to_XYZ_2_degree_cmfs` definition unit tests methods. 

266 """ 

267 

268 def test_LMS_2_degree_cmfs_to_XYZ_2_degree_cmfs(self) -> None: 

269 """ 

270 Test :func:`colour.colorimetry.transformations.\ 

271LMS_2_degree_cmfs_to_XYZ_2_degree_cmfs` definition. 

272 """ 

273 

274 cmfs = MSDS_CMFS["CIE 2015 2 Degree Standard Observer"] 

275 np.testing.assert_allclose( 

276 LMS_2_degree_cmfs_to_XYZ_2_degree_cmfs(435), 

277 cmfs[435], 

278 atol=0.00015, 

279 ) 

280 

281 np.testing.assert_allclose( 

282 LMS_2_degree_cmfs_to_XYZ_2_degree_cmfs(545), 

283 cmfs[545], 

284 atol=0.00015, 

285 ) 

286 

287 np.testing.assert_allclose( 

288 LMS_2_degree_cmfs_to_XYZ_2_degree_cmfs(700), 

289 cmfs[700], 

290 atol=0.00015, 

291 ) 

292 

293 def test_n_dimensional_LMS_2_degree_cmfs_to_XYZ_2_degree_cmfs(self) -> None: 

294 """ 

295 Test :func:`colour.colorimetry.transformations.\ 

296LMS_2_degree_cmfs_to_XYZ_2_degree_cmfs` definition n-dimensional arrays 

297 support. 

298 """ 

299 

300 wl = 700 

301 XYZ = LMS_2_degree_cmfs_to_XYZ_2_degree_cmfs(wl) 

302 

303 wl = np.tile(wl, 6) 

304 XYZ = np.tile(XYZ, (6, 1)) 

305 np.testing.assert_allclose( 

306 LMS_2_degree_cmfs_to_XYZ_2_degree_cmfs(wl), 

307 XYZ, 

308 atol=TOLERANCE_ABSOLUTE_TESTS, 

309 ) 

310 

311 wl = np.reshape(wl, (2, 3)) 

312 XYZ = np.reshape(XYZ, (2, 3, 3)) 

313 np.testing.assert_allclose( 

314 LMS_2_degree_cmfs_to_XYZ_2_degree_cmfs(wl), 

315 XYZ, 

316 atol=TOLERANCE_ABSOLUTE_TESTS, 

317 ) 

318 

319 wl = np.reshape(wl, (2, 3, 1)) 

320 XYZ = np.reshape(XYZ, (2, 3, 1, 3)) 

321 np.testing.assert_allclose( 

322 LMS_2_degree_cmfs_to_XYZ_2_degree_cmfs(wl), 

323 XYZ, 

324 atol=TOLERANCE_ABSOLUTE_TESTS, 

325 ) 

326 

327 @ignore_numpy_errors 

328 def test_nan_LMS_2_degree_cmfs_to_XYZ_2_degree_cmfs(self) -> None: 

329 """ 

330 Test :func:`colour.colorimetry.transformations.\ 

331LMS_2_degree_cmfs_to_XYZ_2_degree_cmfs` definition nan support. 

332 """ 

333 

334 LMS_2_degree_cmfs_to_XYZ_2_degree_cmfs( 

335 np.array([-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]) 

336 ) 

337 

338 

339class TestLMS_10_degree_cmfs_to_XYZ_10_degree_cmfs: 

340 """ 

341 Define :func:`colour.colorimetry.transformations.\ 

342LMS_10_degree_cmfs_to_XYZ_10_degree_cmfs` definition unit tests methods. 

343 """ 

344 

345 def test_LMS_10_degree_cmfs_to_XYZ_10_degree_cmfs(self) -> None: 

346 """ 

347 Test :func:`colour.colorimetry.transformations.\ 

348LMS_10_degree_cmfs_to_XYZ_10_degree_cmfs` definition. 

349 """ 

350 

351 cmfs = MSDS_CMFS["CIE 2015 10 Degree Standard Observer"] 

352 np.testing.assert_allclose( 

353 LMS_10_degree_cmfs_to_XYZ_10_degree_cmfs(435), 

354 cmfs[435], 

355 atol=0.00015, 

356 ) 

357 

358 np.testing.assert_allclose( 

359 LMS_10_degree_cmfs_to_XYZ_10_degree_cmfs(545), 

360 cmfs[545], 

361 atol=0.00015, 

362 ) 

363 

364 np.testing.assert_allclose( 

365 LMS_10_degree_cmfs_to_XYZ_10_degree_cmfs(700), 

366 cmfs[700], 

367 atol=0.00015, 

368 ) 

369 

370 def test_n_dimensional_LMS_10_degree_cmfs_to_XYZ_10_degree_cmfs(self) -> None: 

371 """ 

372 Test :func:`colour.colorimetry.transformations.\ 

373LMS_10_degree_cmfs_to_XYZ_10_degree_cmfs` definition n-dimensional arrays 

374 support. 

375 """ 

376 

377 wl = 700 

378 XYZ = LMS_10_degree_cmfs_to_XYZ_10_degree_cmfs(wl) 

379 

380 wl = np.tile(wl, 6) 

381 XYZ = np.tile(XYZ, (6, 1)) 

382 np.testing.assert_allclose( 

383 LMS_10_degree_cmfs_to_XYZ_10_degree_cmfs(wl), 

384 XYZ, 

385 atol=TOLERANCE_ABSOLUTE_TESTS, 

386 ) 

387 

388 wl = np.reshape(wl, (2, 3)) 

389 XYZ = np.reshape(XYZ, (2, 3, 3)) 

390 np.testing.assert_allclose( 

391 LMS_10_degree_cmfs_to_XYZ_10_degree_cmfs(wl), 

392 XYZ, 

393 atol=TOLERANCE_ABSOLUTE_TESTS, 

394 ) 

395 

396 wl = np.reshape(wl, (2, 3, 1)) 

397 XYZ = np.reshape(XYZ, (2, 3, 1, 3)) 

398 np.testing.assert_allclose( 

399 LMS_10_degree_cmfs_to_XYZ_10_degree_cmfs(wl), 

400 XYZ, 

401 atol=TOLERANCE_ABSOLUTE_TESTS, 

402 ) 

403 

404 @ignore_numpy_errors 

405 def test_nan_LMS_10_degree_cmfs_to_XYZ_10_degree_cmfs(self) -> None: 

406 """ 

407 Test :func:`colour.colorimetry.transformations.\ 

408LMS_10_degree_cmfs_to_XYZ_10_degree_cmfs` definition nan support. 

409 """ 

410 

411 LMS_10_degree_cmfs_to_XYZ_10_degree_cmfs( 

412 np.array([-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]) 

413 )