Coverage for models/tests/test_oklab.py: 100%

1"""Define the unit tests for the :mod:`colour.models.oklab` 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 import Oklab_to_XYZ, XYZ_to_Oklab

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

22 "TestOklab_to_XYZ",

23]

24

25

26 class TestXYZ_to_Oklab:

27 """

28 Define :func:`colour.models.oklab.TestXYZ_to_Oklab` definition unit

29 tests methods.

30 """

31

32 def test_XYZ_to_Oklab(self) -> None:

33 """Test :func:`colour.models.oklab.XYZ_to_Oklab` definition."""

34

35 np.testing.assert_allclose(

36 XYZ_to_Oklab(np.array([0.20654008, 0.12197225, 0.05136952])),

37 np.array([0.51634019, 0.15469500, 0.06289579]),

38 atol=TOLERANCE_ABSOLUTE_TESTS,

39 )

40

41 np.testing.assert_allclose(

42 XYZ_to_Oklab(np.array([0.14222010, 0.23042768, 0.10495772])),

43 np.array([0.59910746, -0.11139207, 0.07508465]),

44 atol=TOLERANCE_ABSOLUTE_TESTS,

45 )

46

47 np.testing.assert_allclose(

48 XYZ_to_Oklab(np.array([0.96907232, 1.00000000, 1.12179215])),

49 np.array([1.00121561, 0.00899591, -0.00535107]),

50 atol=TOLERANCE_ABSOLUTE_TESTS,

51 )

52

53 def test_n_dimensional_XYZ_to_Oklab(self) -> None:

54 """

55 Test :func:`colour.models.oklab.XYZ_to_Oklab` definition

56 n-dimensional support.

57 """

58

59 XYZ = np.array([0.20654008, 0.12197225, 0.05136952])

60 Oklab = XYZ_to_Oklab(XYZ)

61

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

63 Oklab = np.tile(Oklab, (6, 1))

64 np.testing.assert_allclose(

65 XYZ_to_Oklab(XYZ), Oklab, atol=TOLERANCE_ABSOLUTE_TESTS

66 )

67

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

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

70 np.testing.assert_allclose(

71 XYZ_to_Oklab(XYZ), Oklab, atol=TOLERANCE_ABSOLUTE_TESTS

72 )

73

74 def test_domain_range_scale_XYZ_to_Oklab(self) -> None:

75 """

76 Test :func:`colour.models.oklab.XYZ_to_Oklab` definition domain and

77 range scale support.

78 """

79

80 XYZ = np.array([0.20654008, 0.12197225, 0.05136952])

81 Oklab = XYZ_to_Oklab(XYZ)

82

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

84 for scale, factor in d_r:

85 with domain_range_scale(scale):

86 np.testing.assert_allclose(

87 XYZ_to_Oklab(XYZ * factor),

88 Oklab * factor,

89 atol=TOLERANCE_ABSOLUTE_TESTS,

90 )

91

92 @ignore_numpy_errors

93 def test_nan_XYZ_to_Oklab(self) -> None:

94 """

95 Test :func:`colour.models.oklab.XYZ_to_Oklab` definition nan

96 support.

97 """

98

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

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

101 XYZ_to_Oklab(cases)

102

103

104 class TestOklab_to_XYZ:

105 """

106 Define :func:`colour.models.oklab.Oklab_to_XYZ` definition unit tests

107 methods.

108 """

109

110 def test_Oklab_to_XYZ(self) -> None:

111 """Test :func:`colour.models.oklab.Oklab_to_XYZ` definition."""

112

113 np.testing.assert_allclose(

114 Oklab_to_XYZ(np.array([0.51634019, 0.15469500, 0.06289579])),

115 np.array([0.20654008, 0.12197225, 0.05136952]),

116 atol=1e-6,

117 )

118

119 np.testing.assert_allclose(

120 Oklab_to_XYZ(np.array([0.59910746, -0.11139207, 0.07508465])),

121 np.array([0.14222010, 0.23042768, 0.10495772]),

122 atol=1e-6,

123 )

124

125 np.testing.assert_allclose(

126 Oklab_to_XYZ(np.array([1.00121561, 0.00899591, -0.00535107])),

127 np.array([0.96907232, 1.00000000, 1.12179215]),

128 atol=1e-6,

129 )

130

131 def test_n_dimensional_Oklab_to_XYZ(self) -> None:

132 """

133 Test :func:`colour.models.oklab.Oklab_to_XYZ` definition

134 n-dimensional support.

135 """

136

137 Oklab = np.array([0.51634019, 0.15469500, 0.06289579])

138 XYZ = Oklab_to_XYZ(Oklab)

139

140 Oklab = np.tile(Oklab, (6, 1))

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

142 np.testing.assert_allclose(

143 Oklab_to_XYZ(Oklab), XYZ, atol=TOLERANCE_ABSOLUTE_TESTS

144 )

145

146 Oklab = np.reshape(Oklab, (2, 3, 3))

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

148 np.testing.assert_allclose(

149 Oklab_to_XYZ(Oklab), XYZ, atol=TOLERANCE_ABSOLUTE_TESTS

150 )

151

152 def test_domain_range_scale_Oklab_to_XYZ(self) -> None:

153 """

154 Test :func:`colour.models.oklab.Oklab_to_XYZ` definition domain and

155 range scale support.

156 """

157

158 Oklab = np.array([0.51634019, 0.15469500, 0.06289579])

159 XYZ = Oklab_to_XYZ(Oklab)

160

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

162 for scale, factor in d_r:

163 with domain_range_scale(scale):

164 np.testing.assert_array_equal(

165 Oklab_to_XYZ(Oklab * factor),

166 XYZ * factor,

167 )

168

169 @ignore_numpy_errors

170 def test_nan_Oklab_to_XYZ(self) -> None:

171 """

172 Test :func:`colour.models.oklab.Oklab_to_XYZ` definition nan

173 support.

174 """

175

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

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

178 Oklab_to_XYZ(cases)