From 8d9b79d00f3e4bd49bb0c37999f5d5f518a26df9 Mon Sep 17 00:00:00 2001 From: TJDCS Date: Fri, 3 Jan 2025 14:15:19 -0800 Subject: [PATCH 1/4] add default arguments to `sd_reference_illuminant` `shape` --- colour/quality/cfi2017.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/colour/quality/cfi2017.py b/colour/quality/cfi2017.py index f9d271cea8..8fcb84e1cc 100644 --- a/colour/quality/cfi2017.py +++ b/colour/quality/cfi2017.py @@ -42,7 +42,8 @@ if typing.TYPE_CHECKING: from colour.hints import ArrayLike, List, Literal, Tuple -from colour.hints import NDArrayFloat, cast +from colour.colorimetry.spectrum import SPECTRAL_SHAPE_DEFAULT +from colour.hints import ArrayLike, List, NDArrayFloat, Tuple, cast from colour.models import JMh_CIECAM02_to_CAM02UCS, UCS_to_uv, XYZ_to_UCS from colour.temperature import CCT_to_xy_CIE_D, uv_to_CCT_Ohno2013 from colour.utilities import ( @@ -66,16 +67,16 @@ __status__ = "Production" __all__ = [ - "SPECTRAL_SHAPE_CIE2017", "ROOT_RESOURCES_CIE2017", - "DataColorimetry_TCS_CIE2017", + "SPECTRAL_SHAPE_CIE2017", + "CCT_reference_illuminant", "ColourRendering_Specification_CIE2017", + "DataColorimetry_TCS_CIE2017", "colour_fidelity_index_CIE2017", + "delta_E_to_R_f", "load_TCS_CIE2017", - "CCT_reference_illuminant", "sd_reference_illuminant", "tcs_colorimetry_data", - "delta_E_to_R_f", ] SPECTRAL_SHAPE_CIE2017: SpectralShape = SpectralShape(380, 780, 1) @@ -346,7 +347,9 @@ def CCT_reference_illuminant(sd: SpectralDistribution) -> NDArrayFloat: return uv_to_CCT_Ohno2013(UCS_to_uv(XYZ_to_UCS(XYZ)), start=1000, end=25000) -def sd_reference_illuminant(CCT: float, shape: SpectralShape) -> SpectralDistribution: +def sd_reference_illuminant( + CCT: float, shape: SpectralShape = SPECTRAL_SHAPE_DEFAULT +) -> SpectralDistribution: """ Compute the reference illuminant for a given correlated colour temperature :math:`T_{cp}` for use in *CIE 2017 Colour Fidelity Index* (CFI) @@ -357,7 +360,8 @@ def sd_reference_illuminant(CCT: float, shape: SpectralShape) -> SpectralDistrib CCT Correlated colour temperature :math:`T_{cp}`. shape - Desired shape of the returned spectral distribution. + Desired shape of the returned spectral distribution. Defaults to + SPECTRAL_SHAPE_DEFAULT Returns ------- From 1a49fff988ade674ef4f70ecd119dc7561aa0df6 Mon Sep 17 00:00:00 2001 From: TJDCS Date: Fri, 3 Jan 2025 14:38:10 -0800 Subject: [PATCH 2/4] test default sd_reference_illum shape --- colour/quality/tests/test_cfi2017.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/colour/quality/tests/test_cfi2017.py b/colour/quality/tests/test_cfi2017.py index 347b458ae0..b62bd5b35d 100644 --- a/colour/quality/tests/test_cfi2017.py +++ b/colour/quality/tests/test_cfi2017.py @@ -19,6 +19,7 @@ reshape_sd, sd_blackbody, ) +from colour.colorimetry.spectrum import SPECTRAL_SHAPE_DEFAULT from colour.quality.cfi2017 import ( CCT_reference_illuminant, colour_fidelity_index_CIE2017, @@ -924,6 +925,14 @@ class TestSdReferenceIlluminant: definition unit tests methods. """ + def test_default_args(self) -> None: + """Test :func:`color.quality.CIE2017.sd_reference_illuminant` for + default shape argument. + """ + + sd = sd_reference_illuminant(5421) + assert sd.shape == SPECTRAL_SHAPE_DEFAULT + def test_sd_reference_illuminant(self) -> None: """ Test :func:`colour.quality.CIE2017.sd_reference_illuminant` From affff943753b1d6de0158bd8ae90e7a7df876ff7 Mon Sep 17 00:00:00 2001 From: TJDCS Date: Fri, 3 Jan 2025 14:45:44 -0800 Subject: [PATCH 3/4] add a small comment to summarize the pre-commit config --- .pre-commit-config.yaml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9e240b2953..3011f1cae4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,3 +1,10 @@ +# The pre-commit steps used by colour are: +# * codespell +# * isort +# * ruff-format +# * ruff --fix +# * blacken-docs +# * prettier (with specific fixes) repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: "v5.0.0" From 715d256f0c61149da36e8e98179386b626798c47 Mon Sep 17 00:00:00 2001 From: TJDCS Date: Mon, 6 Jan 2025 08:29:29 +0800 Subject: [PATCH 4/4] add alternate `SpectralShape` constructor --- colour/colorimetry/spectrum.py | 23 +++++++++++++++++++++++ colour/colorimetry/tests/test_spectrum.py | 15 +++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/colour/colorimetry/spectrum.py b/colour/colorimetry/spectrum.py index 04e4b7bcc7..10968f8c16 100644 --- a/colour/colorimetry/spectrum.py +++ b/colour/colorimetry/spectrum.py @@ -158,6 +158,29 @@ def __init__(self, start: Real, end: Real, interval: Real) -> None: self.end = end self.interval = interval + @classmethod + def from_array(cls, data: ArrayLike) -> SpectralShape: + """Alternate constructor, create a SpectralShape from an ArrayLike list + of values. Values must be evenly spaced. + + Parameters + ---------- + data : ArrayLike + The wavelength list + + Returns + ------- + SpectralShape + """ + data = np.asarray(data) + + spacing = (diff_intermediate := np.diff(data))[0] + if ~np.all(diff_intermediate == spacing): + error = "data values must have equal spacing" + raise RuntimeError(error) + + return SpectralShape(data[0], data[-1], spacing) + @property def start(self) -> Real: """ diff --git a/colour/colorimetry/tests/test_spectrum.py b/colour/colorimetry/tests/test_spectrum.py index c72a1ad1f1..d1566d4590 100644 --- a/colour/colorimetry/tests/test_spectrum.py +++ b/colour/colorimetry/tests/test_spectrum.py @@ -1417,6 +1417,21 @@ def test_range(self) -> None: np.arange(0, 10 + 0.1, 0.1), ) + def test_from_data(self) -> None: + """Test :func:`colour.colorimetry.spectrum.SpectralShape.from_array`""" + data = np.arange(400, 700 + 1, 10) # arange generates [start, stop) + shape = SpectralShape.from_array(data) + + assert shape.start == 400 + assert shape.end == 700 + assert shape.interval == 10 + + assert shape == SpectralShape(400, 700, 10) + + with pytest.raises(RuntimeError): + data = [400, 450, 500, 555, 600, 650, 700] + SpectralShape.from_array(data) + class TestSpectralDistribution: """