Coverage for notation/tests/test_hexadecimal.py: 100%

1"""Define the unit tests for the :mod:`colour.notation.hexadecimal` 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.notation.hexadecimal import HEX_to_RGB, RGB_to_HEX

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

22 "TestHEX_to_RGB",

23]

24

25

26 class TestRGB_to_HEX:

27 """

28 Define :func:`colour.notation.hexadecimal.RGB_to_HEX` definition unit

29 tests methods.

30 """

31

32 def test_RGB_to_HEX(self) -> None:

33 """Test :func:`colour.notation.hexadecimal.RGB_to_HEX` definition."""

34

35 assert RGB_to_HEX(np.array([0.45620519, 0.03081071, 0.04091952])) == "#74070a"

36

37 assert RGB_to_HEX(np.array([0.00000000, 0.00000000, 0.00000000])) == "#000000"

38

39 assert RGB_to_HEX(np.array([1.00000000, 1.00000000, 1.00000000])) == "#ffffff"

40

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 )

53

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

59

60 RGB = np.array([0.45620519, 0.03081071, 0.04091952])

61 HEX = RGB_to_HEX(RGB)

62

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

64 HEX = np.tile(HEX, 6)

65 assert RGB_to_HEX(RGB).tolist() == HEX.tolist()

66

67 RGB = np.reshape(RGB, (2, 3, 3))

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

69 assert RGB_to_HEX(RGB).tolist() == HEX.tolist()

70

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

76

77 RGB = np.array([0.45620519, 0.03081071, 0.04091952])

78 HEX = RGB_to_HEX(RGB)

79

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

84

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

91

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)

95

96

97 class TestHEX_to_RGB:

98 """

99 Define :func:`colour.notation.hexadecimal.HEX_to_RGB` definition unit

100 tests methods.

101 """

102

103 def test_HEX_to_RGB(self) -> None:

104 """Test :func:`colour.notation.hexadecimal.HEX_to_RGB` definition."""

105

106 np.testing.assert_allclose(

107 HEX_to_RGB("#74070a"),

108 np.array([0.45620519, 0.03081071, 0.04091952]),

109 atol=1e-1,

110 )

111

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 )

117

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 )

123

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

129

130 HEX = "#74070a"

131 RGB = HEX_to_RGB(HEX)

132

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)

136

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)

140

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

146

147 HEX = "#74070a"

148 RGB = HEX_to_RGB(HEX)

149

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)