diff --git a/crates/engine/src/cmd.rs b/crates/engine/src/cmd.rs index 1598870479..e5ec9865f3 100644 --- a/crates/engine/src/cmd.rs +++ b/crates/engine/src/cmd.rs @@ -799,17 +799,20 @@ fn build() -> Vec { "Gradient…", ["Layer", "New Fill Layer"], None, - r##"{"from":"#rrggbb","to":"#rrggbb","angle":deg=90,"style":"linear|radial|angle|reflected|diamond","reverse":bool,"path":"work"|saved path name|{…path}? (the active path: becomes the vector mask; else the selection is the layer mask)}"##, + r##"{"from":"#rrggbb"=foreground,"to":"#rrggbb"? (none: Foreground to Transparent),"angle":deg=90,"style":"linear|radial|angle|reflected|diamond","reverse":bool,"path":"work"|saved path name|{…path}? (the active path: becomes the vector mask; else the selection is the layer mask)}"##, has_doc, |s, p| { let a = color_param("layer.newFillLayer.gradient", p, "from", s.tools.foreground)?; - let b = color_param("layer.newFillLayer.gradient", p, "to", s.tools.background)?; + // Without a `to`, Photoshop's default Foreground to Transparent (#8708): the + // `from` colour at both ends, fading out through the transparency stops. + let to_transparent = p.get("to").is_none_or(Value::is_null); + let b = if to_transparent { a } else { color_param("layer.newFillLayer.gradient", p, "to", s.tools.background)? }; let angle = crate::gradient_fill_cmds::angle_param("layer.newFillLayer.gradient", p, 90.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 default_mask = layer_panel_option(s, "useDefaultMasksOnFillLayers", true); let id = s.edit("New Gradient Fill Layer", |doc, active| { - let fill = Fill::gradient( + let mut fill = Fill::gradient( vec![(0.0, Color::rgba(a[0], a[1], a[2], a[3])), (1.0, Color::rgba(b[0], b[1], b[2], b[3]))], angle, 1.0, @@ -817,6 +820,9 @@ fn build() -> Vec { reverse, ) .in_mode(doc.mode); + if to_transparent && let Fill::Gradient { opacity_stops, .. } = &mut fill { + *opacity_stops = vec![(0.0, 1.0), (1.0, 0.0)]; + } let mut l = Layer::new(doc.next_layer_name("Gradient Fill"), LayerContent::Fill(fill)); mask_new_layer(doc, &mut l, p, "layer.newFillLayer.gradient", default_mask)?; let id = crate::layer_place::insert_new(doc, *active, l, crate::layer_place::Place::Auto); diff --git a/crates/engine/src/cmd/gradient_fill_cmds/tests.rs b/crates/engine/src/cmd/gradient_fill_cmds/tests.rs index 3c0c2f3c6a..eca8a7bc12 100644 --- a/crates/engine/src/cmd/gradient_fill_cmds/tests.rs +++ b/crates/engine/src/cmd/gradient_fill_cmds/tests.rs @@ -386,3 +386,22 @@ fn stop_rejects_huge_indices_without_panicking() { assert_eq!(midpoints, vec![0.25]); assert_eq!(s.active().unwrap().history.past_len(), h + 1); } + +#[test] +fn new_gradient_fill_layer_defaults_to_foreground_to_transparent() { + // #8708: Photoshop's Layer › New Fill Layer › Gradient… starts from Foreground to + // Transparent: the foreground at both ends, its opacity 100% to 0%. A `to` colour keeps a + // two-colour gradient. + let mut s = session(8, "transparent"); + s.tools.foreground = [0.2, 0.4, 0.6, 1.0]; + s.tools.background = [1.0, 1.0, 1.0, 1.0]; + s.execute("layer.newFillLayer.gradient", json!({})).unwrap(); + let Fill::Gradient { stops, opacity_stops, .. } = active_fill(&s) else { panic!("not a gradient") }; + let fg = Color::rgba(0.2, 0.4, 0.6, 1.0); + assert_eq!(stops, vec![(0.0, fg), (1.0, fg)]); + assert_eq!(opacity_stops, vec![(0.0, 1.0), (1.0, 0.0)]); + s.execute("layer.newFillLayer.gradient", json!({"to": "#ff0000"})).unwrap(); + let Fill::Gradient { stops, opacity_stops, .. } = active_fill(&s) else { panic!("not a gradient") }; + assert_eq!(stops[1].1, Color::rgba(1.0, 0.0, 0.0, 1.0)); + assert!(opacity_stops.is_empty()); +}