diff --git a/crates/engine-core/src/cmd/extra_cmds.rs b/crates/engine-core/src/cmd/extra_cmds.rs index d6134b50e1..4b3243e7a9 100644 --- a/crates/engine-core/src/cmd/extra_cmds.rs +++ b/crates/engine-core/src/cmd/extra_cmds.rs @@ -169,6 +169,9 @@ fn stroke(s: &mut Session, p: &Value) -> Result { }; let location = p.get("location").and_then(Value::as_str).unwrap_or("center").to_string(); let id = layer_param(s, p)?; + if let Some(st) = s.active() { + crate::pixel_fill::check_mode_available("edit.stroke", &st.doc, mode)?; + } // Stroke keeps its layer behavior for channel views; an explicit mask edits that mask. let pixels = Value::Null; let target = if crate::commands::is_mask_target(p) { p } else { &pixels }; diff --git a/crates/engine-core/src/pixel_fill.rs b/crates/engine-core/src/pixel_fill.rs index 56eb3515ee..533280fbf3 100644 --- a/crates/engine-core/src/pixel_fill.rs +++ b/crates/engine-core/src/pixel_fill.rs @@ -1,12 +1,41 @@ //! Filling and blending pixels into a surface: Edit › Fill's compositing, which Edit › Clear, //! Edit › Stroke, pastes into masks and channels, a floating selection and the painting tools share. -use photocraft_color::BlendMode; +use photocraft_color::{BlendMode, ColorMode, SampleType}; +use photocraft_doc::Document; use photocraft_geom::{Rect, TILE_SIZE}; use photocraft_raster::{Surface, from_rgba_into, to_rgba}; use crate::{EngineError, Result}; +/// The blend modes the reference application greys in its Fill, Stroke and Fade Mode lists for a document of `mode` +/// at `depth` (#13806, #14647). Combinations it wasn't measured in (Grayscale 16/32, Lab 16, CMYK 16, Multichannel, +/// Duotone…) grey none. +pub fn greyed_modes(mode: ColorMode, depth: SampleType) -> &'static [BlendMode] { + use BlendMode::*; + match (mode, depth) { + (ColorMode::Grayscale, SampleType::U8) => &[DarkerColor, LighterColor, Hue, Saturation, Color, Luminosity], + (ColorMode::Lab, SampleType::U8) => &[Darken, ColorBurn, Lighten, ColorDodge, Difference, Exclusion, Subtract, Divide], + (ColorMode::Rgb, SampleType::F32) => { + &[ColorBurn, LinearBurn, Screen, ColorDodge, Overlay, SoftLight, HardLight, VividLight, LinearLight, PinLight, HardMix, Exclusion] + } + _ => &[], + } +} + +/// An error for `cmd` when `blend` is one of [`greyed_modes`] for `doc`: the dialogs never offer it there. +pub fn check_mode_available(cmd: &str, doc: &Document, blend: BlendMode) -> Result<()> { + if !greyed_modes(doc.mode, doc.depth).contains(&blend) { + return Ok(()); + } + let model = match doc.mode { + ColorMode::Rgb => "RGB".to_string(), + ColorMode::Cmyk => "CMYK".to_string(), + other => format!("{other:?}"), + }; + Err(EngineError::BadParams { cmd: cmd.into(), msg: format!("{} is not available in {model} {}-bit documents", blend.label(), doc.depth.bits()) }) +} + /// What a pixel of the filled area gets before blending. pub enum Source { Color([f32; 4]), diff --git a/crates/engine-paint/src/cmd/fill_cmds.rs b/crates/engine-paint/src/cmd/fill_cmds.rs index 3e607f27dd..c0af527bfa 100644 --- a/crates/engine-paint/src/cmd/fill_cmds.rs +++ b/crates/engine-paint/src/cmd/fill_cmds.rs @@ -16,7 +16,8 @@ use serde_json::{Value, json}; use crate::commands::{cmyk_param, color_param, layer_param, parse_blend}; use crate::content_sampling::{content_aware, content_aware_options}; -use crate::pixel_fill::{Blend, Source, blend_into, read_rgba}; +pub use crate::pixel_fill::greyed_modes; +use crate::pixel_fill::{Blend, Source, blend_into, check_mode_available, read_rgba}; use crate::{EngineError, Result, Session}; const CMD: &str = "edit.fill"; @@ -75,6 +76,7 @@ pub fn fill(s: &mut Session, p: &Value) -> Result { let channel = crate::channel_cmds::is_channel_target(p); let id = if channel { None } else { Some(layer_param(s, p)?) }; let st = s.active().ok_or(EngineError::NoDocument)?; + check_mode_available(CMD, &st.doc, mode)?; let doc_bounds = st.doc.bounds(); let sel = st.doc.selection.clone(); let area = sel.as_ref().map_or(doc_bounds, |m| m.content_bounds().intersect(&doc_bounds)); @@ -160,3 +162,6 @@ fn color_source(s: &Session, c: [f32; 4], role: usize) -> Source { #[cfg(test)] mod tests; + +#[cfg(test)] +mod mode_by_document_tests; diff --git a/crates/engine-paint/src/cmd/fill_cmds/mode_by_document_tests.rs b/crates/engine-paint/src/cmd/fill_cmds/mode_by_document_tests.rs new file mode 100644 index 0000000000..52badaac71 --- /dev/null +++ b/crates/engine-paint/src/cmd/fill_cmds/mode_by_document_tests.rs @@ -0,0 +1,111 @@ +//! #14647: Edit › Fill and Edit › Stroke refuse the blend modes the reference application greys in their Mode lists +//! for the document's colour mode and depth (Grayscale 8-bit, Lab 8-bit, RGB 32-bit), and keep every mode elsewhere. + +use super::*; +use serde_json::json; + +/// A white 16×12 document of `mode` at `depth` bits. +fn session(mode: &str, depth: u32) -> Session { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 16, "height": 12, "mode": mode, "depth": depth, "background": "white"})).unwrap(); + s +} + +fn labels(modes: &[BlendMode]) -> Vec<&'static str> { + modes.iter().map(|m| m.label()).collect() +} + +/// The measured cases: (colour mode, depth, the greyed modes). +const MEASURED: [(&str, u32, &[&str]); 5] = [ + ("grayscale", 8, &["Darker Color", "Lighter Color", "Hue", "Saturation", "Color", "Luminosity"]), + ("lab", 8, &["Darken", "Color Burn", "Lighten", "Color Dodge", "Difference", "Exclusion", "Subtract", "Divide"]), + ( + "rgb", + 32, + &[ + "Color Burn", + "Linear Burn", + "Screen", + "Color Dodge", + "Overlay", + "Soft Light", + "Hard Light", + "Vivid Light", + "Linear Light", + "Pin Light", + "Hard Mix", + "Exclusion", + ], + ), + ("rgb", 16, &[]), + ("cmyk", 8, &[]), +]; + +/// Cases not measured: nothing greyed, as before. +const NOT_MEASURED: [(&str, u32); 4] = [("grayscale", 16), ("lab", 16), ("cmyk", 16), ("rgb", 8)]; + +#[test] +fn the_greyed_modes_follow_the_measured_colour_modes_and_depths() { + for (mode, depth, want) in MEASURED { + let s = session(mode, depth); + let doc = &s.active().unwrap().doc; + assert_eq!(labels(greyed_modes(doc.mode, doc.depth)), want, "{mode} {depth}"); + } + for (mode, depth) in NOT_MEASURED { + let s = session(mode, depth); + let doc = &s.active().unwrap().doc; + assert!(greyed_modes(doc.mode, doc.depth).is_empty(), "{mode} {depth}"); + } +} + +/// Runs `cmd` with `mode` and reports whether it ran; a refusal leaves no history step and no pixel change. +fn runs(s: &mut Session, cmd: &str, mode: BlendMode) -> bool { + let params = match cmd { + "edit.fill" => json!({"contents": "color", "color": "#336699", "mode": mode.key(), "opacity": 50}), + _ => json!({"width": 2, "color": "#336699", "location": "inside", "mode": mode.key(), "opacity": 50}), + }; + let before = s.active().unwrap().history.past_len(); + match s.execute(cmd, params) { + Ok(_) => true, + Err(e) => { + assert!(matches!(&e, EngineError::BadParams { cmd: c, msg } if c == cmd && msg.contains(mode.label())), "{cmd} {}: {e}", mode.label()); + assert_eq!(s.active().unwrap().history.past_len(), before, "{cmd} {}: a refusal records no step", mode.label()); + false + } + } +} + +#[test] +fn fill_and_stroke_refuse_a_greyed_mode_and_take_every_other() { + for cmd in ["edit.fill", "edit.stroke"] { + for (mode, depth, greyed) in MEASURED { + for blend in BlendMode::LAYER_MODES { + let mut s = session(mode, depth); + s.execute("select.all", json!({})).unwrap(); + assert_eq!(runs(&mut s, cmd, blend), !greyed.contains(&blend.label()), "{cmd} in {mode} {depth}: {}", blend.label()); + } + } + } +} + +#[test] +fn the_cases_not_measured_keep_every_mode() { + for cmd in ["edit.fill", "edit.stroke"] { + for (mode, depth) in NOT_MEASURED { + for blend in BlendMode::LAYER_MODES { + let mut s = session(mode, depth); + s.execute("select.all", json!({})).unwrap(); + assert!(runs(&mut s, cmd, blend), "{cmd} in {mode} {depth}: {}", blend.label()); + } + } + } +} + +/// The default mode, Normal, is never greyed, so a Fill without `mode` still runs everywhere. +#[test] +fn a_fill_without_a_mode_runs_in_every_measured_document() { + for (mode, depth, _) in MEASURED { + let mut s = session(mode, depth); + s.execute("edit.fill", json!({"contents": "black"})).unwrap(); + } +} diff --git a/crates/ui-egui/src/dialogs/blend_options.rs b/crates/ui-egui/src/dialogs/blend_options.rs index 6d48c6ec82..8a714d5457 100644 --- a/crates/ui-egui/src/dialogs/blend_options.rs +++ b/crates/ui-egui/src/dialogs/blend_options.rs @@ -1,11 +1,14 @@ //! The Blending rows that Fill, Stroke and the Smart Filter Blending Options dialog share: the mode list is the -//! Layers panel's, so a mode has one key and one shortcut everywhere. +//! Layers panel's, so a mode has one key and one shortcut everywhere. Fill, Stroke and Fade grey the modes the active +//! document's colour mode and depth can't use (#13806, #14647). use craft::ui::patterns::form_dialogs::{DialogFields, Param, ParamKind}; use craft::ui::runtime::env::Label; use photocraft_color::BlendMode; use serde_json::{Map, Value, json}; +use crate::PhotocraftApp; + /// A blend-mode row `key` over the Layers panel's mode list (`group` adds Pass Through), opening on `current`. pub(crate) fn blend_param(key: &str, label: impl Into