diff --git a/crates/imaging/src/ui/mod.rs b/crates/imaging/src/ui/mod.rs index 4c848ec6c..3e9e87b4f 100644 --- a/crates/imaging/src/ui/mod.rs +++ b/crates/imaging/src/ui/mod.rs @@ -90,7 +90,7 @@ pub use mask_view::{MaskViewControls, MaskViewControlsOutput, MaskViewKeys, Mask pub use scopes::{ COLORS_CHANNEL, ChannelOverlay, ClipWarning, ClippingDiagnostic, ClippingFrame, ClippingMode, ClippingOverlay, ClippingWarnings, HistogramSet, ScopeChannel, ScopeCommand, ScopeFrame, ScopeMenuAction, ScopePanel, ScopePanelOutput, ScopePanelState, ScopeReadout, ScopeSettings, ScopeSource, - ScopeView, ScopeWords, ScopeZoneEdit, ToneZone, ToneZones, VectorscopeFrame, VectorscopePlot, ZoneChange, scope_point, + ScopeView, ScopeWords, ToneZone, ToneZones, VectorscopeFrame, VectorscopePlot, ZoneScale, ZoneValues, scope_point, }; pub use select::{ LassoTool, MagneticLassoTool, MagneticSettings, MarqueeKind, MarqueeTool, PolygonLassoTool, SelectEvent, SelectWords, SelectionShape, diff --git a/crates/imaging/src/ui/raw_develop/controls.rs b/crates/imaging/src/ui/raw_develop/controls.rs index 233910ccf..ca8c507ea 100644 --- a/crates/imaging/src/ui/raw_develop/controls.rs +++ b/crates/imaging/src/ui/raw_develop/controls.rs @@ -1,10 +1,24 @@ //! The control column: sections of sliders over the settings. -use craft_ui::contract::{Bind, EditResponse, NoValue, Outcome, Pattern, Props}; -use egui::{Response, Ui}; +use std::ops::RangeInclusive; +use craft_behavior::EditEvents; +use craft_ui::buttons::Button; +use craft_ui::contract::{Bind, EditResponse, Entry, NoValue, Outcome, Pattern, Props, SaveKey, Show as _, ShowContainer as _, clicked}; +use craft_ui::form::SliderField; +use craft_ui::layout_components::Stack; +use craft_ui::pickers::Select; +use craft_ui::runtime::env::{Env, Label}; +use craft_ui::sections::Section; +use craft_ui::style::DataColor; +use craft_ui::value_drag::TrackFace; +use egui::{Color32, Response, Ui}; + +use super::mixer; +use super::words::RawDevelopWords; use super::Demosaic; -use crate::raw::RawDevelopSettings; +use crate::raw::{RawDevelopSettings, VignetteStyle}; +use crate::ui::curve::{Curve, CurveEditor, ToneRamp}; /// What the control column reports. #[derive(Debug)] @@ -16,6 +30,8 @@ pub struct RawDevelopPanelOutput { pub edit: EditResponse, /// The Auto button was pressed. pub auto: bool, + /// The demosaic the user picked this pass, while a raw is being opened. + pub demosaic: Option, } impl Outcome for RawDevelopPanelOutput { @@ -26,11 +42,275 @@ impl Outcome for RawDevelopPanelOutput { } } +/// One plain slider row: its label word, the settings field it edits, its range, step and optional track colours. +/// One table of these drives every plain row of the column, and the scope's tone zones find their field here by key. +#[derive(Clone, Copy)] +pub(super) struct Slider { + /// The settings field's name, the key of a tone zone. + pub(super) key: &'static str, + word: fn(&RawDevelopWords) -> &Label, + pub(super) get: fn(&RawDevelopSettings) -> f32, + pub(super) set: fn(&mut RawDevelopSettings, f32), + pub(super) min: f64, + pub(super) max: f64, + pub(super) step: f64, + stops: Option Vec>, +} + +/// A slider row over the settings field `$field`; its label is the word of the same name, or `$word` when given +/// (`field: word`). +macro_rules! slider { + ($field:ident, $min:expr, $max:expr) => { + slider!($field, $min, $max, 1.0, None) + }; + ($field:ident, $min:expr, $max:expr, $step:expr) => { + slider!($field, $min, $max, $step, None) + }; + ($field:ident, $min:expr, $max:expr, $step:expr, $stops:expr) => { + slider!($field: $field, $min, $max, $step, $stops) + }; + ($field:ident: $word:ident, $min:expr, $max:expr) => { + slider!($field: $word, $min, $max, 1.0, None) + }; + ($field:ident: $word:ident, $min:expr, $max:expr, $step:expr, $stops:expr) => { + Slider { + key: stringify!($field), + word: |w| &w.$word, + get: |s| s.$field, + set: |s, v| s.$field = v, + min: $min, + max: $max, + step: $step, + stops: $stops, + } + }; +} + +/// The Light section's rows below Auto. +pub(super) const LIGHT: &[Slider] = &[ + slider!(exposure, -5.0, 5.0, 0.05), + slider!(contrast, -100.0, 100.0), + slider!(highlights, -100.0, 100.0), + slider!(shadows, -100.0, 100.0), + slider!(whites, -100.0, 100.0), + slider!(blacks, -100.0, 100.0), +]; + +const COLOR: &[Slider] = &[ + slider!(temperature, -100.0, 100.0, 1.0, Some(temperature_stops)), + slider!(tint, -100.0, 100.0, 1.0, Some(tint_stops)), + slider!(vibrance, -100.0, 100.0), + slider!(saturation, -100.0, 100.0), +]; + +const EFFECTS: &[Slider] = &[slider!(texture, -100.0, 100.0), slider!(clarity, -100.0, 100.0), slider!(dehaze, -100.0, 100.0)]; + +const VIGNETTE: &[Slider] = &[ + slider!(vignette_amount, -100.0, 100.0), + slider!(vignette_midpoint, 0.0, 100.0), + slider!(vignette_roundness, -100.0, 100.0), + slider!(vignette_feather, 0.0, 100.0), + slider!(vignette_highlights, 0.0, 100.0), +]; + +const GRAIN: &[Slider] = &[slider!(grain_amount, 0.0, 100.0), slider!(grain_size, 0.0, 100.0), slider!(grain_roughness, 0.0, 100.0)]; + +const CURVE_REGIONS: &[Slider] = &[ + slider!(curve_highlights, -100.0, 100.0), + slider!(curve_lights, -100.0, 100.0), + slider!(curve_darks, -100.0, 100.0), + slider!(curve_shadows, -100.0, 100.0), +]; + +const DETAIL: &[Slider] = &[ + slider!(sharpen_amount, 0.0, 150.0), + slider!(sharpen_radius, 0.5, 3.0, 0.1), + slider!(sharpen_detail, 0.0, 100.0), + slider!(sharpen_masking, 0.0, 100.0), + slider!(noise_luminance, 0.0, 100.0), + slider!(noise_luminance_detail, 0.0, 100.0), + slider!(noise_color, 0.0, 100.0), + slider!(noise_color_detail, 0.0, 100.0), +]; + +/// The Color Grading section's rows below the four wheels. +pub(super) const GRADING_TAIL: &[Slider] = &[slider!(grade_blending: blending, 0.0, 100.0), slider!(grade_balance: balance, -100.0, 100.0)]; + +/// The Calibration section's first row. +const CALIBRATION: &[Slider] = &[slider!(shadow_tint, -100.0, 100.0)]; + +/// Every table of plain rows. +const TABLES: [&[Slider]; 9] = [LIGHT, COLOR, EFFECTS, VIGNETTE, GRAIN, CURVE_REGIONS, DETAIL, GRADING_TAIL, CALIBRATION]; + +/// The label word of the one row that edits the settings field `field` (its serde name, as the automation surface +/// writes it); `None` for a field no one row edits. +pub(super) fn field_word<'w>(words: &'w RawDevelopWords, field: &str) -> Option<&'w Label> { + let row = TABLES.iter().flat_map(|table| table.iter()).find(|row| row.key.replace('_', "").eq_ignore_ascii_case(field))?; + Some((row.word)(words)) +} + +/// The track of Temperature: cool to warm. +fn temperature_stops() -> Vec { + vec![ + DataColor::new(Color32::from_rgb(0x46, 0x82, 0xff)), + DataColor::new(Color32::from_rgb(0xe8, 0xe8, 0xe8)), + DataColor::new(Color32::from_rgb(0xf5, 0xc2, 0x30)), + ] +} + +/// The track of Tint: green to magenta. +fn tint_stops() -> Vec { + vec![ + DataColor::new(Color32::from_rgb(0x4c, 0xaf, 0x50)), + DataColor::new(Color32::from_rgb(0xe8, 0xe8, 0xe8)), + DataColor::new(Color32::from_rgb(0xd0, 0x40, 0xb0)), + ] +} + +/// The curve's levels: the settings hold point curves in 0..=255. +const LEVELS: f32 = 255.0; + +/// The curve editor's value for the settings' points; an empty list is the linear curve. +fn curve_of(points: &[[f32; 2]]) -> Curve { + if points.is_empty() { Curve::identity(LEVELS) } else { Curve::smooth(points.to_vec()) } +} + +/// The settings' points for the editor's curve: the unchanged curve is the empty list. A drawn curve has no points and +/// reads as linear; the column offers no pencil. +fn points_of(curve: &Curve) -> Vec<[f32; 2]> { + const IDENTITY: [[f32; 2]; 2] = [[0.0, 0.0], [LEVELS, LEVELS]]; + match curve.points() { + Some(points) if points != &IDENTITY[..] => points.to_vec(), + _ => Vec::new(), + } +} + +/// A control's edit of one field as an edit of the settings: the settings at the start of the pass with that field +/// replaced. +pub(super) fn lift(events: EditEvents, base: &RawDevelopSettings, set: &impl Fn(&mut RawDevelopSettings, T)) -> EditEvents { + events.map(|value| { + let mut settings = base.clone(); + set(&mut settings, value); + settings + }) +} + +/// What the rows of one pass add up to. +#[derive(Default)] +struct Pass { + edit: EditEvents, + auto: bool, + demosaic: Option, +} + +/// The rows of one pass: the settings they started from, those Reset restores, and what they add up to. +pub(super) struct Rows<'p> { + pub(super) base: &'p RawDevelopSettings, + pub(super) defaults: &'p RawDevelopSettings, + pass: Pass, +} + +impl<'p> Rows<'p> { + fn new(base: &'p RawDevelopSettings, defaults: &'p RawDevelopSettings) -> Self { + Rows { base, defaults, pass: Pass::default() } + } + + /// A slider with a number field over `value`; `set` writes an edit into the settings, and the edit is the pass's. + #[allow(clippy::too_many_arguments)] + pub(super) fn slider( + &mut self, + ui: &mut Ui, + label: &Label, + value: f32, + default: f32, + range: RangeInclusive, + step: f64, + stops: Option>, + set: impl Fn(&mut RawDevelopSettings, f32), + ) { + let mut field = SliderField::new(label.clone(), Bind::given(f64::from(value)).reset(f64::from(default)), range).step(step); + if let Some(stops) = &stops { + field = field.track_face(TrackFace::new(ToneRamp::new(stops))); + } + let events = field.show(ui).events; + self.pass.edit.extend(lift(events, self.base, &|settings: &mut RawDevelopSettings, v: f64| set(settings, v as f32))); + } + + /// One slider row for each entry of `table`. + pub(super) fn table(&mut self, ui: &mut Ui, words: &RawDevelopWords, table: &[Slider]) { + for row in table { + let stops = row.stops.map(|stops| stops()); + self.slider(ui, (row.word)(words), (row.get)(self.base), (row.get)(self.defaults), row.min..=row.max, row.step, stops, row.set); + } + } + + /// The Curve section's editor over the point curve. + fn curve(&mut self, ui: &mut Ui, label: &Label) { + let curve = curve_of(&self.base.point_curve); + let shown = CurveEditor::new(label.clone(), Bind::given(curve)).levels(LEVELS as u32).show(ui); + self.pass.edit.extend(lift(shown.edit.events, self.base, &|settings: &mut RawDevelopSettings, curve: Curve| { + settings.point_curve = points_of(&curve); + })); + } + + /// The three region splits under the editor: between shadows and darks, darks and lights, lights and highlights. + fn splits(&mut self, ui: &mut Ui, words: &RawDevelopWords) { + let labels = [&words.curve_shadow_split, &words.curve_midtone_split, &words.curve_highlight_split]; + for (i, label) in labels.into_iter().enumerate() { + let (value, default) = (self.base.curve_splits[i], self.defaults.curve_splits[i]); + self.slider(ui, label, value, default, 0.0..=100.0, 1.0, None, move |settings, v| settings.curve_splits[i] = v); + } + } + + /// The Calibration section's rows: Shadows Tint, then each primary's Hue and Saturation. + fn calibration(&mut self, ui: &mut Ui, words: &RawDevelopWords) { + self.table(ui, words, CALIBRATION); + let primaries = [&words.primary_red, &words.primary_green, &words.primary_blue]; + for (i, primary) in primaries.into_iter().enumerate() { + let fill = [("primary", primary.as_str())]; + let hue = Label::from(words.calibration_hue.fill(&fill)); + let (value, default) = (self.base.calibration_hue[i], self.defaults.calibration_hue[i]); + self.slider(ui, &hue, value, default, -100.0..=100.0, 1.0, None, move |settings, v| settings.calibration_hue[i] = v); + let sat = Label::from(words.calibration_sat.fill(&fill)); + let (value, default) = (self.base.calibration_sat[i], self.defaults.calibration_sat[i]); + self.slider(ui, &sat, value, default, -100.0..=100.0, 1.0, None, move |settings, v| settings.calibration_sat[i] = v); + } + } + + /// The Vignetting style select. + fn vignette_style(&mut self, ui: &mut Ui, words: &RawDevelopWords) { + let entries = [ + Entry::new(VignetteStyle::HighlightPriority, words.highlight_priority.clone()), + Entry::new(VignetteStyle::ColorPriority, words.color_priority.clone()), + Entry::new(VignetteStyle::PaintOverlay, words.paint_overlay.clone()), + ]; + let events = Select::new(words.vignette_style.clone(), Bind::given(self.base.vignette_style), &entries).show(ui).events; + self.pass.edit.extend(lift(events, self.base, &|settings: &mut RawDevelopSettings, style: VignetteStyle| settings.vignette_style = style)); + } + + /// The Demosaic select of the Detail section. + fn demosaic(&mut self, ui: &mut Ui, words: &RawDevelopWords, current: Demosaic) { + let entries = [ + Entry::new(Demosaic::Ahd, words.demosaic_ahd.clone()), + Entry::new(Demosaic::Mhc, words.demosaic_mhc.clone()), + Entry::new(Demosaic::Bilinear, words.demosaic_bilinear.clone()), + ]; + let events = Select::new(words.demosaic.clone(), Bind::given(current), &entries).show(ui).events; + if let Some(picked) = events.value().copied() { + self.pass.demosaic = Some(picked); + } + } +} + +/// A collapsible section whose open state is remembered under `craft_imaging.raw_develop.section.`. +pub(super) fn section(label: &Label, name: &str, open: bool) -> Section<'static> { + Section::new(label.clone()).remember(SaveKey::named(format!("craft_imaging.raw_develop.section.{name}"))).initially_open(open) +} + /// The control column: Light, Color, Effects, Curve, Color Mixer, Color Grading, Detail and Calibration sections. Each /// row shows `Bind::given(f64::from(value))` and writes its edit back as `f32`. #[must_use = "show the panel with `show`"] #[derive(Pattern, Props)] -#[allow(dead_code)] // P3(dialogs-w18): dl.K67 pub struct RawDevelopPanel<'a> { settings: Bind<'a, RawDevelopSettings>, /// The demosaic select shows while a raw is being opened. @@ -41,13 +321,60 @@ pub struct RawDevelopPanel<'a> { impl<'a> RawDevelopPanel<'a> { /// The panel over `settings`. pub fn new(settings: Bind<'a, RawDevelopSettings>) -> Self { - let _ = settings; - todo!("dl.K67") + RawDevelopPanel { settings, demosaic: None } } - /// Draws the sections. + /// Draws the sections. A body that runs twice in one pass (the measuring run, then the real one) builds its rows + /// afresh each run; only the real run sees input, so the edit is the real run's. pub fn show(self, ui: &mut Ui) -> RawDevelopPanelOutput { - let _ = (self, ui); - todo!("dl.K67") + let Self { mut settings, demosaic } = self; + let words = Env::of(ui).words::(); + let defaults = RawDevelopSettings::default(); + let shown = Stack::new().show(ui, |ui, root| { + let Some(base) = settings.reborrow().read(root).and_then(|held| held.same().cloned()) else { return Pass::default() }; + let mut rows = Rows::new(&base, &defaults); + section(&words.section_light, "light", true).show(ui, |ui, _| { + if clicked(&Button::new(words.auto.clone()).id_salt("auto").show(ui)) { + rows.pass.auto = true; + } + rows.table(ui, &words, LIGHT); + }); + section(&words.section_color, "color", false).show(ui, |ui, _| { + rows.table(ui, &words, COLOR); + }); + section(&words.section_effects, "effects", false).show(ui, |ui, _| { + rows.table(ui, &words, EFFECTS); + rows.vignette_style(ui, &words); + rows.table(ui, &words, VIGNETTE); + rows.table(ui, &words, GRAIN); + }); + section(&words.section_curve, "curve", false).show(ui, |ui, _| { + rows.curve(ui, &words.section_curve); + rows.table(ui, &words, CURVE_REGIONS); + rows.splits(ui, &words); + }); + mixer::color_mixer(ui, &mut rows, &words); + mixer::color_grading(ui, &mut rows, &words); + section(&words.section_detail, "detail", false).show(ui, |ui, _| { + if let Some(current) = demosaic { + rows.demosaic(ui, &words, current); + } + rows.table(ui, &words, DETAIL); + }); + section(&words.section_calibration, "calibration", false).show(ui, |ui, _| { + rows.calibration(ui, &words); + }); + for event in &rows.pass.edit { + settings.reborrow().write(root, event); + } + rows.pass + }); + let pass = shown.inner.unwrap_or_default(); + RawDevelopPanelOutput { + edit: EditResponse::lifted(shown.output.clone(), pass.edit, None), + response: shown.output, + auto: pass.auto, + demosaic: pass.demosaic, + } } } diff --git a/crates/imaging/src/ui/raw_develop/mixer.rs b/crates/imaging/src/ui/raw_develop/mixer.rs new file mode 100644 index 000000000..69e6afe4c --- /dev/null +++ b/crates/imaging/src/ui/raw_develop/mixer.rs @@ -0,0 +1,108 @@ +//! The Color Mixer and Color Grading sections of the control column. + +use craft_ui::contract::{Bind, Entry, Show as _, ShowContainer as _}; +use craft_ui::choice::Segmented; +use craft_ui::runtime::env::Label; +use craft_ui::sections::{HeaderStrength, Section}; +use craft_ui::style::DataColor; +use egui::{Color32, Ui}; + +use super::controls::{GRADING_TAIL, Rows, section}; +use super::words::RawDevelopWords; +use crate::raw::{GradeWheel, RawDevelopSettings}; + +/// The Color Mixer's tabs: the band property a row edits. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +enum MixerTab { + Hue, + Saturation, + Luminance, +} + +impl MixerTab { + /// Reads and writes the tab's value of band `k`. + fn accessors(self) -> (fn(&RawDevelopSettings, usize) -> f32, fn(&mut RawDevelopSettings, usize, f32)) { + match self { + MixerTab::Hue => (|s, k| s.hsl_hue[k], |s, k, v| s.hsl_hue[k] = v), + MixerTab::Saturation => (|s, k| s.hsl_sat[k], |s, k, v| s.hsl_sat[k] = v), + MixerTab::Luminance => (|s, k| s.hsl_lum[k], |s, k, v| s.hsl_lum[k] = v), + } + } +} + +/// The eight bands: their word and the hue (degrees) their track is drawn around. +const BANDS: [(fn(&RawDevelopWords) -> &Label, f32); 8] = [ + (|w| &w.band_reds, 0.0), + (|w| &w.band_oranges, 30.0), + (|w| &w.band_yellows, 60.0), + (|w| &w.band_greens, 120.0), + (|w| &w.band_aquas, 180.0), + (|w| &w.band_blues, 240.0), + (|w| &w.band_purples, 270.0), + (|w| &w.band_magentas, 300.0), +]; + +/// The track of a band's row on `tab`: the hue shifting around the band, the saturation rising, the luminance rising. +fn band_stops(tab: MixerTab, hue: f32) -> Vec { + let color = |h: f32, s: f32, v: f32| { + let unit = (h / 360.0).rem_euclid(1.0); + DataColor::new(Color32::from(egui::ecolor::Hsva::new(unit, s, v, 1.0))) + }; + match tab { + MixerTab::Hue => vec![color(hue - 30.0, 0.9, 1.0), color(hue, 0.9, 1.0), color(hue + 30.0, 0.9, 1.0)], + MixerTab::Saturation => vec![color(hue, 0.15, 0.9), color(hue, 1.0, 1.0)], + MixerTab::Luminance => vec![color(hue, 0.9, 0.25), color(hue, 0.9, 0.7), color(hue, 0.3, 1.0)], + } +} + +/// One grading wheel: its word and its settings field. +#[derive(Clone, Copy)] +struct Wheel { + word: fn(&RawDevelopWords) -> &Label, + get: fn(&RawDevelopSettings) -> &GradeWheel, + get_mut: fn(&mut RawDevelopSettings) -> &mut GradeWheel, +} + +const WHEELS: [Wheel; 4] = [ + Wheel { word: |w| &w.wheel_shadows, get: |s| &s.grade_shadows, get_mut: |s| &mut s.grade_shadows }, + Wheel { word: |w| &w.wheel_midtones, get: |s| &s.grade_midtones, get_mut: |s| &mut s.grade_midtones }, + Wheel { word: |w| &w.wheel_highlights, get: |s| &s.grade_highlights, get_mut: |s| &mut s.grade_highlights }, + Wheel { word: |w| &w.wheel_global, get: |s| &s.grade_global, get_mut: |s| &mut s.grade_global }, +]; + +/// The Color Mixer section: the Hue, Saturation and Luminance tabs over the eight bands. The tabs keep the chosen tab, +/// Hue until one is picked. +pub(super) fn color_mixer(ui: &mut Ui, rows: &mut Rows<'_>, words: &RawDevelopWords) { + section(&words.section_color_mixer, "color_mixer", false).show(ui, |ui, _| { + let tabs = [ + Entry::new(MixerTab::Hue, words.mixer_hue.clone()), + Entry::new(MixerTab::Saturation, words.mixer_saturation.clone()), + Entry::new(MixerTab::Luminance, words.mixer_luminance.clone()), + ]; + let shown = Segmented::new(words.section_color_mixer.clone(), Bind::kept().reset(MixerTab::Hue), &tabs).show(ui); + let kept = shown.shown.as_ref().and_then(|held| held.same()).map(|tab| **tab); + let tab = shown.value().copied().or(kept).unwrap_or(MixerTab::Hue); + let (get, set) = tab.accessors(); + let (base, defaults) = (rows.base, rows.defaults); + for (k, (word, hue)) in BANDS.iter().enumerate() { + let stops = Some(band_stops(tab, *hue)); + rows.slider(ui, word(words), get(base, k), get(defaults, k), -100.0..=100.0, 1.0, stops, move |settings, v| set(settings, k, v)); + } + }); +} + +/// The Color Grading section: Hue, Saturation and Luminance rows for each wheel, then Blending and Balance. +pub(super) fn color_grading(ui: &mut Ui, rows: &mut Rows<'_>, words: &RawDevelopWords) { + section(&words.section_color_grading, "color_grading", false).show(ui, |ui, _| { + for wheel in WHEELS { + Section::fixed((wheel.word)(words).clone()).strength(HeaderStrength::Caption).show(ui, |ui, _| { + let (now, was) = ((wheel.get)(rows.base), (wheel.get)(rows.defaults)); + let (now, was) = (*now, *was); + rows.slider(ui, &words.mixer_hue, now.hue, was.hue, 0.0..=360.0, 1.0, None, move |s, v| (wheel.get_mut)(s).hue = v); + rows.slider(ui, &words.mixer_saturation, now.sat, was.sat, 0.0..=100.0, 1.0, None, move |s, v| (wheel.get_mut)(s).sat = v); + rows.slider(ui, &words.mixer_luminance, now.lum, was.lum, -100.0..=100.0, 1.0, None, move |s, v| (wheel.get_mut)(s).lum = v); + }); + } + rows.table(ui, words, GRADING_TAIL); + }); +} diff --git a/crates/imaging/src/ui/raw_develop/preview.rs b/crates/imaging/src/ui/raw_develop/preview.rs new file mode 100644 index 000000000..268914a9e --- /dev/null +++ b/crates/imaging/src/ui/raw_develop/preview.rs @@ -0,0 +1,54 @@ +//! The preview's controls on the workspace canvas: the colour samplers' readouts and the clipping overlay. The +//! workspace owns the sampler tool, the points and the cache; these functions read the source for them. + +use std::sync::Arc; + +use craft_color::ui::{ColorSamplers, SamplerCache, SamplerReadout, SamplerReadouts}; +use craft_theme::Srgba; +use craft_ui::primitives::Revision; +use craft_ui::runtime::env::Label; + +use super::source::RawDevelopSource; +use crate::scopes::ClippingMask; +use crate::ui::scopes::{ClippingMode, ClippingOverlay, ScopeReadout}; + +/// The readouts of `samplers` over the picture `before` or after, from `cache` while `revision` and the points are +/// unchanged. The samplers ask in RGB; with `lab` (Show Lab Color Readouts) each readout also carries its Lab value, +/// asked of the source again for the few points. +pub(super) fn sampler_readouts( + samplers: &ColorSamplers, + source: &dyn RawDevelopSource, + before: bool, + revision: u64, + lab: bool, + cache: &mut SamplerCache, +) -> Arc> { + let read = |at: [f64; 2]| match source.sample(at, before, false) { + Some(ScopeReadout::Rgb([r, g, b])) => Srgba { r, g, b, a: 1.0 }, + _ => Srgba { r: 0.0, g: 0.0, b: 0.0, a: 0.0 }, + }; + let readouts = SamplerReadouts::new(samplers, read).revision(revision).get(cache); + if !lab { + return readouts; + } + let mut with_lab = (*readouts).clone(); + for readout in &mut with_lab { + let at = readout.position.map(|v| v as f64); + readout.lab = match source.sample(at, before, true) { + Some(ScopeReadout::Lab([l, a, b])) => Some([l.round().clamp(0.0, 100.0) as i16, a.round().clamp(-128.0, 127.0) as i16, b.round().clamp(-128.0, 127.0) as i16]), + _ => None, + }; + } + Arc::new(with_lab) +} + +/// The source's clipping mask and its revision while a warning is on; nothing when `mode` shows none. +pub(super) fn clipping_state(source: &dyn RawDevelopSource, mode: ClippingMode) -> Option<(Arc, Revision)> { + if matches!(mode, ClippingMode::None) { None } else { source.clipping() } +} + +/// The overlay of `held` (from [`clipping_state`]) in `mode`. The overlay rebuilds its texture only when the revision, +/// the mode or the theme's inks change. +pub(super) fn clipping_overlay<'a>(label: impl Into