mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Batch fix-up: adding or deleting an opacity stop keeps the other segments' midpoints (#13855, CodeRabbit on #18551)
Only the split or merged segment of a kept gradient starts halfway; a segment between the same two stops as before keeps its opacity midpoint. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
a46bc09a93
commit
c9616de7bb
2 files changed
+35
-1
No files matched your search
@@ -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<f32> {
|
||||
let sorted = |s: &[(f32, f32)]| {
|
||||
let mut t: Vec<f32> = s.iter().map(|s| s.0).collect();
|
||||
t.sort_by(f32::total_cmp);
|
||||
t
|
||||
};
|
||||
let (old, new) = (sorted(old), sorted(new));
|
||||
let mids: Vec<f32> =
|
||||
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<()> {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
Reference in new issue
Block a user