"""
Demonstrate *Academy Color Encoding System* *Input Transform* computations.

This module demonstrates the computation of ACES relative exposure values
from spectral distributions and the creation of input device transforms
for cameras using various optimisation methods.
"""

import os

import colour
from colour.hints import cast
from colour.utilities import message_box

message_box('"ACES" "Input Transform" Computations')

message_box(
    'Compute "ACES" relative exposure values for some colour rendition '
    "chart spectral distributions:\n\n"
    '\t("dark skin", "blue sky")'
)
print(
    colour.sd_to_aces_relative_exposure_values(
        colour.SDS_COLOURCHECKERS["ColorChecker N Ohta"]["dark skin"]
    )
)
print(
    colour.sd_to_aces_relative_exposure_values(
        colour.SDS_COLOURCHECKERS["ColorChecker N Ohta"]["blue sky"]
    )
)

print("\n")

message_box(
    'Compute "ACES" relative exposure values for various ideal '
    "reflectors:\n\n"
    '\t("18%", "100%")'
)
wavelengths = colour.characterisation.MSDS_ACES_RICD.wavelengths
gray_reflector = colour.SpectralDistribution(
    dict(zip(wavelengths, [0.18] * len(wavelengths), strict=True)), name="18%"
)
print(repr(colour.sd_to_aces_relative_exposure_values(gray_reflector)))

perfect_reflector = colour.SpectralDistribution(
    dict(zip(wavelengths, [1.0] * len(wavelengths), strict=True)), name="100%"
)
print(colour.sd_to_aces_relative_exposure_values(perfect_reflector))

print("\n")

message_box(
    'Compute an "ACES" input device transform for a "CANON EOS 5DMark II" '
    "and and *CIE Illuminant D Series* *D55*:"
)

path = os.path.join(
    colour.characterisation.aces_it.ROOT_RESOURCES_RAWTOACES,
    "CANON_EOS_5DMark_II_RGB_Sensitivities.csv",
)
sensitivities = colour.colorimetry.sds_and_msds_to_msds(
    list(colour.io.read_sds_from_csv_file(path).values())
)
illuminant = colour.SDS_ILLUMINANTS["D55"]

print(
    colour.matrix_idt(
        cast("colour.characterisation.RGB_CameraSensitivities", sensitivities),
        illuminant,
    )
)

message_box(
    'Optimise in "Oklab" colourspace using "Finlayson et al. (2015)" '
    "root-polynomials colour correction:"
)

M, RGB_w = colour.matrix_idt(  # pyright: ignore
    cast("colour.characterisation.RGB_CameraSensitivities", sensitivities),
    illuminant,
    optimisation_factory=colour.characterisation.optimisation_factory_Oklab_15,
)

print((M, RGB_w))
