Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Paint Bucket and Gradient keep alpha on a transparency-locked layer (#1104) (#1193)

prekabreki0423f74b5068
diff --git a/crates/algo/src/paint.rs b/crates/algo/src/paint.rs
index 2e4def328..489e11f67 100644
--- a/crates/algo/src/paint.rs
+++ b/crates/algo/src/paint.rs
@@ -124,6 +124,7 @@ pub fn paint_gradient(
blend_mode: BlendMode,
dither: bool,
selection: Option<&Surface>,
+ lock_transparency: bool,
) {
composite_area(
s,
@@ -142,7 +143,7 @@ pub fn paint_gradient(
}
c
},
- false,
+ lock_transparency,
);
}
@@ -159,8 +160,9 @@ pub fn bucket_fill(
color: [f32; 4],
opacity: f32,
selection: Option<&Surface>,
+ lock_transparency: bool,
) -> bool {
- bucket_fill_src(s, area, seed, tolerance, contiguous, anti_alias, opacity, selection, |_, _| color)
+ bucket_fill_src(s, area, seed, tolerance, contiguous, anti_alias, opacity, selection, lock_transparency, |_, _| color)
}
/// Like [`bucket_fill`] but the fill colour comes from `src(x, y)` — e.g. a pattern sampled at the
@@ -175,6 +177,7 @@ pub fn bucket_fill_src(
anti_alias: bool,
opacity: f32,
selection: Option<&Surface>,
+ lock_transparency: bool,
src: impl Fn(i32, i32) -> [f32; 4] + Sync,
) -> bool {
if !area.contains(seed.0, seed.1) {
@@ -185,7 +188,7 @@ pub fn bucket_fill_src(
drop(img);
// Composite only over the filled region's box.
let b = region.bbox;
- composite_area(s, b, BlendMode::Normal, |x, y| region.at(x, y) * opacity * selection.map_or(1.0, |m| m.sample_channel(x, y, 0)), src, false);
+ composite_area(s, b, BlendMode::Normal, |x, y| region.at(x, y) * opacity * selection.map_or(1.0, |m| m.sample_channel(x, y, 0)), src, lock_transparency);
true
}
@@ -223,6 +226,7 @@ mod tests {
BlendMode::Normal,
false,
Some(&sel),
+ false,
);
assert!(s.pixel(0, 0)[0] < 0.01 && s.pixel(10, 0)[0] > 0.99);
assert!((s.pixel(5, 0)[0] - 0.5).abs() < 0.01);
@@ -235,9 +239,9 @@ mod tests {
let a = Rect::new(0, 0, 10, 10);
s.fill_rect(a, &[1.0, 1.0, 1.0, 1.0]);
s.fill_rect(Rect::new(5, 0, 6, 10), &[0.0, 0.0, 0.0, 1.0]);
- assert!(bucket_fill(&mut s, a, (1, 1), 10.0, true, false, [1.0, 0.0, 0.0, 1.0], 1.0, None));
+ assert!(bucket_fill(&mut s, a, (1, 1), 10.0, true, false, [1.0, 0.0, 0.0, 1.0], 1.0, None, false));
assert_eq!(s.pixel(2, 2), vec![1.0, 0.0, 0.0, 1.0]);
assert_eq!(s.pixel(8, 2), vec![1.0, 1.0, 1.0, 1.0]);
- assert!(!bucket_fill(&mut s, a, (50, 1), 10.0, true, false, [1.0, 0.0, 0.0, 1.0], 1.0, None));
+ assert!(!bucket_fill(&mut s, a, (50, 1), 10.0, true, false, [1.0, 0.0, 0.0, 1.0], 1.0, None, false));
}
}
diff --git a/crates/engine/src/paint_cmds.rs b/crates/engine/src/paint_cmds.rs
index 783753bdf..52918bd91 100644
--- a/crates/engine/src/paint_cmds.rs
+++ b/crates/engine/src/paint_cmds.rs
@@ -54,17 +54,17 @@ fn bucket(s: &mut Session, p: &Value) -> Result<Value> {
let filled = s.edit("Paint Bucket", |doc, active| {
let area = doc.bounds();
let sel = doc.selection.clone();
- let (surf, _) = crate::channel_cmds::target_surface(doc, *active, p)?;
+ let (surf, lock) = crate::channel_cmds::target_surface(doc, *active, p)?;
let ok = if let Some((tile, scale, angle, phase)) = &pattern {
// Render the pattern over the canvas once, then sample it at each filled pixel.
let place = photocraft_compose::pattern::Placement::new(photocraft_geom::Rect::EMPTY, false, *phase, *scale, *angle);
let rendered = photocraft_compose::pattern::render(tile, &place, area);
let w = area.width() as usize;
- bucket_fill_src(surf, area, (x, y), tol, contiguous, aa, opacity, sel.as_ref(), |px, py| {
+ bucket_fill_src(surf, area, (x, y), tol, contiguous, aa, opacity, sel.as_ref(), lock, |px, py| {
rendered[(py - area.y0) as usize * w + (px - area.x0) as usize]
})
} else {
- bucket_fill(surf, area, (x, y), tol, contiguous, aa, c, opacity, sel.as_ref())
+ bucket_fill(surf, area, (x, y), tol, contiguous, aa, c, opacity, sel.as_ref(), lock)
};
surf.prune();
Ok(ok)
@@ -102,8 +102,8 @@ fn gradient(s: &mut Session, p: &Value) -> Result<Value> {
s.edit("Gradient", |doc, active| {
let sel = doc.selection.clone();
let area = sel.as_ref().map(|m| m.content_bounds()).filter(|r| !r.is_empty()).unwrap_or_else(|| doc.bounds()).intersect(&doc.bounds());
- let (surf, _) = crate::channel_cmds::target_surface(doc, *active, p)?;
- paint_gradient(surf, area, from, to, shape, &stops, reverse, opacity, blend, dither, sel.as_ref());
+ let (surf, lock) = crate::channel_cmds::target_surface(doc, *active, p)?;
+ paint_gradient(surf, area, from, to, shape, &stops, reverse, opacity, blend, dither, sel.as_ref(), lock);
Ok(())
})?;
Ok(Value::Null)
diff --git a/crates/engine/tests/fill_lock_transparency.rs b/crates/engine/tests/fill_lock_transparency.rs
new file mode 100644
index 000000000..f7e4520bf
--- /dev/null
+++ b/crates/engine/tests/fill_lock_transparency.rs
@@ -0,0 +1,38 @@
+//! Paint Bucket and Gradient keep alpha on a transparency-locked layer, as Edit › Fill and the
+//! brushes do (#1104).
+
+use photocraft_engine::Session;
+use serde_json::json;
+
+/// A transparent layer with a red stroke across it, transparency locked.
+fn locked() -> Session {
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 40, "height": 30})).unwrap();
+ s.execute("layer.new.layer", json!({})).unwrap();
+ s.execute("paint.stroke", json!({"points": [[5, 10], [20, 10]], "size": 8, "hardness": 1.0, "color": "#ff0000"})).unwrap();
+ s.execute("layer.lockLayers", json!({"transparency": true})).unwrap();
+ s
+}
+
+fn px(s: &Session, x: i32, y: i32) -> [f32; 4] {
+ let st = s.active().unwrap();
+ st.active_layer.and_then(|id| st.doc.layer(id)).unwrap().surface().unwrap().rgba(x, y)
+}
+
+#[test]
+fn bucket_keeps_alpha() {
+ let mut s = locked();
+ s.execute("paint.bucket", json!({"x": 30, "y": 25, "color": "#0000ff", "antiAlias": false})).unwrap();
+ assert_eq!(px(&s, 30, 25)[3], 0.0, "transparent pixel became opaque");
+ s.execute("paint.bucket", json!({"x": 12, "y": 10, "color": "#0000ff", "antiAlias": false})).unwrap();
+ assert_eq!(px(&s, 12, 10), [0.0, 0.0, 1.0, 1.0], "the opaque stroke is still recoloured");
+}
+
+#[test]
+fn gradient_keeps_alpha() {
+ let mut s = locked();
+ s.execute("paint.gradient", json!({"from": [0, 0], "to": [40, 30]})).unwrap();
+ assert_eq!(px(&s, 30, 25)[3], 0.0, "transparent pixel became opaque");
+ assert_eq!(px(&s, 12, 10)[3], 1.0);
+ assert_ne!(px(&s, 12, 10), [1.0, 0.0, 0.0, 1.0], "the opaque stroke is still painted");
+}