Coverage for models/rgb/tests/test_cmyk.py: 100%

1"""Define the unit tests for the :mod:`colour.models.rgb.cmyk` module."""

2

3 from __future__ import annotations

4

5 from itertools import product

6

7 import numpy as np

8

9 from colour.constants import TOLERANCE_ABSOLUTE_TESTS

10 from colour.models.rgb.cmyk import CMY_to_CMYK, CMY_to_RGB, CMYK_to_CMY, RGB_to_CMY

11 from colour.utilities import domain_range_scale, ignore_numpy_errors

12

13 __author__ = "Colour Developers"

14 __copyright__ = "Copyright 2013 Colour Developers"

15 __license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause"

16 __maintainer__ = "Colour Developers"

17 __email__ = "colour-developers@colour-science.org"

18 __status__ = "Production"

19

20 __all__ = [

21 "TestRGB_to_CMY",

22 "TestCMY_to_RGB",

23 "TestCMY_to_CMYK",

24 "TestCMYK_to_CMY",

25]

26

27

28 class TestRGB_to_CMY:

29 """

30 Define :func:`colour.models.rgb.cmyk.RGB_to_CMY` definition unit tests

31 methods.

32 """

33

34 def test_RGB_to_CMY(self) -> None:

35 """Test :func:`colour.models.rgb.cmyk.RGB_to_CMY` definition."""

36

37 np.testing.assert_allclose(

38 RGB_to_CMY(np.array([0.45620519, 0.03081071, 0.04091952])),

39 np.array([0.54379481, 0.96918929, 0.95908048]),

40 atol=TOLERANCE_ABSOLUTE_TESTS,

41 )

42

43 np.testing.assert_allclose(

44 RGB_to_CMY(np.array([0.00000000, 0.00000000, 0.00000000])),

45 np.array([1.00000000, 1.00000000, 1.00000000]),

46 atol=TOLERANCE_ABSOLUTE_TESTS,

47 )

48

49 np.testing.assert_allclose(

50 RGB_to_CMY(np.array([1.00000000, 1.00000000, 1.00000000])),

51 np.array([0.00000000, 0.00000000, 0.00000000]),

52 atol=TOLERANCE_ABSOLUTE_TESTS,

53 )

54

55 def test_n_dimensional_RGB_to_CMY(self) -> None:

56 """

57 Test :func:`colour.models.rgb.cmyk.RGB_to_CMY` definition

58 n-dimensional arrays support.

59 """

60

61 RGB = np.array([0.45620519, 0.03081071, 0.04091952])

62 CMY = RGB_to_CMY(RGB)

63

64 RGB = np.tile(RGB, (6, 1))

65 CMY = np.tile(CMY, (6, 1))

66 np.testing.assert_allclose(RGB_to_CMY(RGB), CMY, atol=TOLERANCE_ABSOLUTE_TESTS)

67

68 RGB = np.reshape(RGB, (2, 3, 3))

69 CMY = np.reshape(CMY, (2, 3, 3))

70 np.testing.assert_allclose(RGB_to_CMY(RGB), CMY, atol=TOLERANCE_ABSOLUTE_TESTS)

71

72 def test_domain_range_scale_RGB_to_CMY(self) -> None:

73 """

74 Test :func:`colour.models.rgb.cmyk.RGB_to_CMY` definition domain and

75 range scale support.

76 """

77

78 RGB = np.array([0.45620519, 0.03081071, 0.04091952])

79 CMY = RGB_to_CMY(RGB)

80

81 d_r = (("reference", 1), ("1", 1), ("100", 100))

82 for scale, factor in d_r:

83 with domain_range_scale(scale):

84 np.testing.assert_allclose(

85 RGB_to_CMY(RGB * factor),

86 CMY * factor,

87 atol=TOLERANCE_ABSOLUTE_TESTS,

88 )

89

90 @ignore_numpy_errors

91 def test_nan_RGB_to_CMY(self) -> None:

92 """

93 Test :func:`colour.models.rgb.cmyk.RGB_to_CMY` definition nan

94 support.

95 """

96

97 cases = [-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]

98 cases = np.array(list(set(product(cases, repeat=3))))

99 RGB_to_CMY(cases)

100

101

102 class TestCMY_to_RGB:

103 """

104 Define :func:`colour.models.rgb.cmyk.CMY_to_RGB` definition unit tests

105 methods.

106 """

107

108 def test_CMY_to_RGB(self) -> None:

109 """Test :func:`colour.models.rgb.cmyk.CMY_to_RGB` definition."""

110

