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

1"""Define the unit tests for the :mod:`colour.models.rgb.ycocg` 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 import RGB_to_YCoCg, YCoCg_to_RGB

11 from colour.utilities import 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__ = "Development"

19

20 __all__ = [

21 "TestRGB_to_YCoCg",

22 "TestYCoCg_to_RGB",

23]

24

25

26 class TestRGB_to_YCoCg:

27 """

28 Define :func:`colour.models.rgb.ycocg.RGB_to_YCoCg` definition unit tests

29 methods.

30 """

31

32 def test_RGB_to_YCoCg(self) -> None:

33 """Test :func:`colour.models.rgb.ycocg.RGB_to_YCoCg` definition."""

34

35 np.testing.assert_array_equal(

36 RGB_to_YCoCg(np.array([0.75, 0.75, 0.0])),

37 np.array([0.5625, 0.375, 0.1875]),

38 )

39

40 np.testing.assert_array_equal(

41 RGB_to_YCoCg(np.array([0.25, 0.5, 0.75])),

42 np.array([0.5, -0.25, 0.0]),

43 )

44

45 np.testing.assert_array_equal(

46 RGB_to_YCoCg(np.array([0.0, 0.75, 0.75])),

47 np.array([0.5625, -0.375, 0.1875]),

48 )

49

50 def test_n_dimensional_RGB_to_YCoCg(self) -> None:

51 """

52 Test :func:`colour.models.rgb.ycocg.RGB_to_YCoCg` definition

53 n-dimensional arrays support.

54 """

55

56 RGB = np.array([0.75, 0.75, 0.0])

57 YCoCg = RGB_to_YCoCg(RGB)

58

59 RGB = np.tile(RGB, 4)

60 RGB = np.reshape(RGB, (4, 3))

61 YCoCg = np.tile(YCoCg, 4)

62 YCoCg = np.reshape(YCoCg, (4, 3))

63 np.testing.assert_allclose(

64 RGB_to_YCoCg(RGB), YCoCg, atol=TOLERANCE_ABSOLUTE_TESTS

65 )

66

67 RGB = np.tile(RGB, 4)

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

69 YCoCg = np.tile(YCoCg, 4)

70 YCoCg = np.reshape(YCoCg, (4, 4, 3))

71 np.testing.assert_allclose(

72 RGB_to_YCoCg(RGB), YCoCg, atol=TOLERANCE_ABSOLUTE_TESTS

73 )

74

75 RGB = np.tile(RGB, 4)

76 RGB = np.reshape(RGB, (4, 4, 4, 3))

77 YCoCg = np.tile(YCoCg, 4)

78 YCoCg = np.reshape(YCoCg, (4, 4, 4, 3))

79 np.testing.assert_allclose(

80 RGB_to_YCoCg(RGB), YCoCg, atol=TOLERANCE_ABSOLUTE_TESTS

81 )

82

83 @ignore_numpy_errors

84 def test_nan_RGB_to_YCoCg(self) -> None:

85 """

86 Test :func:`colour.models.rgb.ycocg.RGB_to_YCoCg` definition nan

87 support.

88 """

89

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

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

92 RGB_to_YCoCg(cases)

93

94

95 class TestYCoCg_to_RGB:

96 """

97 Define :func:`colour.models.rgb.ycocg.YCoCg_to_RGB` definition unit tests

98 methods.

99 """

100

101 def test_YCoCg_to_RGB(self) -> None:

102 """Test :func:`colour.models.rgb.ycocg.YCoCg_to_RGB` definition."""

103

104 np.testing.assert_array_equal(

105 YCoCg_to_RGB(np.array([0.5625, 0.375, 0.1875])),

106 np.array([0.75, 0.75, 0.0]),

107 )

108

109 np.testing.assert_array_equal(

110 YCoCg_to_RGB(np.array([0.5, -0.25, 0.0])),

111 np.array([0.25, 0.5, 0.75]),

112 )

113

114 np.testing.assert_array_equal(

115 YCoCg_to_RGB(np.array([0.5625, -0.375, 0.1875])),

116 np.array([0.0, 0.75, 0.75]),

117 )

118

119 def test_n_dimensional_YCoCg_to_RGB(self) -> None:

120 """

121 Test :func:`colour.models.rgb.ycocg.YCoCg_to_RGB` definition

122 n-dimensional arrays support.

123 """

124

125 YCoCg = np.array([0.5625, 0.375, 0.1875])

126 RGB = YCoCg_to_RGB(YCoCg)

127

128 RGB = np.tile(RGB, 4)

129 RGB = np.reshape(RGB, (4, 3))

130 YCoCg = np.tile(YCoCg, 4)

131 YCoCg = np.reshape(YCoCg, (4, 3))

132 np.testing.assert_allclose(

133 YCoCg_to_RGB(YCoCg), RGB, atol=TOLERANCE_ABSOLUTE_TESTS

134 )

135

136 RGB = np.tile(RGB, 4)

137 RGB = np.reshape(RGB, (4, 4, 3))

138 YCoCg = np.tile(YCoCg, 4)

139 YCoCg = np.reshape(YCoCg, (4, 4, 3))

140 np.testing.assert_allclose(

141 YCoCg_to_RGB(YCoCg), RGB, atol=TOLERANCE_ABSOLUTE_TESTS

142 )

143

144 RGB = np.tile(RGB, 4)

145 RGB = np.reshape(RGB, (4, 4, 4, 3))

146 YCoCg = np.tile(YCoCg, 4)

147 YCoCg = np.reshape(YCoCg, (4, 4, 4, 3))

148 np.testing.assert_allclose(

149 YCoCg_to_RGB(YCoCg), RGB, atol=TOLERANCE_ABSOLUTE_TESTS

150 )

151

152 @ignore_numpy_errors

153 def test_nan_YCoCg_to_RGB(self) -> None:

154 """

155 Test :func:`colour.models.rgb.ycocg.YCoCg_to_RGB` definition nan

156 support.

157 """

158

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

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

161 YCoCg_to_RGB(cases)