Coverage for models/rgb/tests/test_ycocg.py: 100%
69 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-16 22:49 +1300
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-16 22:49 +1300
1"""Define the unit tests for the :mod:`colour.models.rgb.ycocg` module."""
3from __future__ import annotations
5from itertools import product
7import numpy as np
9from colour.constants import TOLERANCE_ABSOLUTE_TESTS
10from colour.models.rgb import RGB_to_YCoCg, YCoCg_to_RGB
11from colour.utilities import ignore_numpy_errors
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"
20__all__ = [
21 "TestRGB_to_YCoCg",
22 "TestYCoCg_to_RGB",
23]
26class TestRGB_to_YCoCg:
27 """
28 Define :func:`colour.models.rgb.ycocg.RGB_to_YCoCg` definition unit tests
29 methods.
30 """
32 def test_RGB_to_YCoCg(self) -> None:
33 """Test :func:`colour.models.rgb.ycocg.RGB_to_YCoCg` definition."""
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 )
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 )
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 )
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 """
56 RGB = np.array([0.75, 0.75, 0.0])
57 YCoCg = RGB_to_YCoCg(RGB)
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 )
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 )
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 )
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 """
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)
95class TestYCoCg_to_RGB:
96 """
97 Define :func:`colour.models.rgb.ycocg.YCoCg_to_RGB` definition unit tests
98 methods.
99 """
101 def test_YCoCg_to_RGB(self) -> None:
102 """Test :func:`colour.models.rgb.ycocg.YCoCg_to_RGB` definition."""
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 )
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 )
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 )
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 """
125 YCoCg = np.array([0.5625, 0.375, 0.1875])
126 RGB = YCoCg_to_RGB(YCoCg)
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 )
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 )
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 )
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 """
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)