111 np.testing.assert_allclose(

112 CMY_to_RGB(np.array([0.54379481, 0.96918929, 0.95908048])),

113 np.array([0.45620519, 0.03081071, 0.04091952]),

114 atol=TOLERANCE_ABSOLUTE_TESTS,

115 )

116

117 np.testing.assert_allclose(

118 CMY_to_RGB(np.array([1.00000000, 1.00000000, 1.00000000])),

119 np.array([0.00000000, 0.00000000, 0.00000000]),

120 atol=TOLERANCE_ABSOLUTE_TESTS,

121 )

122

123 np.testing.assert_allclose(

124 CMY_to_RGB(np.array([0.00000000, 0.00000000, 0.00000000])),

125 np.array([1.00000000, 1.00000000, 1.00000000]),

126 atol=TOLERANCE_ABSOLUTE_TESTS,

127 )

128

129 def test_n_dimensional_CMY_to_RGB(self) -> None:

130 """

131 Test :func:`colour.models.rgb.cmyk.CMY_to_RGB` definition

132 n-dimensional arrays support.

133 """

134

135 CMY = np.array([0.54379481, 0.96918929, 0.95908048])

136 RGB = CMY_to_RGB(CMY)

137

138 CMY = np.tile(CMY, (6, 1))

139 RGB = np.tile(RGB, (6, 1))

140 np.testing.assert_allclose(CMY_to_RGB(CMY), RGB, atol=TOLERANCE_ABSOLUTE_TESTS)

141

142 CMY = np.reshape(CMY, (2, 3, 3))

143 RGB = np.reshape(RGB, (2, 3, 3))

144 np.testing.assert_allclose(CMY_to_RGB(CMY), RGB, atol=TOLERANCE_ABSOLUTE_TESTS)

145

146 def test_domain_range_scale_CMY_to_RGB(self) -> None:

147 """

148 Test :func:`colour.models.rgb.cmyk.CMY_to_RGB` definition domain and

149 range scale support.

150 """

151

152 CMY = np.array([0.54379481, 0.96918929, 0.95908048])

153 RGB = CMY_to_RGB(CMY)

154

155 d_r = (("reference", 1), ("1", 1), ("100", 100))

156 for scale, factor in d_r:

157 with domain_range_scale(scale):

158 np.testing.assert_allclose(

159 CMY_to_RGB(CMY * factor),

160 RGB * factor,

161 atol=TOLERANCE_ABSOLUTE_TESTS,

162 )

163

164 @ignore_numpy_errors

165 def test_nan_CMY_to_RGB(self) -> None:

166 """Test :func:`colour.models.rgb.cmyk.CMY_to_RGB` definition nan support."""

167

168 cases = [-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]

169 cases = np.array(list(set(product(cases, repeat=3))))

170 CMY_to_RGB(cases)

171

172

173 class TestCMY_to_CMYK:

174 """

175 Define :func:`colour.models.rgb.cmyk.CMY_to_CMYK` definition unit tests

176 methods.

177 """

178

179 def test_CMY_to_CMYK(self) -> None:

180 """Test :func:`colour.models.rgb.cmyk.CMY_to_CMYK` definition."""

181

182 np.testing.assert_allclose(

183 CMY_to_CMYK(np.array([0.54379481, 0.96918929, 0.95908048])),

184 np.array([0.00000000, 0.93246304, 0.91030457, 0.54379481]),

185 atol=TOLERANCE_ABSOLUTE_TESTS,

186 )

187

188 np.testing.assert_allclose(

189 CMY_to_CMYK(np.array([0.15000000, 1.00000000, 1.00000000])),

190 np.array([0.00000000, 1.00000000, 1.00000000, 0.15000000]),

191 atol=TOLERANCE_ABSOLUTE_TESTS,

192 )

193

194 np.testing.assert_allclose(

195 CMY_to_CMYK(np.array([0.15000000, 0.00000000, 0.00000000])),

196 np.array([0.15000000, 0.00000000, 0.00000000, 0.00000000]),

197 atol=TOLERANCE_ABSOLUTE_TESTS,

198 )

199

200 def test_n_dimensional_CMY_to_CMYK(self) -> None:

201 """

202 Test :func:`colour.models.rgb.cmyk.CMY_to_CMYK` definition

203 n-dimensional arrays support.

204 """

205

206 CMY = np.array([0.54379481, 0.96918929, 0.95908048])

207 CMYK = CMY_to_CMYK(CMY)

208

209 CMY = np.tile(CMY, (6, 1))

210 CMYK = np.tile(CMYK, (6, 1))

211 np.testing.assert_allclose(

212 CMY_to_CMYK(CMY), CMYK, atol=TOLERANCE_ABSOLUTE_TESTS

213 )

214

215 CMY = np.reshape(CMY, (2, 3, 3))

