diff --git a/crates/color/src/blend.rs b/crates/color/src/blend.rs index 196a0dcafd..7b26f06a4f 100644 --- a/crates/color/src/blend.rs +++ b/crates/color/src/blend.rs @@ -46,6 +46,23 @@ pub enum BlendMode { PaintNetColorDodge, } +/// Parse legacy command names and labels, retaining their case and punctuation tolerance. +pub fn parse_blend(s: &str) -> Option { + fn normalize(value: &str) -> String { + value.to_ascii_lowercase().replace([' ', '_', '-', '(', ')'], "") + } + static ALIASES: std::sync::OnceLock> = std::sync::OnceLock::new(); + let aliases = ALIASES.get_or_init(|| { + let mut aliases = std::collections::HashMap::new(); + for mode in std::iter::once(BlendMode::PassThrough).chain(BlendMode::layer_modes()) { + aliases.entry(normalize(mode.label())).or_insert(mode); + aliases.entry(normalize(&format!("{mode:?}"))).or_insert(mode); + } + aliases + }); + aliases.get(&normalize(s)).copied() +} + impl BlendMode { /// Stable key used by the Layers panel. pub fn key(self) -> &'static str { @@ -674,6 +691,25 @@ mod tests; mod key_tests { use super::BlendMode; + #[test] + fn parser_preserves_legacy_names_and_normalization() { + let normalize = |s: &str| s.to_ascii_lowercase().replace([' ', '_', '-', '(', ')'], ""); + let legacy = |s: &str| { + let wanted = normalize(s); + std::iter::once(BlendMode::PassThrough) + .chain(BlendMode::layer_modes()) + .find(|mode| normalize(mode.label()) == wanted || normalize(&format!("{mode:?}")) == wanted) + }; + for mode in std::iter::once(BlendMode::PassThrough).chain(BlendMode::layer_modes()) { + for name in [mode.label().to_owned(), format!("{mode:?}"), mode.label().to_ascii_uppercase().replace(' ', "_-")] { + assert_eq!(super::parse_blend(&name), legacy(&name), "{name}"); + } + } + for name in ["", "not a mode", "color.burn", "é"] { + assert_eq!(super::parse_blend(name), legacy(name), "{name}"); + } + } + #[test] fn canonical_keys_match_legacy_panel_names_and_round_trip() { for mode in std::iter::once(BlendMode::PassThrough).chain(BlendMode::layer_modes()) { diff --git a/crates/engine/src/cmd.rs b/crates/engine/src/cmd.rs index c2d10acae7..809fd05d96 100644 --- a/crates/engine/src/cmd.rs +++ b/crates/engine/src/cmd.rs @@ -1,6 +1,6 @@ //! The command registry. Ids follow Photoshop's menu structure (see the parity checklist). -use photocraft_color::{BlendMode, Color, ColorMode, SampleType}; +use photocraft_color::{Color, ColorMode, SampleType}; use photocraft_doc::{Adjustment, Document, Fill, Layer, LayerContent, LayerId, LayerMask, Size}; use photocraft_geom::{Rect, TILE_SIZE}; use photocraft_raster::Surface; @@ -189,13 +189,13 @@ use adjust_params::{adjust_mode, destructive_adjust, doc_mode, new_adjustment}; pub use adjust_params::{adjustment_from_params, adjustment_kind}; pub(crate) use enabled::{always, check_all_unlocked, check_pixels_unlocked, has_paintable, is_mask_target, paint_surface}; use enabled::{can_redo, can_undo, has_doc, has_layer, has_pixel_or_channel, has_selection}; -pub use layer_params::blend_from_str; pub(crate) use layer_params::{NEW_LAYER_PATH, explicit_layer, layer_listing, layer_panel_option, layer_param, mask_at_depth, mask_new_layer, selection_mask}; pub(crate) use layer_references::{recorded_drop, recorded_layer}; use ops::{arrange, merge_group, move_mask, set_mask, set_mask_and_deselect}; pub(crate) use ops::{layer_copy, translate_layer, translate_layer_wrapped}; use params::{bad, set_clipped}; pub(crate) use params::{check_f32_values, cmyk_param, color_param, int, int_i32, parse_hex, u32_id, u32_id_param}; +pub use photocraft_color::blend::parse_blend; pub fn command_specs() -> &'static [CommandSpec] { ®istry().specs @@ -726,7 +726,7 @@ fn build() -> Vec { // The Background has no opacity, fill or blend mode: like Photoshop, setting // one that changes its look turns it into a normal layer ("Layer 0") first. let num = |k: &str| p.get(k).and_then(Value::as_f64).is_some_and(|v| v < 1.0); - let blends = p.get("blend").and_then(Value::as_str).and_then(blend_from_str).is_some_and(|b| b != photocraft_color::BlendMode::Normal); + let blends = p.get("blend").and_then(Value::as_str).and_then(parse_blend).is_some_and(|b| b != photocraft_color::BlendMode::Normal); for &id in &ids { if num("opacity") || num("fill") || blends { crate::extra_cmds::background_to_layer(doc, id); @@ -745,7 +745,7 @@ fn build() -> Vec { l.fill_opacity = v; } if let Some(v) = p.get("blend").and_then(Value::as_str) { - let b = blend_from_str(v).ok_or_else(|| bad("layer.setProps", format!("unknown blend mode `{v}`")))?; + let b = parse_blend(v).ok_or_else(|| bad("layer.setProps", format!("unknown blend mode `{v}`")))?; // Pass Through is a group's mode only, as in Photoshop. if b == photocraft_color::BlendMode::PassThrough && !l.is_group() { return Err(bad("layer.setProps", format!("Pass Through applies to groups only; layer {} isn't one", l.id.0))); diff --git a/crates/engine/src/cmd/adjust_params.rs b/crates/engine/src/cmd/adjust_params.rs index 416016c8d3..fa078383e2 100644 --- a/crates/engine/src/cmd/adjust_params.rs +++ b/crates/engine/src/cmd/adjust_params.rs @@ -24,7 +24,7 @@ pub(super) fn new_adjustment(s: &mut Session, adj: Adjustment, p: &Value, cmd: & None | Some(Value::Null) => BlendMode::Normal, Some(v) => { let m = v.as_str().ok_or_else(|| bad("`mode` must be a blend mode name".into()))?; - crate::commands::blend_from_str(m).filter(|b| *b != BlendMode::PassThrough).ok_or_else(|| bad(format!("unknown layer blend mode `{m}`")))? + crate::commands::parse_blend(m).filter(|b| *b != BlendMode::PassThrough).ok_or_else(|| bad(format!("unknown layer blend mode `{m}`")))? } }; let clip = match p.get("clipToPrevious") { diff --git a/crates/engine/src/cmd/atn_cmds/steps_make.rs b/crates/engine/src/cmd/atn_cmds/steps_make.rs index 0e16f09c8f..995a7120e5 100644 --- a/crates/engine/src/cmd/atn_cmds/steps_make.rs +++ b/crates/engine/src/cmd/atn_cmds/steps_make.rs @@ -892,7 +892,7 @@ fn adjustment_layer_to(id: &str, p: &Value) -> Option { None | Some(Value::Null) => None, Some(v) => { let label = v.as_str()?; - if crate::commands::blend_from_str(label)? == photocraft_doc::BlendMode::PassThrough { + if crate::commands::parse_blend(label)? == photocraft_doc::BlendMode::PassThrough { return None; } Some(blend_ps(label)?) diff --git a/crates/engine/src/cmd/atn_cmds/tables.rs b/crates/engine/src/cmd/atn_cmds/tables.rs index 0a6a07abee..6751291997 100644 --- a/crates/engine/src/cmd/atn_cmds/tables.rs +++ b/crates/engine/src/cmd/atn_cmds/tables.rs @@ -56,7 +56,7 @@ pub(super) fn blend_label(ps: &str) -> Option<&'static str> { } pub(super) fn blend_ps(label: &str) -> Option<&'static str> { - crate::commands::blend_from_str(label).and_then(|m| BLEND_MODES.iter().find(|(_, v)| crate::commands::blend_from_str(v) == Some(m))).map(|(k, _)| *k) + crate::commands::parse_blend(label).and_then(|m| BLEND_MODES.iter().find(|(_, v)| crate::commands::parse_blend(v) == Some(m))).map(|(k, _)| *k) } /// Photoshop ↔ PhotoCraft value tables; Photoshop codes compare without their space padding. diff --git a/crates/engine/src/cmd/brush_cmds.rs b/crates/engine/src/cmd/brush_cmds.rs index f17b5b28b4..afd521e942 100644 --- a/crates/engine/src/cmd/brush_cmds.rs +++ b/crates/engine/src/cmd/brush_cmds.rs @@ -448,7 +448,7 @@ fn with_blend_mode(mut b: BrushSettings, p: &Value) -> BrushSettings { } else if p.get("mode").and_then(Value::as_str).is_some_and(|m| m.eq_ignore_ascii_case("behind")) { b.mode = photocraft_color::BlendMode::Normal; b.behind = true; - } else if let Some(m) = p.get("mode").and_then(Value::as_str).and_then(crate::commands::blend_from_str) { + } else if let Some(m) = p.get("mode").and_then(Value::as_str).and_then(crate::commands::parse_blend) { b.mode = m; b.behind = false; } diff --git a/crates/engine/src/cmd/channel_cmds/refs.rs b/crates/engine/src/cmd/channel_cmds/refs.rs index 39a56e1928..eace867c12 100644 --- a/crates/engine/src/cmd/channel_cmds/refs.rs +++ b/crates/engine/src/cmd/channel_cmds/refs.rs @@ -314,7 +314,7 @@ impl Blending { match name.to_ascii_lowercase().as_str() { "add" => Ok(Blending::Add { scale, offset }), "subtract" => Ok(Blending::Subtract { scale, offset }), - other => match crate::commands::blend_from_str(other) { + other => match crate::commands::parse_blend(other) { Some(m) if m.is_separable() && !matches!(m, BlendMode::Dissolve | BlendMode::PassThrough) => Ok(Blending::Mode(m)), _ => Err(format!("unsupported blending `{other}`")), }, diff --git a/crates/engine/src/cmd/edit_menu_cmds.rs b/crates/engine/src/cmd/edit_menu_cmds.rs index fab273a9a6..a78e39e49b 100644 --- a/crates/engine/src/cmd/edit_menu_cmds.rs +++ b/crates/engine/src/cmd/edit_menu_cmds.rs @@ -14,7 +14,7 @@ use serde::{Deserialize, Serialize}; use serde_json::{Value, json}; use crate::channel_cmds::Target; -use crate::commands::{CommandSpec, blend_from_str, int, layer_param}; +use crate::commands::{CommandSpec, int, layer_param, parse_blend}; use crate::{EngineError, Result, Session}; // ------------------------------------------------------------------ state diff --git a/crates/engine/src/cmd/edit_menu_cmds/fade.rs b/crates/engine/src/cmd/edit_menu_cmds/fade.rs index e8c797b212..59f15057e3 100644 --- a/crates/engine/src/cmd/edit_menu_cmds/fade.rs +++ b/crates/engine/src/cmd/edit_menu_cmds/fade.rs @@ -1,5 +1,5 @@ use super::{ - BlendMode, Document, EngineError, FadeSource, FadeTarget, Rect, Result, Session, Surface, Value, bad, blend_from_str, f32_or, from_rgba_into, json, str_or, + BlendMode, Document, EngineError, FadeSource, FadeTarget, Rect, Result, Session, Surface, Value, bad, f32_or, from_rgba_into, json, parse_blend, str_or, surface_of, surface_of_mut, to_rgba, }; @@ -103,7 +103,7 @@ fn fade_job(s: &Session, p: &Value) -> Result { let cmd = "edit.fade"; let src = s.edit_state.fade.clone().ok_or_else(|| bad(cmd, "nothing to fade"))?; let opacity = f32_or(p, "opacity", 100.0).clamp(0.0, 100.0) / 100.0; - let mode = blend_from_str(str_or(p, "mode", "normal")).ok_or_else(|| bad(cmd, "unknown blend mode"))?; + let mode = parse_blend(str_or(p, "mode", "normal")).ok_or_else(|| bad(cmd, "unknown blend mode"))?; let st = s.active().ok_or(EngineError::NoDocument)?; let prev = before_state(st, &src).ok_or_else(|| bad(cmd, "no previous state"))?; let before = surface_of(&prev, src.target).cloned().ok_or_else(|| EngineError::Other("the layer or channel did not exist before this step".into()))?; diff --git a/crates/engine/src/cmd/extra_cmds.rs b/crates/engine/src/cmd/extra_cmds.rs index 505048f6ae..7e09659db9 100644 --- a/crates/engine/src/cmd/extra_cmds.rs +++ b/crates/engine/src/cmd/extra_cmds.rs @@ -147,7 +147,7 @@ fn stroke(s: &mut Session, p: &Value) -> Result { let picked = s.tools.picked_cmyk([color[0], color[1], color[2]]); let mode = match p.get("mode") { None | Some(Value::Null) => photocraft_color::BlendMode::Normal, - Some(Value::String(m)) => crate::commands::blend_from_str(m) + Some(Value::String(m)) => crate::commands::parse_blend(m) .filter(|m| *m != photocraft_color::BlendMode::PassThrough) .ok_or_else(|| EngineError::BadParams { cmd: "edit.stroke".into(), msg: format!("unknown blend mode `{m}`") })?, Some(v) => return Err(EngineError::BadParams { cmd: "edit.stroke".into(), msg: format!("mode must be a blend mode name, not {v}") }), diff --git a/crates/engine/src/cmd/fill_cmds.rs b/crates/engine/src/cmd/fill_cmds.rs index 988a5b4aad..2d16adb077 100644 --- a/crates/engine/src/cmd/fill_cmds.rs +++ b/crates/engine/src/cmd/fill_cmds.rs @@ -11,7 +11,7 @@ use photocraft_geom::{Rect, TILE_SIZE}; use photocraft_raster::{Surface, from_rgba_into, to_rgba}; use serde_json::{Value, json}; -use crate::commands::{blend_from_str, color_param, layer_param}; +use crate::commands::{color_param, layer_param, parse_blend}; use crate::{EngineError, Result, Session}; const CMD: &str = "edit.fill"; @@ -66,7 +66,7 @@ fn contents(p: &Value) -> Result<&str> { fn mode(p: &Value) -> Result { match p.get("mode") { None | Some(Value::Null) => Ok(BlendMode::Normal), - Some(Value::String(m)) => blend_from_str(m).filter(|m| *m != BlendMode::PassThrough).ok_or_else(|| bad(format!("unknown blend mode `{m}`"))), + Some(Value::String(m)) => parse_blend(m).filter(|m| *m != BlendMode::PassThrough).ok_or_else(|| bad(format!("unknown blend mode `{m}`"))), Some(v) => Err(bad(format!("`mode` must be a blend mode name, not {v}"))), } } diff --git a/crates/engine/src/cmd/gradient_fill_cmds.rs b/crates/engine/src/cmd/gradient_fill_cmds.rs index 39a61de8f4..7a4e036409 100644 --- a/crates/engine/src/cmd/gradient_fill_cmds.rs +++ b/crates/engine/src/cmd/gradient_fill_cmds.rs @@ -17,7 +17,7 @@ use photocraft_doc::{Document, Fill, GradientStyle, Layer, LayerContent, LayerId use photocraft_geom::Rect; use serde_json::{Value, json}; -use crate::commands::{CommandSpec, blend_from_str, check_pixels_unlocked, layer_param}; +use crate::commands::{CommandSpec, check_pixels_unlocked, layer_param, parse_blend}; use crate::presets::gradients::{GradientPreset, transparency_param}; use crate::{EngineError, Result, Session}; @@ -300,7 +300,7 @@ pub fn new_layer(s: &Session, doc: &Document, p: &Value) -> Result { let opacity = num(p, "opacity").unwrap_or(100.0).clamp(0.0, 100.0) / 100.0; let blend = match p.get("mode").and_then(Value::as_str) { None | Some("") => photocraft_color::BlendMode::Normal, - Some(m) => blend_from_str(m).ok_or_else(|| bad(CMD, format!("unknown blend mode `{m}`")))?, + Some(m) => parse_blend(m).ok_or_else(|| bad(CMD, format!("unknown blend mode `{m}`")))?, }; let canvas = doc.bounds(); if canvas.is_empty() { diff --git a/crates/engine/src/cmd/layer_menu_cmds.rs b/crates/engine/src/cmd/layer_menu_cmds.rs index 944b6755c6..448b047067 100644 --- a/crates/engine/src/cmd/layer_menu_cmds.rs +++ b/crates/engine/src/cmd/layer_menu_cmds.rs @@ -525,7 +525,7 @@ fn blending_options(s: &mut Session, p: &Value) -> Result { const CMD: &str = "layer.layerStyle.blendingOptions"; let id = layer_param(s, p)?; let blend = match p.get("blend").and_then(Value::as_str) { - Some(b) => Some(crate::commands::blend_from_str(b).ok_or_else(|| bad(CMD, format!("unknown blend mode `{b}`")))?), + Some(b) => Some(crate::commands::parse_blend(b).ok_or_else(|| bad(CMD, format!("unknown blend mode `{b}`")))?), None => None, }; let opacity = p.get("opacity").and_then(Value::as_f64).map(|v| (v as f32 / 100.0).clamp(0.0, 1.0)); diff --git a/crates/engine/src/cmd/layer_params.rs b/crates/engine/src/cmd/layer_params.rs index 82726f8369..1f15afea2e 100644 --- a/crates/engine/src/cmd/layer_params.rs +++ b/crates/engine/src/cmd/layer_params.rs @@ -1,5 +1,5 @@ use super::params::bad; -use super::{BlendMode, Document, EngineError, Layer, LayerId, LayerMask, Result, SampleType, Session, Value}; +use super::{Document, EngineError, Layer, LayerId, LayerMask, Result, SampleType, Session, Value}; /// The layer a command's `"layer"` param names: an id, or a name (the topmost layer of that /// name, as Photoshop's "select layer by name" action step finds it, so a loaded action replays @@ -72,12 +72,6 @@ pub(crate) fn layer_param(s: &Session, p: &Value) -> Result { } } -pub fn blend_from_str(s: &str) -> Option { - let norm = |x: &str| x.to_ascii_lowercase().replace([' ', '_', '-', '(', ')'], ""); - let want = norm(s); - std::iter::once(BlendMode::PassThrough).chain(BlendMode::layer_modes()).find(|m| norm(m.label()) == want || norm(&format!("{m:?}")) == want) -} - /// The active selection as the mask of a layer being created, or None without a selection. /// Photoshop masks a new adjustment or fill layer with the selection, so the adjustment acts on /// the selected area only (#1250). [`mask_new_layer`] then drops the selection (#8801). diff --git a/crates/engine/src/cmd/paint_cmds.rs b/crates/engine/src/cmd/paint_cmds.rs index e4e0c2265a..ffe6720b56 100644 --- a/crates/engine/src/cmd/paint_cmds.rs +++ b/crates/engine/src/cmd/paint_cmds.rs @@ -3,7 +3,7 @@ use photocraft_algo::paint::{FillMode, GradientShape, bucket_fill_inks, bucket_fill_region_inks, paint_gradient}; use serde_json::{Value, json}; -use crate::commands::{CommandSpec, blend_from_str}; +use crate::commands::{CommandSpec, parse_blend}; use crate::presets::gradients; use crate::{EngineError, Result, Session}; @@ -47,7 +47,7 @@ fn fill_mode(p: &Value) -> Result { match p.get("mode") { None | Some(Value::Null) => Ok(FillMode::Blend(photocraft_color::BlendMode::Normal)), Some(Value::String(m)) if m.eq_ignore_ascii_case("clear") => Ok(FillMode::Clear), - Some(Value::String(m)) => blend_from_str(m) + Some(Value::String(m)) => parse_blend(m) .filter(|m| *m != photocraft_color::BlendMode::PassThrough) .map(FillMode::Blend) .ok_or_else(|| bad(format!("unknown blend mode `{m}`"))), @@ -186,7 +186,7 @@ fn gradient(s: &mut Session, p: &Value) -> Result { }; let reverse = b(p, "reverse", false); let opacity = opacity(p, "paint.gradient")?; - let blend = p.get("mode").and_then(Value::as_str).and_then(blend_from_str).unwrap_or(photocraft_color::BlendMode::Normal); + let blend = p.get("mode").and_then(Value::as_str).and_then(parse_blend).unwrap_or(photocraft_color::BlendMode::Normal); let dither = b(p, "dither", true); // Photoshop dithers gradients by default (reduces banding). let layer = if p.get("layer").is_some() { Some(crate::commands::layer_param(s, p)?) } else { s.active().and_then(|st| st.active_layer) }; s.edit("Gradient", |doc, _| { diff --git a/crates/engine/src/cmd/retouch_cmds.rs b/crates/engine/src/cmd/retouch_cmds.rs index 95cbe7ca99..868a5428ba 100644 --- a/crates/engine/src/cmd/retouch_cmds.rs +++ b/crates/engine/src/cmd/retouch_cmds.rs @@ -22,7 +22,7 @@ use photocraft_raster::{Surface, from_rgba_into, to_rgba}; use rayon::prelude::*; use serde_json::{Value, json}; -use crate::commands::{CommandSpec, blend_from_str}; +use crate::commands::{CommandSpec, parse_blend}; use crate::{EngineError, Result, Session}; mod art_history; diff --git a/crates/engine/src/cmd/retouch_cmds/clone.rs b/crates/engine/src/cmd/retouch_cmds/clone.rs index ada043ade4..59c39ab183 100644 --- a/crates/engine/src/cmd/retouch_cmds/clone.rs +++ b/crates/engine/src/cmd/retouch_cmds/clone.rs @@ -3,7 +3,7 @@ use super::heal::history_source; use super::sampling::{SampleLayers, sample_layers}; use super::{ BlendMode, BrushSettings, Document, EngineError, LayerId, PixelFormat, Rect, Region, Result, SampleType, Session, Stroke, StrokePoint, Surface, Value, - alpha_index, apply_coverage, bad, blend_from_str, coverage, damage_json, flag, has_pixel_layer, json, parse_brush, pattern_stamp, periodic, point, + alpha_index, apply_coverage, bad, coverage, damage_json, flag, has_pixel_layer, json, parse_blend, parse_brush, pattern_stamp, periodic, point, reject_huge_stroke, run_stroke, to_rgba, }; @@ -212,7 +212,7 @@ pub(super) fn clone_result(dmg: Rect, off: (i32, i32), aligned: bool, p: &Value, pub(super) fn blend_param(p: &Value, cmd: &str) -> Result { match p.get("mode").and_then(Value::as_str) { None => Ok(BlendMode::Normal), - Some(m) => blend_from_str(m).ok_or_else(|| bad(cmd, format!("unknown blend mode `{m}`"))), + Some(m) => parse_blend(m).ok_or_else(|| bad(cmd, format!("unknown blend mode `{m}`"))), } } diff --git a/crates/engine/src/cmd/retouch_cmds/dab.rs b/crates/engine/src/cmd/retouch_cmds/dab.rs index 564df7512d..7c17ecf9e5 100644 --- a/crates/engine/src/cmd/retouch_cmds/dab.rs +++ b/crates/engine/src/cmd/retouch_cmds/dab.rs @@ -1,8 +1,8 @@ use super::sampling::{AllLayers, sample_all_layers}; use super::{ BlendMode, BrushSettings, Document, EngineError, Footprint, IndexedParallelIterator, LayerId, ParallelIterator, ParallelSliceMut, PixelFormat, Rect, - Region, Result, Session, Smudge, Stroke, StrokePoint, Surface, ToneRange, Value, alpha_index, apply_dab_stroke, bad, blend_from_str, damage_json, - dodge_burn, flag, from_rgba_into, has_pixel_layer, json, local_blur, local_sharpen, num, parse_brush, run_stroke, sponge, string, to_rgba, + Region, Result, Session, Smudge, Stroke, StrokePoint, Surface, ToneRange, Value, alpha_index, apply_dab_stroke, bad, damage_json, dodge_burn, flag, + from_rgba_into, has_pixel_layer, json, local_blur, local_sharpen, num, parse_blend, parse_brush, run_stroke, sponge, string, to_rgba, }; // --------------------------------------------------------------------------------------------- @@ -206,7 +206,7 @@ pub(super) enum DabTool { fn smudge_mode(p: &Value, cmd: &str) -> Result { use BlendMode::{Color, Darken, Hue, Lighten, Luminosity, Normal, Saturation}; let Some(v) = p.get("mode") else { return Ok(Normal) }; - let named = v.as_str().and_then(blend_from_str).filter(|m| matches!(m, Normal | Darken | Lighten | Hue | Saturation | Color | Luminosity)); + let named = v.as_str().and_then(parse_blend).filter(|m| matches!(m, Normal | Darken | Lighten | Hue | Saturation | Color | Luminosity)); named.ok_or_else(|| { let given = v.as_str().map_or_else(|| v.to_string(), str::to_owned); bad(cmd, format!("unknown mode `{given}` (normal|darken|lighten|hue|saturation|color|luminosity)")) diff --git a/crates/engine/src/cmd/smart_cmds.rs b/crates/engine/src/cmd/smart_cmds.rs index ada909f54b..ffb172cf88 100644 --- a/crates/engine/src/cmd/smart_cmds.rs +++ b/crates/engine/src/cmd/smart_cmds.rs @@ -34,7 +34,7 @@ use photocraft_raster::Surface; use serde_json::{Value, json}; use stack_cache::{Stage, StageKey}; -use crate::commands::{CommandSpec, blend_from_str, layer_param}; +use crate::commands::{CommandSpec, layer_param, parse_blend}; use crate::jobs::JobCtx; use crate::{EngineError, Result, Session}; diff --git a/crates/engine/src/cmd/smart_cmds/filters.rs b/crates/engine/src/cmd/smart_cmds/filters.rs index c37c17a6f2..46a5ccba39 100644 --- a/crates/engine/src/cmd/smart_cmds/filters.rs +++ b/crates/engine/src/cmd/smart_cmds/filters.rs @@ -1,8 +1,8 @@ use super::rendering::{refresh_or_fail, refresh_or_fail_in}; use super::source::source_bytes; use super::{ - BlendMode, Document, EngineError, Layer, LayerContent, LayerId, LayerMask, Result, Session, SmartObject, Surface, Value, bad, blend_from_str, json, - layer_param, other, + BlendMode, Document, EngineError, Layer, LayerContent, LayerId, LayerMask, Result, Session, SmartObject, Surface, Value, bad, json, layer_param, other, + parse_blend, }; pub(super) fn smart(doc: &Document, id: LayerId) -> Result<&SmartObject> { @@ -241,7 +241,7 @@ pub(super) fn set_filter_params(s: &mut Session, p: &Value) -> Result { pub(super) fn blending_options(s: &mut Session, p: &Value) -> Result { const CMD: &str = "layer.smartFilter.blendingOptions"; let blend = match p.get("blend").and_then(Value::as_str) { - Some(b) => Some(blend_from_str(b).filter(|m| *m != BlendMode::PassThrough).ok_or_else(|| bad(CMD, format!("unknown blend mode `{b}`")))?), + Some(b) => Some(parse_blend(b).filter(|m| *m != BlendMode::PassThrough).ok_or_else(|| bad(CMD, format!("unknown blend mode `{b}`")))?), None => None, }; let opacity = p.get("opacity").and_then(Value::as_f64).map(|o| if o > 1.0 { o / 100.0 } else { o } as f32); diff --git a/crates/engine/src/cmd/vector_cmds.rs b/crates/engine/src/cmd/vector_cmds.rs index 9c91142e26..aa9e9a33ce 100644 --- a/crates/engine/src/cmd/vector_cmds.rs +++ b/crates/engine/src/cmd/vector_cmds.rs @@ -26,7 +26,7 @@ use photocraft_geom::{Affine, Point, Rect}; use photocraft_vector as vector; use serde_json::{Value, json}; -use crate::commands::{CommandSpec, blend_from_str}; +use crate::commands::{CommandSpec, parse_blend}; use crate::{EngineError, Result, Session}; /// Keys PSD uses for a shape layer's vector data; dropped when a shape is rasterized. diff --git a/crates/engine/src/cmd/vector_cmds/paths.rs b/crates/engine/src/cmd/vector_cmds/paths.rs index df8d922b45..5fdda419f9 100644 --- a/crates/engine/src/cmd/vector_cmds/paths.rs +++ b/crates/engine/src/cmd/vector_cmds/paths.rs @@ -2,7 +2,7 @@ use super::params::{color, f64p, layer_id, op_from, parse_path, path_json}; use super::shape::{map_path, map_shape, transform_shape, with_shape}; use super::{ Affine, BlendMode, ClippingPath, Color, Document, EngineError, LayerContent, LayerId, NamedPath, Path, Rect, Result, SelectionMode, Session, Value, bad, - blend_from_str, json, sel, vector, + json, parse_blend, sel, vector, }; // --------------------------------------------------------------------------- @@ -444,7 +444,7 @@ pub(super) fn path_fill(s: &mut Session, p: &Value) -> Result { let rgb = c.to_rgb(); let src = [rgb[0], rgb[1], rgb[2], c.alpha]; let opacity = (f64p(p, "opacity").unwrap_or(100.0) / 100.0).clamp(0.0, 1.0) as f32; - let mode = p.get("mode").and_then(Value::as_str).and_then(blend_from_str).unwrap_or(BlendMode::Normal); + let mode = p.get("mode").and_then(Value::as_str).and_then(parse_blend).unwrap_or(BlendMode::Normal); let feather = f64p(p, "feather").unwrap_or(0.0) as f32; let aa = p.get("antiAlias").and_then(Value::as_bool).unwrap_or(true); let id = layer_id(s, p)?; diff --git a/crates/engine/src/group_options.rs b/crates/engine/src/group_options.rs index dd2ddee02f..a2a50f027b 100644 --- a/crates/engine/src/group_options.rs +++ b/crates/engine/src/group_options.rs @@ -37,9 +37,7 @@ impl GroupOptions { let blend = match get("mode") { None => None, Some(v) => Some( - v.as_str() - .and_then(crate::commands::blend_from_str) - .ok_or_else(|| bad("`mode` must be a blend mode such as \"passThrough\" or \"multiply\""))?, + v.as_str().and_then(crate::commands::parse_blend).ok_or_else(|| bad("`mode` must be a blend mode such as \"passThrough\" or \"multiply\""))?, ), }; let opacity = match get("opacity") { diff --git a/crates/engine/src/layer_style.rs b/crates/engine/src/layer_style.rs index 9f7c2c7873..6a4e683fa7 100644 --- a/crates/engine/src/layer_style.rs +++ b/crates/engine/src/layer_style.rs @@ -9,7 +9,7 @@ use photocraft_doc::{ }; use serde_json::{Value, json}; -use crate::commands::{CommandSpec, blend_from_str}; +use crate::commands::{CommandSpec, parse_blend}; use crate::presets::{always, bad}; use crate::{EngineError, Result, Session}; @@ -714,7 +714,7 @@ fn overlay_common(c: &mut FxCommon, p: &Value) { if let Some(x) = b_of(p, "enabled") { c.enabled = x; } - if let Some(x) = p.get("blend").and_then(Value::as_str).and_then(blend_from_str) { + if let Some(x) = p.get("blend").and_then(Value::as_str).and_then(parse_blend) { c.blend = x; } if let Some(x) = f_of(p, "opacity") { @@ -883,10 +883,10 @@ fn overlay_bevel(bv: &mut Bevel, p: &Value) { } overlay_contour(p, "glossContour", &mut bv.gloss_contour); overlay_bool(p, "glossAntiAlias", &mut bv.gloss_anti_alias); - if let Some(blend) = p.get("highlightBlend").and_then(Value::as_str).and_then(blend_from_str) { + if let Some(blend) = p.get("highlightBlend").and_then(Value::as_str).and_then(parse_blend) { bv.highlight.blend = blend; } - if let Some(blend) = p.get("shadowBlend").and_then(Value::as_str).and_then(blend_from_str) { + if let Some(blend) = p.get("shadowBlend").and_then(Value::as_str).and_then(parse_blend) { bv.shadow.blend = blend; } overlay_color(p, "highlightColor", &mut bv.highlight_color); diff --git a/crates/engine/src/new_layer.rs b/crates/engine/src/new_layer.rs index b102907ccc..ed05e24c8e 100644 --- a/crates/engine/src/new_layer.rs +++ b/crates/engine/src/new_layer.rs @@ -74,7 +74,7 @@ pub(crate) fn parse(p: &Value) -> Result { None | Some(Value::Null) => BlendMode::Normal, Some(v) => { let name = v.as_str().ok_or_else(|| bad("`mode` must be a blend mode name"))?; - crate::commands::blend_from_str(name).filter(|m| *m != BlendMode::PassThrough).ok_or_else(|| bad(format!("unknown layer blend mode `{name}`")))? + crate::commands::parse_blend(name).filter(|m| *m != BlendMode::PassThrough).ok_or_else(|| bad(format!("unknown layer blend mode `{name}`")))? } }; let opacity = match p.get("opacity") { diff --git a/crates/engine/src/presets/styles.rs b/crates/engine/src/presets/styles.rs index cac8fb158a..241cea036f 100644 --- a/crates/engine/src/presets/styles.rs +++ b/crates/engine/src/presets/styles.rs @@ -280,7 +280,7 @@ fn new_preset(s: &mut Session, p: &Value) -> Result { } } }, - blend: p.get("blend").and_then(Value::as_str).and_then(crate::commands::blend_from_str).or_else(|| with_blend.then_some(l.blend)), + blend: p.get("blend").and_then(Value::as_str).and_then(crate::commands::parse_blend).or_else(|| with_blend.then_some(l.blend)), fill_opacity: p.get("fillOpacity").and_then(Value::as_f64).map(|v| (v / 100.0).clamp(0.0, 1.0) as f32).or_else(|| with_blend.then_some(l.fill_opacity)), opacity: p.get("opacity").and_then(Value::as_f64).map(|v| (v / 100.0).clamp(0.0, 1.0) as f32).or_else(|| with_blend.then_some(l.opacity)), }; diff --git a/crates/ui-egui/src/control/dialogs.rs b/crates/ui-egui/src/control/dialogs.rs index 3e40a1788a..f7423d0441 100644 --- a/crates/ui-egui/src/control/dialogs.rs +++ b/crates/ui-egui/src/control/dialogs.rs @@ -199,14 +199,10 @@ fn dont_save_all(_: &Value, _: &Value) -> Vec<(String, Value)> { vec![pair("applyToAll", json!(true)), pair("choice", json!("dontSave"))] } -/// A blend label such as "Multiply" to the Blend row's id; a string `blend_from_str` refuses passes through so the +/// A blend label such as "Multiply" to the Blend row's id; a string `parse_blend` refuses passes through so the /// row refuses it. fn blend_id(value: &Value) -> Value { - value - .as_str() - .and_then(photocraft_engine::commands::blend_from_str) - .map(|mode| json!(crate::panels::layers_tree::blend_key(mode).to_string())) - .unwrap_or_else(|| value.clone()) + value.as_str().and_then(photocraft_engine::commands::parse_blend).map(|mode| json!(mode.key().to_string())).unwrap_or_else(|| value.clone()) } // PLAN: the framework field ids `mode` (Fill, Stroke) and `blend` (Smart Filter) are assumed (dialogs-w22 by request). diff --git a/crates/ui-egui/src/control/dispatch.rs b/crates/ui-egui/src/control/dispatch.rs index ea4a202f17..52f5bb000c 100644 --- a/crates/ui-egui/src/control/dispatch.rs +++ b/crates/ui-egui/src/control/dispatch.rs @@ -615,7 +615,7 @@ fn marquee_params(app: &PhotocraftApp, p: &Value) -> std::result::Result<(Option fn gradient_blend_param(p: &Value) -> std::result::Result, String> { let Some(value) = p.get("gradientBlendMode") else { return Ok(None) }; let Some(name) = value.as_str() else { return Err("gradientBlendMode must be a blend mode name".into()) }; - photocraft_engine::commands::blend_from_str(name) + photocraft_engine::commands::parse_blend(name) .filter(|m| photocraft_color::BlendMode::LAYER_MODES.contains(m)) .map(Some) .ok_or_else(|| format!("unknown gradient blend mode `{name}`")) diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 4b7963e120..30b71c92b4 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -660,7 +660,7 @@ fn convert_blend_mode(key: &str, value: &mut Value) { } let Some(mode) = value.get_mut("mode") else { return }; if let Some(blend) = mode.as_str().and_then(crate::panels::layers_tree::blend_of) { - *mode = Value::String(crate::panels::layers_tree::blend_key(blend)); + *mode = Value::String(blend.key().to_string()); } } diff --git a/crates/ui-egui/src/dialogs/blend_options.rs b/crates/ui-egui/src/dialogs/blend_options.rs index c0a99dce95..6d48c6ec82 100644 --- a/crates/ui-egui/src/dialogs/blend_options.rs +++ b/crates/ui-egui/src/dialogs/blend_options.rs @@ -6,18 +6,18 @@ use craft::ui::runtime::env::Label; use photocraft_color::BlendMode; use serde_json::{Map, Value, json}; -use crate::panels::layers_tree::blend_key; - /// 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