extern crate alloc;
use moxcms::TransformOptions;
use crate::error::{ColorError, ColorResult};
use crate::profile::IccProfile;
use crate::proofing::{GamutWarning, ProofingConfig};
use crate::rendering_intent::RenderingIntent;
pub struct ProofingConfigBuilder {
pub(crate) source: Option<IccProfile>,
pub(crate) simulation: Option<IccProfile>,
pub(crate) display: Option<IccProfile>,
pub(crate) simulation_intent: RenderingIntent,
pub(crate) display_intent: RenderingIntent,
pub(crate) gamut_warning: GamutWarning,
}
impl ProofingConfigBuilder {
pub fn source(mut self, profile: &IccProfile) -> Self {
self.source = Some(profile.clone());
self
}
pub fn simulation(mut self, profile: &IccProfile) -> Self {
self.simulation = Some(profile.clone());
self
}
pub fn display(mut self, profile: &IccProfile) -> Self {
self.display = Some(profile.clone());
self
}
pub fn simulation_intent(mut self, intent: RenderingIntent) -> Self {
self.simulation_intent = intent;
self
}
pub fn display_intent(mut self, intent: RenderingIntent) -> Self {
self.display_intent = intent;
self
}
pub fn gamut_warning(mut self, warning: GamutWarning) -> Self {
self.gamut_warning = warning;
self
}
pub fn build(self) -> ColorResult<ProofingConfig> {
let src = self.source.ok_or_else(|| {
ColorError::IncompleteProofingConfig("source profile".into())
})?;
let sim = self.simulation.ok_or_else(|| {
ColorError::IncompleteProofingConfig("simulation profile".into())
})?;
let disp = self.display.ok_or_else(|| {
ColorError::IncompleteProofingConfig("display profile".into())
})?;
let src_space = src.color_space()?;
let sim_space = sim.color_space()?;
let disp_space = disp.color_space()?;
let sim_opts = TransformOptions {
rendering_intent: self.simulation_intent.to_moxcms(),
..TransformOptions::default()
};
let disp_opts = TransformOptions {
rendering_intent: self.display_intent.to_moxcms(),
..TransformOptions::default()
};
let sim_exec = src
.inner
.create_transform_f32(
src_space.to_moxcms_layout(),
&sim.inner,
sim_space.to_moxcms_layout(),
sim_opts,
)
.map_err(|e| {
ColorError::TransformExecution(alloc::format!("{:?}", e))
})?;
let disp_exec = sim
.inner
.create_transform_f32(
sim_space.to_moxcms_layout(),
&disp.inner,
disp_space.to_moxcms_layout(),
disp_opts,
)
.map_err(|e| {
ColorError::TransformExecution(alloc::format!("{:?}", e))
})?;
ProofingConfig::new(
sim_exec,
disp_exec,
self.gamut_warning,
sim_space.channel_count(),
src_space.channel_count(),
disp_space.channel_count(),
)
}
}