216 CMYK = np.reshape(CMYK, (2, 3, 4))

217 np.testing.assert_allclose(

218 CMY_to_CMYK(CMY), CMYK, atol=TOLERANCE_ABSOLUTE_TESTS

219 )

220

221 def test_domain_range_scale_CMY_to_CMYK(self) -> None:

222 """

223 Test :func:`colour.models.rgb.cmyk.CMY_to_CMYK` definition domain and

224 range scale support.

225 """

226

227 CMY = np.array([0.54379481, 0.96918929, 0.95908048])

228 CMYK = CMY_to_CMYK(CMY)

229

230 d_r = (("reference", 1), ("1", 1), ("100", 100))

231 for scale, factor in d_r:

232 with domain_range_scale(scale):

233 np.testing.assert_allclose(

234 CMY_to_CMYK(CMY * factor),

235 CMYK * factor,

236 atol=TOLERANCE_ABSOLUTE_TESTS,

237 )

238

239 @ignore_numpy_errors

240 def test_nan_CMY_to_CMYK(self) -> None:

241 """

242 Test :func:`colour.models.rgb.cmyk.CMY_to_CMYK` definition nan

243 support.

244 """

245

246 cases = [-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]

247 cases = np.array(list(set(product(cases, repeat=3))))

248 CMY_to_CMYK(cases)

249

250

251 class TestCMYK_to_CMY:

252 """

253 Define :func:`colour.models.rgb.cmyk.CMYK_to_CMY` definition unit tests

254 methods.

255 """

256

257 def test_CMYK_to_CMY(self) -> None:

258 """Test :func:`colour.models.rgb.cmyk.CMYK_to_CMY` definition."""

259

260 np.testing.assert_allclose(

261 CMYK_to_CMY(np.array([0.00000000, 0.93246304, 0.91030457, 0.54379481])),

262 np.array([0.54379481, 0.96918929, 0.95908048]),

263 atol=TOLERANCE_ABSOLUTE_TESTS,

264 )

265

266 np.testing.assert_allclose(

267 CMYK_to_CMY(np.array([0.00000000, 1.00000000, 1.00000000, 0.15000000])),

268 np.array([0.15000000, 1.00000000, 1.00000000]),

269 atol=TOLERANCE_ABSOLUTE_TESTS,

270 )

271

272 np.testing.assert_allclose(

273 CMYK_to_CMY(np.array([0.15000000, 0.00000000, 0.00000000, 0.00000000])),

274 np.array([0.15000000, 0.00000000, 0.00000000]),

275 atol=TOLERANCE_ABSOLUTE_TESTS,

276 )

277

278 def test_n_dimensional_CMYK_to_CMY(self) -> None:

279 """

280 Test :func:`colour.models.rgb.cmyk.CMYK_to_CMY` definition

281 n-dimensional arrays support.

282 """

283

284 CMYK = np.array([0.00000000, 0.93246304, 0.91030457, 0.54379481])

285 CMY = CMYK_to_CMY(CMYK)

286

287 CMYK = np.tile(CMYK, (6, 1))

288 CMY = np.tile(CMY, (6, 1))

289 np.testing.assert_allclose(

290 CMYK_to_CMY(CMYK), CMY, atol=TOLERANCE_ABSOLUTE_TESTS

291 )

292

293 CMYK = np.reshape(CMYK, (2, 3, 4))

294 CMY = np.reshape(CMY, (2, 3, 3))

295 np.testing.assert_allclose(

296 CMYK_to_CMY(CMYK), CMY, atol=TOLERANCE_ABSOLUTE_TESTS

297 )

298

299 def test_domain_range_scale_CMYK_to_CMY(self) -> None:

300 """

301 Test :func:`colour.models.rgb.cmyk.CMYK_to_CMY` definition domain and

302 range scale support.

303 """

304

305 CMYK = np.array([0.00000000, 0.93246304, 0.91030457, 0.54379481])

306 CMY = CMYK_to_CMY(CMYK)

307

308 d_r = (("reference", 1), ("1", 1), ("100", 100))

309 for scale, factor in d_r:

310 with domain_range_scale(scale):

311 np.testing.assert_allclose(

312 CMYK_to_CMY(CMYK * factor),

313 CMY * factor,

314 atol=TOLERANCE_ABSOLUTE_TESTS,

315 )

316

317 @ignore_numpy_errors

318 def test_nan_CMYK_to_CMY(self) -> None:

319 """

320 Test :func:`colour.models.rgb.cmyk.CMYK_to_CMY` definition nan

321 support.

322 """

323

324 cases = [-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]

325 cases = np.array(list(set(product(cases, repeat=4))))

326 CMYK_to_CMY(cases)