diff --git a/crates/engine-core/src/cmd/gradient_fill_cmds/source.rs b/crates/engine-core/src/cmd/gradient_fill_cmds/source.rs index 6974904624..b45d29a441 100644 --- a/crates/engine-core/src/cmd/gradient_fill_cmds/source.rs +++ b/crates/engine-core/src/cmd/gradient_fill_cmds/source.rs @@ -67,7 +67,7 @@ pub fn rebake(mut edited: Fill, before: &Fill, original: &Fill) -> Fill { src.opacity_midpoints.clear(); } else { if opacity_stops.len() != src.opacity_stops.len() { - src.opacity_midpoints.clear(); + src.opacity_midpoints = kept_segment_mids(&src.opacity_stops, &src.opacity_midpoints, opacity_stops); } src.opacity_stops = opacity_stops.clone(); } @@ -94,6 +94,20 @@ pub fn rebake(mut edited: Fill, before: &Fill, original: &Fill) -> Fill { edited } +/// The midpoints of `new`'s segments after a stop was added or deleted: a segment between the +/// same two locations as one of `old`'s keeps its midpoint, a split or merged one starts halfway. +fn kept_segment_mids(old: &[(f32, f32)], old_mids: &[f32], new: &[(f32, f32)]) -> Vec { + let sorted = |s: &[(f32, f32)]| { + let mut t: Vec = s.iter().map(|s| s.0).collect(); + t.sort_by(f32::total_cmp); + t + }; + let (old, new) = (sorted(old), sorted(new)); + let mids: Vec = + new.windows(2).map(|w| old.windows(2).position(|o| o[0] == w[0] && o[1] == w[1]).and_then(|k| old_mids.get(k).copied()).unwrap_or(0.5)).collect(); + if mids.iter().all(|m| (m - 0.5).abs() < 1e-6) { Vec::new() } else { mids } +} + /// `gradient.fill.set`'s `method` and `smoothness` (Photoshop's interpolation) on a view: they /// start a record when the fill has none (its stops and midpoints as they are). pub fn apply_interpolation_params(cmd: &str, view: &mut Fill, p: &Value) -> Result<()> { diff --git a/crates/engine-core/src/cmd/gradient_fill_cmds/tests.rs b/crates/engine-core/src/cmd/gradient_fill_cmds/tests.rs index 2ab5f3aaee..15bc5d9727 100644 --- a/crates/engine-core/src/cmd/gradient_fill_cmds/tests.rs +++ b/crates/engine-core/src/cmd/gradient_fill_cmds/tests.rs @@ -432,3 +432,23 @@ fn dropping_a_kept_gradient_keeps_its_opacity_midpoints() { assert!((at(0.2) - 0.5).abs() < 0.02, "half way at the 20 % midpoint: {}", at(0.2)); assert!((at(0.0) - 1.0).abs() < 1e-3 && at(1.0).abs() < 1e-3); } + +/// CodeRabbit on #18551: adding an opacity stop to a kept gradient starts only the split +/// segment halfway; the other segments keep their opacity midpoints. +#[test] +fn adding_an_opacity_stop_keeps_the_other_segments_midpoints() { + let mut s = session(8, "white"); + s.execute(CREATE, json!({"from": [0, 16], "to": [48, 16]})).unwrap(); + let mut f = photoshop_fill(); + if let Fill::Gradient { opacity_stops, interpolation: Some(src), .. } = &mut f { + src.opacity_stops = vec![(0.0, 1.0), (0.5, 0.2), (1.0, 1.0)]; + src.opacity_midpoints = vec![0.2, 0.8]; + *opacity_stops = photocraft_io::gradient_bake::bake_interpolation_opacity(src).unwrap(); + } + set_active_fill(&mut s, f); + s.execute(STOP, json!({"kind": "opacity", "action": "add", "location": 0.75, "opacity": 50})).unwrap(); + let Fill::Gradient { interpolation: Some(src), .. } = active_fill(&s) else { panic!() }; + assert_eq!(src.opacity_stops.len(), 4); + assert!(src.opacity_midpoints.first().is_some_and(|m| (m - 0.2).abs() < 1e-6), "untouched segment: {:?}", src.opacity_midpoints); + assert!(src.opacity_midpoints.get(1).is_none_or(|m| (m - 0.5).abs() < 1e-6), "split segment starts halfway: {:?}", src.opacity_midpoints); +}