Coverage for colour/models/rgb/transfer_functions/tests/test_common.py: 100%
71 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-15 19:01 +1300
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-15 19:01 +1300
1"""
2Define the unit tests for the
3:mod:`colour.models.rgb.transfer_functions.common` module.
4"""
6import numpy as np
8from colour.constants import TOLERANCE_ABSOLUTE_TESTS
9from colour.models.rgb.transfer_functions import CV_range, full_to_legal, legal_to_full
10from colour.utilities import ignore_numpy_errors
12__author__ = "Colour Developers"
13__copyright__ = "Copyright 2013 Colour Developers"
14__license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause"
15__maintainer__ = "Colour Developers"
16__email__ = "colour-developers@colour-science.org"
17__status__ = "Development"
19__all__ = [
20 "TestCV_range",
21 "TestLegalToFull",
22 "TestFullToLegal",
23]
26class TestCV_range:
27 """
28 Define :func:`colour.models.rgb.transfer_functions.common.CV_range`
29 definition unit tests methods.
30 """
32 def test_CV_range(self) -> None:
33 """
34 Test :func:`colour.models.rgb.transfer_functions.common.CV_range`
35 definition.
36 """
38 np.testing.assert_array_equal(CV_range(8, True, True), np.array([16, 235]))
40 np.testing.assert_array_equal(CV_range(8, False, True), np.array([0, 255]))
42 np.testing.assert_allclose(
43 CV_range(8, True, False),
44 np.array([0.06274510, 0.92156863]),
45 atol=TOLERANCE_ABSOLUTE_TESTS,
46 )
48 np.testing.assert_array_equal(CV_range(8, False, False), np.array([0, 1]))
50 np.testing.assert_array_equal(CV_range(10, True, True), np.array([64, 940]))
52 np.testing.assert_array_equal(CV_range(10, False, True), np.array([0, 1023]))
54 np.testing.assert_allclose(
55 CV_range(10, True, False),
56 np.array([0.06256109, 0.91886608]),
57 atol=TOLERANCE_ABSOLUTE_TESTS,
58 )
60 np.testing.assert_array_equal(CV_range(10, False, False), np.array([0, 1]))
63class TestLegalToFull:
64 """
65 Define :func:`colour.models.rgb.transfer_functions.common.legal_to_full`
66 definition unit tests methods.
67 """
69 def test_legal_to_full(self) -> None:
70 """
71 Test :func:`colour.models.rgb.transfer_functions.common.legal_to_full`
72 definition.
73 """
75 np.testing.assert_allclose(legal_to_full(64 / 1023), 0.0)
77 np.testing.assert_allclose(legal_to_full(940 / 1023), 1.0)
79 np.testing.assert_allclose(legal_to_full(64 / 1023, out_int=True), 0)
81 np.testing.assert_allclose(legal_to_full(940 / 1023, out_int=True), 1023)
83 np.testing.assert_allclose(legal_to_full(64, in_int=True), 0.0)
85 np.testing.assert_allclose(legal_to_full(940, in_int=True), 1.0)
87 np.testing.assert_allclose(legal_to_full(64, in_int=True, out_int=True), 0)
89 np.testing.assert_allclose(legal_to_full(940, in_int=True, out_int=True), 1023)
91 def test_n_dimensional_legal_to_full(self) -> None:
92 """
93 Test :func:`colour.models.rgb.transfer_functions.common.legal_to_full`
94 definition n-dimensional arrays support.
95 """
97 CV_l = 0.918866080156403
98 CV_f = legal_to_full(CV_l, 10)
100 CV_l = np.tile(CV_l, 6)
101 CV_f = np.tile(CV_f, 6)
102 np.testing.assert_allclose(
103 legal_to_full(CV_l, 10), CV_f, atol=TOLERANCE_ABSOLUTE_TESTS
104 )
106 CV_l = np.reshape(CV_l, (2, 3))
107 CV_f = np.reshape(CV_f, (2, 3))
108 np.testing.assert_allclose(
109 legal_to_full(CV_l, 10), CV_f, atol=TOLERANCE_ABSOLUTE_TESTS
110 )
112 CV_l = np.reshape(CV_l, (2, 3, 1))
113 CV_f = np.reshape(CV_f, (2, 3, 1))
114 np.testing.assert_allclose(
115 legal_to_full(CV_l, 10), CV_f, atol=TOLERANCE_ABSOLUTE_TESTS
116 )
118 @ignore_numpy_errors
119 def test_nan_legal_to_full(self) -> None:
120 """
121 Test :func:`colour.models.rgb.transfer_functions.common.legal_to_full`
122 definition nan support.
123 """
125 legal_to_full(np.array([-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]), 10)
128class TestFullToLegal:
129 """
130 Define :func:`colour.models.rgb.transfer_functions.common.full_to_legal`
131 definition unit tests methods.
132 """
134 def test_full_to_legal(self) -> None:
135 """
136 Test :func:`colour.models.rgb.transfer_functions.common.full_to_legal`
137 definition.
138 """
140 np.testing.assert_allclose(full_to_legal(0.0), 0.062561094819159)
142 np.testing.assert_allclose(full_to_legal(1.0), 0.918866080156403)
144 np.testing.assert_allclose(full_to_legal(0.0, out_int=True), 64)
146 np.testing.assert_allclose(full_to_legal(1.0, out_int=True), 940)
148 np.testing.assert_allclose(full_to_legal(0, in_int=True), 0.062561094819159)
150 np.testing.assert_allclose(full_to_legal(1023, in_int=True), 0.918866080156403)
152 np.testing.assert_allclose(full_to_legal(0, in_int=True, out_int=True), 64)
154 np.testing.assert_allclose(full_to_legal(1023, in_int=True, out_int=True), 940)
156 def test_n_dimensional_full_to_legal(self) -> None:
157 """
158 Test :func:`colour.models.rgb.transfer_functions.common.full_to_legal`
159 definition n-dimensional arrays support.
160 """
162 CF_f = 1.0
163 CV_l = full_to_legal(CF_f, 10)
165 CF_f = np.tile(CF_f, 6)
166 CV_l = np.tile(CV_l, 6)
167 np.testing.assert_allclose(
168 full_to_legal(CF_f, 10), CV_l, atol=TOLERANCE_ABSOLUTE_TESTS
169 )
171 CF_f = np.reshape(CF_f, (2, 3))
172 CV_l = np.reshape(CV_l, (2, 3))
173 np.testing.assert_allclose(
174 full_to_legal(CF_f, 10), CV_l, atol=TOLERANCE_ABSOLUTE_TESTS
175 )
177 CF_f = np.reshape(CF_f, (2, 3, 1))
178 CV_l = np.reshape(CV_l, (2, 3, 1))
179 np.testing.assert_allclose(
180 full_to_legal(CF_f, 10), CV_l, atol=TOLERANCE_ABSOLUTE_TESTS
181 )
183 @ignore_numpy_errors
184 def test_nan_full_to_legal(self) -> None:
185 """
186 Test :func:`colour.models.rgb.transfer_functions.common.full_to_legal`
187 definition nan support.
188 """
190 full_to_legal(np.array([-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]), 10)