PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix: match Photoshop Gradient Fill scale limits (#6797)
codexistence7efaa504559a
diff --git a/crates/engine/src/gradient_fill_cmds.rs b/crates/engine/src/gradient_fill_cmds.rsindex 041d31169..356d2252d 100644--- a/crates/engine/src/gradient_fill_cmds.rs+++ b/crates/engine/src/gradient_fill_cmds.rs@@ -28,9 +28,10 @@ pub const GET: &str = "gradient.fill.get";/// Largest coordinate accepted for a start/end point (pixels).const MAX_COORD: f32 = 1.0e7;-/// Scale range of a gradient fill (a fraction): Photoshop allows 10–150 % (the PSD stores a-/// percentage). A handle drag past it clamps, so the far handle stops where the scale does.-const SCALE_RANGE: (f32, f32) = (0.1, 1.5);++/// Scale range of a Gradient Fill layer (a fraction): Photoshop allows 1–1000 %.+/// Gradient Overlay effects have a separate range; the live tool creates fill layers.+pub const SCALE_RANGE: (f32, f32) = (0.01, 10.0);fn bad(cmd: &str, msg: impl Into<String>) -> EngineError {EngineError::BadParams { cmd: cmd.into(), msg: msg.into() }@@ -149,7 +150,7 @@ fn has_doc(s: &Session) -> std::result::Result<(), String> {s.active().map(|_| ()).ok_or_else(|| "no document open".into())}-fn clamp_scale(v: f32) -> f32 {+pub(crate) fn clamp_scale(v: f32) -> f32 {v.clamp(SCALE_RANGE.0, SCALE_RANGE.1)}diff --git a/crates/engine/src/gradient_fill_cmds/tests.rs b/crates/engine/src/gradient_fill_cmds/tests.rsindex efcdb494d..3c0c2f3c6 100644--- a/crates/engine/src/gradient_fill_cmds/tests.rs+++ b/crates/engine/src/gradient_fill_cmds/tests.rs@@ -27,7 +27,7 @@ fn create_makes_a_canvas_aligned_fill_layer_with_the_drag_as_handles() {let r = s.execute(CREATE,- json!({"from": [14, 16], "to": [34, 8], "style": "radial", "stops": [[0, "#ff0000"], [1, "#0000ff"]], "opacity": 50, "mode": "multiply"}),+ json!({"from": [14, 16], "to": [200, -40], "style": "radial", "stops": [[0, "#ff0000"], [1, "#0000ff"]], "opacity": 50, "mode": "multiply"}),).unwrap();assert_eq!(layer_count(&s), 2);@@ -42,7 +42,7 @@ fn create_makes_a_canvas_aligned_fill_layer_with_the_drag_as_handles() {assert!(!align && dither);let g = s.execute(GET, json!({})).unwrap();let (from, to) = (g["from"].as_array().unwrap(), g["to"].as_array().unwrap());- for (v, want) in from.iter().chain(to).zip([14.0, 16.0, 34.0, 8.0]) {+ for (v, want) in from.iter().chain(to).zip([14.0, 16.0, 200.0, -40.0]) {assert!((v.as_f64().unwrap() - want).abs() < 1e-3, "{g}");}// One undo step removes the layer.@@ -225,11 +225,11 @@ fn stops_add_move_recolour_delete_and_midpoints() {#[test]fn preview_equals_the_committed_edit() {let mut s = session(8, "white");- s.execute(CREATE, json!({"from": [4, 4], "to": [40, 20], "style": "diamond"})).unwrap();+ s.execute(CREATE, json!({"from": [4, 4], "to": [40, 20], "style": "radial"})).unwrap();let d = s.active().unwrap();let l = d.doc.layer(d.active_layer.unwrap()).unwrap().clone();let LayerContent::Fill(f) = &l.content else { panic!() };- let p = json!({"from": [10, 12], "to": [30, 2]});+ let p = json!({"from": [10, 12], "to": [150, -80]});let preview = apply_set(&l, f, d.doc.bounds(), &p, [0.0; 4], [1.0; 4]).unwrap();s.execute(SET, p).unwrap();assert_eq!(active_fill(&s), preview);@@ -316,12 +316,12 @@ fn commands_fail_gracefully() {s.execute(SET, json!({"scale": 1e30, "angle": 1e9, "offset": [1e4, -1e4]})).unwrap();let g = s.execute(GET, json!({})).unwrap();assert!(g["scale"].as_f64().unwrap().is_finite() && g["from"][0].as_f64().unwrap().is_finite());- // Scale stays within Photoshop's 10–150 %, also for a handle dragged far away.- assert!((g["scale"].as_f64().unwrap() - 150.0).abs() < 1e-3, "{g}");+ // Fill layers use Photoshop's 1–1000 %, also for a handle dragged far away.+ assert!((g["scale"].as_f64().unwrap() - 1000.0).abs() < 1e-3, "{g}");let g = s.execute(SET, json!({"from": [0, 0], "to": [1e6, 0]})).unwrap();- assert!((g["scale"].as_f64().unwrap() - 150.0).abs() < 1e-3, "{g}");- let g = s.execute(SET, json!({"scale": 1})).unwrap();- assert!((g["scale"].as_f64().unwrap() - 10.0).abs() < 1e-3, "{g}");+ assert!((g["scale"].as_f64().unwrap() - 1000.0).abs() < 1e-3, "{g}");+ let g = s.execute(SET, json!({"scale": 0.1})).unwrap();+ assert!((g["scale"].as_f64().unwrap() - 1.0).abs() < 1e-3, "{g}");}/// Dragging one handle past the scale range stops it; the other handle stays put (#1243).@@ -332,17 +332,19 @@ fn a_clamped_handle_drag_leaves_the_other_handle_alone() {for style in STYLES {for (from, to, short, short_start) in [([14.0, 16.0], [34.0, 16.0], [15.0, 16.0], [33.0, 16.0]), ([24.0, 6.0], [24.0, 26.0], [24.0, 7.0], [24.0, 25.0])]{- let mut s = session(8, "white");+ let mut s = Session::new();+ s.execute("file.new", json!({"width": 480, "height": 320})).unwrap();s.execute(CREATE, json!({"from": from, "to": to, "style": style})).unwrap();- // End dragged almost onto the start: far below 10 %.+ // End dragged almost onto the start: below 1 % on this larger canvas.let g = s.execute(SET, json!({"to": short})).unwrap();- assert!((g["scale"].as_f64().unwrap() - 10.0).abs() < 1e-3, "{style}: {g}");+ assert!((g["scale"].as_f64().unwrap() - 1.0).abs() < 1e-3, "{style}: {g}");assert!(close(pt(&g, "from"), from), "{style}: start moved to {:?}", pt(&g, "from"));// The end stops on the line towards the pointer.let e = pt(&g, "to");assert!((e[0] - from[0]) * (short[0] - from[0]) >= 0.0 && (e[1] - from[1]) * (short[1] - from[1]) >= 0.0, "{style}: {e:?}");// And the same for the start handle.- let mut s = session(8, "white");+ let mut s = Session::new();+ s.execute("file.new", json!({"width": 480, "height": 320})).unwrap();s.execute(CREATE, json!({"from": from, "to": to, "style": style})).unwrap();let g = s.execute(SET, json!({"from": short_start})).unwrap();assert!(close(pt(&g, "to"), to), "{style}: end moved: {g}");diff --git a/crates/engine/src/presets/gradients.rs b/crates/engine/src/presets/gradients.rsindex 7c92dfba9..2a700aad9 100644--- a/crates/engine/src/presets/gradients.rs+++ b/crates/engine/src/presets/gradients.rs@@ -604,7 +604,7 @@ fn apply(s: &mut Session, p: &Value) -> Result<Value> {let g = preset_param(s, p, CMD)?;let stops = g.doc_stops(s.tools.foreground, s.tools.background);let angle = p.get("angle").and_then(Value::as_f64).unwrap_or(90.0) as f32;- let scale = (p.get("scale").and_then(Value::as_f64).unwrap_or(100.0) as f32 / 100.0).clamp(0.1, 1.5);+ let scale = crate::gradient_fill_cmds::clamp_scale(p.get("scale").and_then(Value::as_f64).unwrap_or(100.0) as f32 / 100.0);let style = crate::layer_style::gradient_style(p.get("style").and_then(Value::as_str).unwrap_or("linear"));let reverse = p.get("reverse").and_then(Value::as_bool).unwrap_or(false);let id = s.edit("New Gradient Fill Layer", |doc, active| {@@ -696,7 +696,7 @@ pub fn specs() -> Vec<CommandSpec> {menu: &[],shortcut: None,params: leak(format!(- r##"{{"preset":name?=current|{stops},"angle":deg=90,"style":"linear|radial|angle|reflected|diamond"="linear","scale":10..150=100,"reverse":bool}} → {{layer}}"##+ r##"{{"preset":name?=current|{stops},"angle":deg=90,"style":"linear|radial|angle|reflected|diamond"="linear","scale":1..1000=100,"reverse":bool}} → {{layer}}"##)),enabled: super::has_doc,run: apply,diff --git a/crates/ui-egui/src/gradient_ui/properties_panel.rs b/crates/ui-egui/src/gradient_ui/properties_panel.rsindex 39b961ee2..d97510253 100644--- a/crates/ui-egui/src/gradient_ui/properties_panel.rs+++ b/crates/ui-egui/src/gradient_ui/properties_panel.rs@@ -49,7 +49,8 @@ fn options(app: &mut PhotocraftApp, ui: &mut egui::Ui, layer: &Layer) {if let Some(value) = numeric_row(ui, "angle", "Angle", angle, -180.0..=180.0, "°") {let _ = app.run(cmds::SET, json!({"layer": layer.id.0, "angle": value}));}- if let Some(value) = numeric_row(ui, "scale", "Scale", scale * 100.0, 10.0..=150.0, "%") {+ let (min, max) = cmds::SCALE_RANGE;+ if let Some(value) = numeric_row(ui, "scale", "Scale", scale * 100.0, min * 100.0..=max * 100.0, "%") {let _ = app.run(cmds::SET, json!({"layer": layer.id.0, "scale": value}));}ui.horizontal_wrapped(|ui| {