Coverage for colour/notation/tests/test_hexadecimal.py: 100%
61 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"""Define the unit tests for the :mod:`colour.notation.hexadecimal` module."""
3from __future__ import annotations
5from itertools import product
7import numpy as np
9from colour.constants import TOLERANCE_ABSOLUTE_TESTS
10from colour.notation.hexadecimal import HEX_to_RGB, RGB_to_HEX
11from colour.utilities import domain_range_scale, 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__ = "Production"
20__all__ = [
21 "TestRGB_to_HEX",
22 "TestHEX_to_RGB",
23]
26class TestRGB_to_HEX:
27 """
28 Define :func:`colour.notation.hexadecimal.RGB_to_HEX` definition unit
29 tests methods.
30 """
32 def test_RGB_to_HEX(self) -> None:
33 """Test :func:`colour.notation.hexadecimal.RGB_to_HEX` definition."""
35 assert RGB_to_HEX(np.array([0.45620519, 0.03081071, 0.04091952])) == "#74070a"
37 assert RGB_to_HEX(np.array([0.00000000, 0.00000000, 0.00000000])) == "#000000"
39 assert RGB_to_HEX(np.array([1.00000000, 1.00000000, 1.00000000])) == "#ffffff"
41 np.testing.assert_equal(
42 RGB_to_HEX(
43 np.array(
44 [
45 [10.00000000, 1.00000000, 1.00000000],
46 [1.00000000, 1.00000000, 1.00000000],
47 [0.00000000, 1.00000000, 0.00000000],
48 ]
49 )
50 ),
51 ["#fe0e0e", "#0e0e0e", "#000e00"],
52 )
54 def test_n_dimensional_RGB_to_HEX(self) -> None:
55 """
56 Test :func:`colour.notation.hexadecimal.RGB_to_HEX` definition
57 n-dimensional arrays support.
58 """
60 RGB = np.array([0.45620519, 0.03081071, 0.04091952])
61 HEX = RGB_to_HEX(RGB)
63 RGB = np.tile(RGB, (6, 1))
64 HEX = np.tile(HEX, 6)
65 assert RGB_to_HEX(RGB).tolist() == HEX.tolist()
67 RGB = np.reshape(RGB, (2, 3, 3))
68 HEX = np.reshape(HEX, (2, 3))
69 assert RGB_to_HEX(RGB).tolist() == HEX.tolist()
71 def test_domain_range_scale_RGB_to_HEX(self) -> None:
72 """
73 Test :func:`colour.notation.hexadecimal.RGB_to_HEX` definition domain
74 and range scale support.
75 """
77 RGB = np.array([0.45620519, 0.03081071, 0.04091952])
78 HEX = RGB_to_HEX(RGB)
80 d_r = (("reference", 1), ("1", 1), ("100", 100))
81 for scale, factor in d_r:
82 with domain_range_scale(scale):
83 assert RGB_to_HEX(RGB * factor) == HEX
85 @ignore_numpy_errors
86 def test_nan_RGB_to_HEX(self) -> None:
87 """
88 Test :func:`colour.notation.hexadecimal.RGB_to_HEX` definition
89 nan support.
90 """
92 cases = [-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]
93 cases = np.array(list(set(product(cases, repeat=3))))
94 RGB_to_HEX(cases)
97class TestHEX_to_RGB:
98 """
99 Define :func:`colour.notation.hexadecimal.HEX_to_RGB` definition unit
100 tests methods.
101 """
103 def test_HEX_to_RGB(self) -> None:
104 """Test :func:`colour.notation.hexadecimal.HEX_to_RGB` definition."""
106 np.testing.assert_allclose(
107 HEX_to_RGB("#74070a"),
108 np.array([0.45620519, 0.03081071, 0.04091952]),
109 atol=1e-1,
110 )
112 np.testing.assert_allclose(
113 HEX_to_RGB("#000000"),
114 np.array([0.00000000, 0.00000000, 0.00000000]),
115 atol=TOLERANCE_ABSOLUTE_TESTS,
116 )
118 np.testing.assert_allclose(
119 HEX_to_RGB("#ffffff"),
120 np.array([1.00000000, 1.00000000, 1.00000000]),
121 atol=TOLERANCE_ABSOLUTE_TESTS,
122 )
124 def test_n_dimensional_HEX_to_RGB(self) -> None:
125 """
126 Test :func:`colour.notation.hexadecimal.HEX_to_RGB` definition
127 n-dimensional arrays support.
128 """
130 HEX = "#74070a"
131 RGB = HEX_to_RGB(HEX)
133 HEX = np.tile(HEX, 6)
134 RGB = np.tile(RGB, (6, 1))
135 np.testing.assert_allclose(HEX_to_RGB(HEX), RGB, atol=TOLERANCE_ABSOLUTE_TESTS)
137 HEX = np.reshape(HEX, (2, 3))
138 RGB = np.reshape(RGB, (2, 3, 3))
139 np.testing.assert_allclose(HEX_to_RGB(HEX), RGB, atol=TOLERANCE_ABSOLUTE_TESTS)
141 def test_domain_range_scale_HEX_to_RGB(self) -> None:
142 """
143 Test :func:`colour.notation.hexadecimal.HEX_to_RGB` definition domain
144 and range scale support.
145 """
147 HEX = "#74070a"
148 RGB = HEX_to_RGB(HEX)
150 d_r = (("reference", 1), ("1", 1), ("100", 100))
151 for scale, factor in d_r:
152 with domain_range_scale(scale):
153 np.testing.assert_array_equal(HEX_to_RGB(HEX), RGB * factor)