Coverage for io/xrite.py: 62%
37 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"""
2X-Rite Data Input
3=================
5Define input functionality for X-Rite spectral data files.
7- :func:`colour.read_sds_from_xrite_file`
8"""
10from __future__ import annotations
12import codecs
13import re
14import typing
16from colour.colorimetry import SpectralDistribution
18if typing.TYPE_CHECKING:
19 from colour.hints import Dict, PathLike
21__author__ = "Colour Developers"
22__copyright__ = "Copyright 2013 Colour Developers"
23__license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause"
24__maintainer__ = "Colour Developers"
25__email__ = "colour-developers@colour-science.org"
26__status__ = "Production"
28__all__ = [
29 "XRITE_FILE_ENCODING",
30 "read_sds_from_xrite_file",
31]
33XRITE_FILE_ENCODING: str = "utf-8"
36def read_sds_from_xrite_file(
37 path: str | PathLike,
38) -> Dict[str, SpectralDistribution]:
39 """
40 Read spectral data from the specified *X-Rite* file and convert it to a
41 *dict* of :class:`colour.SpectralDistribution` class instances.
43 Parameters
44 ----------
45 path
46 Absolute *X-Rite* file path.
48 Returns
49 -------
50 :class:`dict`
51 *dict* of :class:`colour.SpectralDistribution` class instances.
53 Raises
54 ------
55 IOError
56 If the file cannot be read.
58 Notes
59 -----
60 - This parser is minimalistic and absolutely not bullet-proof.
62 Examples
63 --------
64 >>> import os
65 >>> from pprint import pprint
66 >>> xrite_file = os.path.join(
67 ... os.path.dirname(__file__),
68 ... "tests",
69 ... "resources",
70 ... "X-Rite_Digital_Colour_Checker.txt",
71 ... )
72 >>> sds_data = read_sds_from_xrite_file(xrite_file)
73 >>> pprint(list(sds_data.keys())) # doctest: +SKIP
74 ['X1', 'X2', 'X3', 'X4', 'X5', 'X6', 'X7', 'X8', 'X9', 'X10']
75 """
77 path = str(path)
79 with codecs.open(path, encoding=XRITE_FILE_ENCODING) as xrite_file:
80 lines = xrite_file.read().strip().split("\n")
82 index = 0
83 xrite_sds = {}
84 is_spectral_data_format, is_spectral_data = False, False
85 for line in lines:
86 line = line.strip() # noqa: PLW2901
88 if line == "END_DATA_FORMAT":
89 is_spectral_data_format = False
91 if line == "END_DATA":
92 is_spectral_data = False
94 if is_spectral_data_format:
95 wavelengths = list(re.findall("nm(\\d+)", line))
96 index = len(wavelengths)
98 if is_spectral_data:
99 tokens = line.split()
100 xrite_sds[tokens[1]] = SpectralDistribution(
101 tokens[-index:], wavelengths, name=tokens[1]
102 )
104 if line == "BEGIN_DATA_FORMAT":
105 is_spectral_data_format = True
107 if line == "BEGIN_DATA":
108 is_spectral_data = True
110 return xrite_sds