Debian Codegraph @

tests/test_colorspace_to_vrml.py

raw
1 import sys
2
3 import pytest
4
5 from DisplayCAL import colormath
6 from DisplayCAL.cgats import CGATS
7 from DisplayCAL.colorspace_to_vrml import (
8 CAT,
9 ColorSpaceToVRML,
10 DIN99bToVRML,
11 DIN99cToVRML,
12 DIN99dToVRML,
13 DIN99ToVRML,
14 HSIToVRML,
15 HSLToVRML,
16 HSVToVRML,
17 ICtCpToVRML,
18 IPTToVRML,
19 LabToVRML,
20 LCHabToVRML,
21 LCHuvToVRML,
22 LptToVRML,
23 LuvPrimeToVRML,
24 LuvToVRML,
25 RGBToVRML,
26 xyYToVRML,
27 )
28
29
30 @pytest.fixture(scope="module")
31 def get_cgats_data(data_files):
32 """Return the CGATS data from the test data files."""
33 cgats_path = data_files["profile_tags_targ.ti3"] # cgats0_simple.txt"]
34 cgats = CGATS(cgats=cgats_path)
35 return cgats.queryv1("DATA")
36
37
38 def test_color_space_to_vrml_init_data_arg_is_skipped():
39 """ColorSpaceToVRML.__init__() data is skipped raises TypeError."""
40 with pytest.raises(TypeError) as cm:
41 _ = ColorSpaceToVRML(
42 white_point=(0.9642, 1.0, 0.8249),
43 cat=CAT.Bradford,
44 rgb_black_offset=40,
45 normalize_rgb_white=False,
46 )
47
48 py_error_message = {
49 9: "__init__() missing 1 required positional argument: 'data'",
50 }.get(sys.version_info.minor,
51 "ColorSpaceToVRML.__init__() missing 1 required positional argument: 'data'"
52 )
53 assert str(cm.value) == py_error_message
54
55
56 def test_color_space_to_vrml_init_data_arg_is_none():
57 """ColorSpaceToVRML.__init__() data is None raises TypeError."""
58 with pytest.raises(TypeError) as cm:
59 _ = ColorSpaceToVRML(
60 data=None,
61 white_point=(0.9642, 1.0, 0.8249),
62 cat=CAT.Bradford,
63 rgb_black_offset=40,
64 normalize_rgb_white=False,
65 )
66
67 assert str(cm.value) == (
68 "ColorSpaceToVRML.data must be a dictionary or a CGATS instance, "
69 "not NoneType: None"
70 )
71
72
73 def test_color_space_to_vrml_data_attr_is_none(get_cgats_data):
74 """ColorSpaceToVRML.data is set to None raises TypeError."""
75 colorspace_to_vrml = ColorSpaceToVRML(
76 data=get_cgats_data,
77 white_point=(0.9642, 1.0, 0.8249),
78 cat=CAT.Bradford,
79 rgb_black_offset=40,
80 normalize_rgb_white=False,
81 )
82 with pytest.raises(TypeError) as cm:
83 colorspace_to_vrml.data = None
84
85 assert str(cm.value) == (
86 "ColorSpaceToVRML.data must be a dictionary or a CGATS instance, "
87 "not NoneType: None"
88 )
89
90
91 def test_color_space_to_vrml_init_data_arg_is_a_dictionary(get_cgats_data):
92 """ColorSpaceToVRML.__init__() data arg can be a dictionary."""
93 cgats_data_as_dict = dict(get_cgats_data)
94 assert isinstance(cgats_data_as_dict, dict)
95 # this should not raise an error
96 _ = ColorSpaceToVRML(
97 data=cgats_data_as_dict,
98 white_point=(0.9642, 1.0, 0.8249),
99 cat=CAT.Bradford,
100 rgb_black_offset=40,
101 normalize_rgb_white=False,
102 )
103
104
105 def test_color_space_to_vrml_data_attr_is_a_dictionary(get_cgats_data):
106 """ColorSpaceToVRML.data is set to a dictionary."""
107 cgats_data_as_dict = dict(get_cgats_data)
108 assert isinstance(cgats_data_as_dict, dict)
109 # this should not raise an error
110 colorspace_to_vrml = ColorSpaceToVRML(
111 data={"a": 1},
112 white_point=(0.9642, 1.0, 0.8249),
113 cat=CAT.Bradford,
114 rgb_black_offset=40,
115 normalize_rgb_white=False,
116 )
117 colorspace_to_vrml.data = cgats_data_as_dict
118 assert colorspace_to_vrml.data == cgats_data_as_dict
119
120
121 def test_color_space_to_vrml_init_data_arg_is_a_cgats_instance(get_cgats_data):
122 """ColorSpaceToVRML.__init__() data arg can be a CGATS instance."""
123 cgats_data_as_cgats = get_cgats_data
124 assert isinstance(cgats_data_as_cgats, CGATS)
125 # this should not raise an error
126 _ = ColorSpaceToVRML(
127 data=cgats_data_as_cgats,
128 white_point=(0.9642, 1.0, 0.8249),
129 cat=CAT.Bradford,
130 rgb_black_offset=40,
131 normalize_rgb_white=False,
132 )
133
134
135 def test_color_space_to_vrml_data_attr_is_a_cgats_instance(get_cgats_data):
136 """ColorSpaceToVRML.data is set to a CGATS instance."""
137 cgats_data_as_cgats = get_cgats_data
138 assert isinstance(cgats_data_as_cgats, CGATS)
139 colorspace_to_vrml = ColorSpaceToVRML(
140 data={"a": 1},
141 white_point=(0.9642, 1.0, 0.8249),
142 cat=CAT.Bradford,
143 rgb_black_offset=40,
144 normalize_rgb_white=False,
145 )
146 colorspace_to_vrml.data = cgats_data_as_cgats
147 assert colorspace_to_vrml.data == cgats_data_as_cgats
148
149
150 def test_color_space_to_vrml_init_data_arg_value_is_passed_to_the_data_attr(
151 get_cgats_data,
152 ):
153 """ColorSpaceToVRML.__init__() data is passed to the data attribute."""
154 test_data = get_cgats_data
155 colorspace_to_vrml = ColorSpaceToVRML(
156 data=test_data,
157 white_point=(0.9642, 1.0, 0.8249),
158 cat=CAT.Bradford,
159 rgb_black_offset=40,
160 normalize_rgb_white=False,
161 )
162 assert colorspace_to_vrml.data == test_data
163
164
165 def test_color_space_to_vrml_colorspace_is_read_only(get_cgats_data):
166 """ColorSpaceToVRML.colorspace is read-only."""
167 colorspace_to_vrml = ColorSpaceToVRML(
168 data=get_cgats_data,
169 white_point=(0.9642, 1.0, 0.8249),
170 cat=CAT.Bradford,
171 rgb_black_offset=40,
172 normalize_rgb_white=False,
173 )
174 with pytest.raises(AttributeError) as cm:
175 colorspace_to_vrml.colorspace = "New Colorspace"
176
177 py_error_message = {
178 9: "can't set attribute",
179 10: "can't set attribute 'colorspace'",
180 }.get(sys.version_info.minor,
181 "property 'colorspace' of 'ColorSpaceToVRML' object has no setter"
182 )
183 assert str(cm.value) == py_error_message
184
185
186 # ColorSpaceToVRML.cat
187
188
189 def test_color_space_to_vrml_cat_is_skipped(get_cgats_data):
190 """ColorSpaceToVRML.cat is skipped uses Bradford as default."""
191 colorspace_to_vrml = ColorSpaceToVRML(
192 data=get_cgats_data,
193 white_point=(0.9642, 1.0, 0.8249),
194 rgb_black_offset=40,
195 normalize_rgb_white=False,
196 )
197 assert colorspace_to_vrml.cat == CAT.Bradford
198
199
200 def test_color_space_to_vrml_cat_is_str(get_cgats_data):
201 """ColorSpaceToVRML.cat is a string."""
202 colorspace_to_vrml = ColorSpaceToVRML(
203 data=get_cgats_data,
204 white_point=(0.9642, 1.0, 0.8249),
205 cat="XYZ Scaling",
206 rgb_black_offset=40,
207 normalize_rgb_white=False,
208 )
209 assert colorspace_to_vrml.cat == CAT.XYZ_Scaling
210
211
212 # ColorSpaceToVRML.colorspace
213
214
215 @pytest.mark.parametrize(
216 "colorspace_class, expected_colorspace",
217 [
218 (DIN99bToVRML, "DIN99b"),
219 (DIN99cToVRML, "DIN99c"),
220 (DIN99dToVRML, "DIN99d"),
221 (DIN99ToVRML, "DIN99"),
222 (HSIToVRML, "HSI"),
223 (HSLToVRML, "HSL"),
224 (HSVToVRML, "HSV"),
225 (ICtCpToVRML, "ICtCp"),
226 (IPTToVRML, "IPT"),
227 (LabToVRML, "Lab"),
228 (LCHabToVRML, "LCH(ab)"),
229 (LCHuvToVRML, "LCH(uv)"),
230 (LptToVRML, "Lpt"),
231 (LuvPrimeToVRML, "Lu'v'"),
232 (LuvToVRML, "Luv"),
233 (RGBToVRML, "RGB"),
234 (xyYToVRML, "xyY"),
235 ],
236 )
237 def test_color_space_to_vrml_colorspace_is_correct(
238 colorspace_class, expected_colorspace, get_cgats_data
239 ):
240 """ColorSpaceToVRML.colorspace is correct."""
241 obj = colorspace_class(
242 data=get_cgats_data,
243 white_point=(0.9642, 1.0, 0.8249),
244 cat=CAT.Bradford,
245 rgb_black_offset=40,
246 normalize_rgb_white=False,
247 )
248 assert obj.colorspace == expected_colorspace
249
250
251 @pytest.mark.parametrize(
252 "colorspace_class, vrml_filename",
253 (
254 (DIN99bToVRML, "DIN99b.vrml"),
255 (DIN99cToVRML, "DIN99c.vrml"),
256 (DIN99dToVRML, "DIN99d.vrml"),
257 (DIN99ToVRML, "DIN99.vrml"),
258 (HSIToVRML, "HSI.vrml"),
259 (HSLToVRML, "HSL.vrml"),
260 (HSVToVRML, "HSV.vrml"),
261 (ICtCpToVRML, "ICtCp.vrml"),
262 (IPTToVRML, "IPT.vrml"),
263 (LabToVRML, "Lab.vrml"),
264 (LCHabToVRML, "LCHab.vrml"),
265 (LCHuvToVRML, "LCHuv.vrml"),
266 (LptToVRML, "Lpt.vrml"),
267 (LuvPrimeToVRML, "LuvPrime.vrml"),
268 (LuvToVRML, "Luv.vrml"),
269 (RGBToVRML, "RGB.vrml"),
270 (xyYToVRML, "xyY.vrml"),
271 ),
272 )
273 def test_color_space_to_vrml_generate_vrml(
274 colorspace_class,
275 vrml_filename,
276 get_cgats_data,
277 data_files,
278 setup_argyll,
279 ):
280 """ColorSpaceToVRML.generate_vrml() returns a VRML string."""
281 vrml_file_path = data_files[vrml_filename]
282 with open(vrml_file_path, "r", encoding="utf-8") as f:
283 expected_vrml = f.read()
284 obj = colorspace_class(
285 data=get_cgats_data,
286 white_point=colormath.get_whitepoint("D50"),
287 cat=CAT.Bradford,
288 rgb_black_offset=40,
289 normalize_rgb_white=False,
290 )
291 assert obj.vrml == ""
292 obj.generate_vrml()
293 assert obj.vrml == expected_vrml
294
295
296 def test_color_space_to_vrml_valid_color_space_names_is_correct():
297 """ColorSpaceToVRML.VALID_COLOR_SPACE_NAMES contains correct names."""
298 expected_names = (
299 "DIN99",
300 "DIN99b",
301 "DIN99c",
302 "DIN99d",
303 "HSI",
304 "HSL",
305 "HSV",
306 "ICtCp",
307 "IPT",
308 "Lab",
309 "LCH(ab)",
310 "LCH(uv)",
311 "Lpt",
312 "Lu'v'",
313 "Luv",
314 "RGB",
315 "xyY",
316 )
317 assert ColorSpaceToVRML.VALID_COLOR_SPACE_NAMES == expected_names