Skip to content

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

Channels: keep RGB off while an alpha or mask is shown, and select from that channel. (#2555)

Desayuno64cb9513c71856
diff --git a/crates/engine/src/channel_cmds.rs b/crates/engine/src/channel_cmds.rs
index 8f263c058..c1e3316ab 100644
--- a/crates/engine/src/channel_cmds.rs
+++ b/crates/engine/src/channel_cmds.rs
@@ -113,19 +113,29 @@ impl Default for QuickMaskOptions {
pub(crate) fn fix_view(st: &mut DocState) {
let n = st.doc.channels.len();
let colors = color_count(&st.doc);
- let v = &mut st.channel_view;
- v.alpha_visible.truncate(n);
- v.color_hidden.truncate(colors);
- match v.target {
- ChannelTarget::Alpha(i) if i >= n => v.target = ChannelTarget::Composite,
- ChannelTarget::Color(k) if k >= colors || colors < 2 => v.target = ChannelTarget::Composite,
- _ => {}
- }
- // Never leave the canvas showing nothing at all.
- if v.visible_colors(colors) == 0 && !(0..n).any(|i| v.alpha_shown(i)) {
- v.color_hidden.clear();
+ let quick = st.doc.quick_mask.is_some();
+ {
+ let v = &mut st.channel_view;
+ v.alpha_visible.truncate(n);
+ v.color_hidden.truncate(colors);
+ match v.target {
+ ChannelTarget::Alpha(i) if i >= n => v.target = ChannelTarget::Composite,
+ ChannelTarget::Color(k) if k >= colors || colors < 2 => v.target = ChannelTarget::Composite,
+ _ => {}
+ }
}
crate::mask_view_cmds::fix(st);
+ // An alpha/spot eye, Quick Mask, or the active layer's mask can be the only thing on the
+ // canvas (so R, G and B can all be off). Otherwise the colour channels come back.
+ let active = st.active_layer;
+ let shown = {
+ let doc = &st.doc;
+ let v = &st.channel_view;
+ (0..n).any(|i| v.alpha_shown(i)) || (quick && !v.quick_mask_hidden) || active.is_some_and(|id| doc.layer(id).is_some_and(|l| l.mask.is_some()))
+ };
+ if st.channel_view.visible_colors(colors) == 0 && !shown {
+ st.channel_view.color_hidden.clear();
+ }
}
/// Bump the revision for a view-only change without dirtying a clean document.
diff --git a/crates/engine/src/channel_cmds/tests.rs b/crates/engine/src/channel_cmds/tests.rs
index 9a9b710b3..1ba0febfa 100644
--- a/crates/engine/src/channel_cmds/tests.rs
+++ b/crates/engine/src/channel_cmds/tests.rs
@@ -168,6 +168,49 @@ fn channel_management_and_view_state() {
assert_eq!(doc(&s).channels.len(), 3);
}
+#[test]
+fn every_colour_channel_can_hide_while_alpha_stays() {
+ let mut s = session();
+ s.execute("channel.new", json!({"fill": "white", "name": "Alpha"})).unwrap();
+ // A new channel is shown alone. Bring RGB back (the overlay), then turn the RGB eye off.
+ s.execute("channel.setVisible", json!({"channel": "composite", "visible": true})).unwrap();
+ assert!(s.active().unwrap().channel_view.alpha_shown(0));
+ assert_eq!(s.active().unwrap().channel_view.visible_colors(3), 3);
+ s.execute("channel.setVisible", json!({"channel": "composite", "visible": false})).unwrap();
+ let st = s.active().unwrap();
+ assert_eq!(st.channel_view.visible_colors(3), 0, "the RGB eye can turn R, G and B off together");
+ assert!(st.channel_view.alpha_shown(0), "Alpha stays on");
+ s.execute("channel.setVisible", json!({"channel": "composite", "visible": true})).unwrap();
+ for name in ["red", "green", "blue"] {
+ s.execute("channel.setVisible", json!({"channel": name, "visible": false})).unwrap();
+ }
+ let st = s.active().unwrap();
+ assert_eq!(st.channel_view.visible_colors(3), 0, "R, G and B can each be turned off");
+ assert!(st.channel_view.alpha_shown(0));
+}
+
+#[test]
+fn quick_mask_and_a_layer_mask_can_be_the_only_visible_channel() {
+ let mut s = session();
+ s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap();
+ s.execute("select.editInQuickMaskMode", json!({})).unwrap();
+ s.execute("channel.setVisible", json!({"channel": "composite", "visible": false})).unwrap();
+ assert_eq!(s.active().unwrap().channel_view.visible_colors(3), 0);
+ assert!(s.active().unwrap().doc.quick_mask.is_some());
+ s.execute("select.editInQuickMaskMode", json!({"on": false})).unwrap();
+ // Nothing else to show: the colour channels come back.
+ s.execute("channel.setVisible", json!({"channel": "composite", "visible": false})).unwrap();
+ assert_eq!(s.active().unwrap().channel_view.visible_colors(3), 3);
+
+ s.execute("layer.new.layer", json!({})).unwrap();
+ s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap();
+ s.execute("layer.layerMask.hideAll", json!({})).unwrap();
+ for name in ["red", "green", "blue"] {
+ s.execute("channel.setVisible", json!({"channel": name, "visible": false})).unwrap();
+ }
+ assert_eq!(s.active().unwrap().channel_view.visible_colors(3), 0, "a transparency mask can be shown alone");
+}
+
#[test]
fn shortcut_slots_target_channels() {
let mut s = session();
diff --git a/crates/engine/src/mask_view_cmds.rs b/crates/engine/src/mask_view_cmds.rs
index 055557480..d7cfc338c 100644
--- a/crates/engine/src/mask_view_cmds.rs
+++ b/crates/engine/src/mask_view_cmds.rs
@@ -112,6 +112,8 @@ fn set_view(s: &mut Session, p: &Value) -> Result<Value> {
let applies = next.is_some() || st.channel_view.layer_mask.is_some_and(|v| v.layer == layer);
if applies && st.channel_view.layer_mask != next {
st.channel_view.layer_mask = next;
+ // Hiding the mask while every colour channel is off would leave a blank canvas.
+ crate::channel_cmds::fix_view(st);
// A view change: redraw without recompositing, and keep a clean document clean.
let clean = st.saved_revision == st.revision;
st.revision += 1;
diff --git a/crates/engine/src/selection_cmds.rs b/crates/engine/src/selection_cmds.rs
index 3a677f522..972004170 100644
--- a/crates/engine/src/selection_cmds.rs
+++ b/crates/engine/src/selection_cmds.rs
@@ -55,6 +55,34 @@ fn current_mask(doc: &Document) -> (Rect, Vec<f32>) {
(area, sel::mask_from_surface(doc.selection.as_ref(), area))
}
+/// Grayscale the canvas is actually showing, as opaque RGBA, when that is not the colour
+/// composite: a gray layer-mask view, or — with every colour channel hidden — the first
+/// visible alpha, Quick Mask, or the active layer's mask. The Magic Wand selects from this
+/// so a white alpha stays one region instead of breaking up along the RGB underneath.
+fn shown_gray(s: &Session) -> Option<(Rect, Vec<[u8; 4]>)> {
+ let st = s.active()?;
+ let doc = &st.doc;
+ let area = doc.bounds();
+ let v = &st.channel_view;
+ let colors_off = v.visible_colors(crate::channel_cmds::color_count(doc)) == 0;
+ let surf = if let Some((mask, crate::mask_view_cmds::MaskViewMode::Gray)) = v.shown_layer_mask(doc) {
+ &mask.surface
+ } else if colors_off {
+ if let Some(i) = (0..doc.channels.len()).find(|i| v.alpha_shown(*i)) {
+ &doc.channels[i].surface
+ } else if let Some(q) = doc.quick_mask.as_ref().filter(|_| !v.quick_mask_hidden) {
+ &q.surface
+ } else if let Some((mask, _)) = v.shown_layer_mask(doc) {
+ &mask.surface
+ } else {
+ &st.active_layer.and_then(|id| doc.layer(id))?.mask.as_ref()?.surface
+ }
+ } else {
+ return None;
+ };
+ Some((area, sel::rgba8_image(surf, area)))
+}
+
/// 8-bit RGBA of the active layer (or the composite with `all_layers`) over the canvas.
pub(crate) fn sample_rgba8(s: &Session, all_layers: bool) -> Result<(Rect, Vec<[u8; 4]>)> {
let d = s.active().ok_or(EngineError::NoDocument)?;
@@ -71,7 +99,10 @@ pub(crate) fn sample_rgba8(s: &Session, all_layers: bool) -> Result<(Rect, Vec<[
fn magic_wand(s: &mut Session, p: &Value) -> Result<Value> {
let (x, y) = (f(p, "x", 0.0).floor() as i32, f(p, "y", 0.0).floor() as i32);
- let (area, img) = sample_rgba8(s, b(p, "sampleAllLayers", false))?;
+ let (area, img) = match shown_gray(s) {
+ Some(plane) => plane,
+ None => sample_rgba8(s, b(p, "sampleAllLayers", false))?,
+ };
let region = sel::wand_region(&img, area, (x, y), f(p, "tolerance", 32.0), b(p, "contiguous", true), b(p, "antiAlias", true));
drop(img);
let m = mode(p);
@@ -547,7 +578,7 @@ pub fn specs() -> Vec<CommandSpec> {
"select.magicWand",
"Magic Wand",
[],
- r##"{"x":px,"y":px,"tolerance":0..255=32,"contiguous":bool=true,"antiAlias":bool=true,"sampleAllLayers":bool=false,"mode":"replace|add|subtract|intersect"="replace"}"##,
+ r##"{"x":px,"y":px,"tolerance":0..255=32,"contiguous":bool=true,"antiAlias":bool=true,"sampleAllLayers":bool=false,"mode":"replace|add|subtract|intersect"="replace"} (samples the visible alpha, Quick Mask, or layer mask when that is the picture)"##,
has_doc,
magic_wand
),
@@ -618,6 +649,45 @@ mod tests {
s.active().unwrap().doc.selection.as_ref().map_or(0.0, |m| m.sample_channel(x, y, 0))
}
+ #[test]
+ fn magic_wand_selects_the_visible_alpha_or_mask_not_the_rgb() {
+ let mut s = session();
+ // Alpha covers the first red square and the pixels beside it. RGB would stop at the red.
+ s.execute("select.rect", json!({"x": 0, "y": 0, "width": 20, "height": 20})).unwrap();
+ s.execute("channel.new", json!({"fill": "selection", "name": "Alpha"})).unwrap();
+ s.execute("select.magicWand", json!({"x": 7, "y": 7, "tolerance": 0, "antiAlias": false})).unwrap();
+ assert_eq!(coverage(&s, 7, 7), 1.0);
+ assert_eq!(coverage(&s, 2, 2), 1.0, "the white alpha includes pixels that are not the red square");
+ assert_eq!(coverage(&s, 30, 10), 0.0, "outside the alpha stays unselected");
+ // Colour channels back on: the wand reads RGB again.
+ s.execute("channel.setVisible", json!({"channel": "composite", "visible": true})).unwrap();
+ s.execute("channel.setVisible", json!({"channel": 0, "visible": false})).unwrap();
+ s.execute("select.magicWand", json!({"x": 7, "y": 7, "tolerance": 0, "antiAlias": false})).unwrap();
+ assert_eq!(coverage(&s, 7, 7), 1.0);
+ assert_eq!(coverage(&s, 2, 2), 0.0, "RGB sampling stops at the red square");
+ // Eyes off again, Alpha still the picture.
+ s.execute("channel.setVisible", json!({"channel": 0, "visible": true})).unwrap();
+ s.execute("channel.setVisible", json!({"channel": "composite", "visible": false})).unwrap();
+ s.execute("select.magicWand", json!({"x": 7, "y": 7, "tolerance": 0, "antiAlias": false})).unwrap();
+ assert_eq!(coverage(&s, 2, 2), 1.0);
+
+ let mut s = session();
+ s.execute("select.rect", json!({"x": 0, "y": 0, "width": 20, "height": 20})).unwrap();
+ s.execute("layer.layerMask.revealSelection", json!({})).unwrap();
+ s.execute("select.deselect", json!({})).unwrap();
+ s.execute("view.layerMask", json!({"mode": "gray"})).unwrap();
+ s.execute("select.magicWand", json!({"x": 7, "y": 7, "tolerance": 0, "antiAlias": false})).unwrap();
+ assert_eq!(coverage(&s, 2, 2), 1.0, "gray mask view selects the mask");
+ assert_eq!(coverage(&s, 30, 10), 0.0);
+ s.execute("view.layerMask", json!({"mode": "off"})).unwrap();
+ for name in ["red", "green", "blue"] {
+ s.execute("channel.setVisible", json!({"channel": name, "visible": false})).unwrap();
+ }
+ s.execute("select.magicWand", json!({"x": 7, "y": 7, "tolerance": 0, "antiAlias": false})).unwrap();
+ assert_eq!(coverage(&s, 2, 2), 1.0, "a layer mask shown alone is what the wand reads");
+ assert_eq!(coverage(&s, 30, 10), 0.0);
+ }
+
#[test]
fn magic_wand_contiguous_and_modes() {
let mut s = session();
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 7c96f2a31..ee3e171c5 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -5765,7 +5765,15 @@ mod tests {
let f = factor as i32;
let r = DRect::new(pos[0] as i32 * f, pos[1] as i32 * f, (pos[0] + image.size[0]) as i32 * f, (pos[1] + image.size[1]) as i32 * f)
.intersect(&shown.bounds());
- let expected = crate::channel_view::render(&shown, &app.session.documents()[0].channel_view, r, factor, false).unwrap();
+ let expected = crate::channel_view::render(
+ &shown,
+ &app.session.documents()[0].channel_view,
+ r,
+ factor,
+ false,
+ app.session.documents()[0].active_layer,
+ )
+ .unwrap();
assert_eq!(image.pixels, expected, "uploaded mask values match the live stroke");
delta.clear();
assert_eq!(app.session.documents()[0].revision, revision);
@@ -5793,7 +5801,7 @@ mod tests {
let st = &app.session.documents()[0];
assert_eq!(
image.pixels,
- crate::channel_view::render(&st.doc, &st.channel_view, st.doc.bounds(), factor, false).unwrap(),
+ crate::channel_view::render(&st.doc, &st.channel_view, st.doc.bounds(), factor, false, st.active_layer).unwrap(),
"undo restores the shown mask"
);
delta.clear();
@@ -5836,10 +5844,14 @@ mod tests {
let pos = upload.pos.expect("partial channel upload");
let egui::ImageData::Color(image) = &upload.image;
let r = DRect::new(pos[0] as i32, pos[1] as i32, (pos[0] + image.size[0]) as i32, (pos[1] + image.size[1]) as i32);
- assert_eq!(image.pixels, crate::channel_view::render(&shown, &app.session.documents()[0].channel_view, r, 1, false).unwrap(), "{target}");
+ assert_eq!(
+ image.pixels,
+ crate::channel_view::render(&shown, &app.session.documents()[0].channel_view, r, 1, false, app.session.documents()[0].active_layer).unwrap(),
+ "{target}"
+ );
assert_ne!(
image.pixels,
- crate::channel_view::render(&original, &app.session.documents()[0].channel_view, r, 1, false).unwrap(),
+ crate::channel_view::render(&original, &app.session.documents()[0].channel_view, r, 1, false, app.session.documents()[0].active_layer).unwrap(),
"target was painted"
);
delta.clear();
@@ -5854,7 +5866,18 @@ mod tests {
let upload = &delta.set.get(&tex).unwrap()[0];
assert!(upload.pos.is_none(), "discarding a preview refreshes the view");
let egui::ImageData::Color(image) = &upload.image;
- assert_eq!(image.pixels, crate::channel_view::render(&original, &app.session.documents()[0].channel_view, original.bounds(), 1, false).unwrap());
+ assert_eq!(
+ image.pixels,
+ crate::channel_view::render(
+ &original,
+ &app.session.documents()[0].channel_view,
+ original.bounds(),
+ 1,
+ false,
+ app.session.documents()[0].active_layer
+ )
+ .unwrap()
+ );
delta.clear();
}
}
diff --git a/crates/ui-egui/src/channel_view.rs b/crates/ui-egui/src/channel_view.rs
index 520994e0b..783a6313b 100644
--- a/crates/ui-egui/src/channel_view.rs
+++ b/crates/ui-egui/src/channel_view.rs
@@ -67,7 +67,7 @@ fn composite(doc: &Document, r: Rect, factor: u32) -> Vec<[f32; 4]> {
/// The channel view of `r` (document pixels, sampled every `factor` px) as premultiplied pixels,
/// or `None` when the plain composite is all there is to show. `in_color` shows single colour
/// channels tinted instead of grayscale (Photoshop's "Show Channels in Color").
-pub fn render(doc: &Document, v: &ChannelView, r: Rect, factor: u32, in_color: bool) -> Option<Vec<Color32>> {
+pub fn render(doc: &Document, v: &ChannelView, r: Rect, factor: u32, in_color: bool, active: Option<photocraft_doc::LayerId>) -> Option<Vec<Color32>> {
if v.is_plain(doc) || r.is_empty() {
return None;
}
@@ -92,11 +92,26 @@ pub fn render(doc: &Document, v: &ChannelView, r: Rect, factor: u32, in_color: b
let (w, h) = (r.width().div_ceil(factor), r.height().div_ceil(factor));
let n = (w * h) as usize;
let mut overlays: Vec<&AlphaChannel> = (0..doc.channels.len()).filter(|i| v.alpha_shown(*i)).map(|i| &doc.channels[i]).collect();
- // Premultiplied RGBA accumulator.
+ let gray = |s: &Surface| sample(s, r, factor).into_iter().map(|g| [g, g, g, 1.0]).collect();
+ // Premultiplied RGBA accumulator. With no colour channel left, the channel being edited
+ // (alpha, Quick Mask, or layer mask) is the picture — grayscale, not a rubylith over nothing.
+ let mut solo_quick = false;
+ let mut solo_mask = false;
let mut out: Vec<[f32; 4]> = if vis == 0 {
- // No colour channel visible: the first visible alpha channel in grayscale.
- let first = (!overlays.is_empty()).then(|| overlays.remove(0))?;
- sample(&first.surface, r, factor).into_iter().map(|g| [g, g, g, 1.0]).collect()
+ if !overlays.is_empty() {
+ let first = overlays.remove(0);
+ gray(&first.surface)
+ } else if let Some(q) = doc.quick_mask.as_ref().filter(|_| !v.quick_mask_hidden) {
+ solo_quick = true;
+ gray(&q.surface)
+ } else if let Some((m, _)) = layer_mask {
+ solo_mask = true;
+ gray(&m.surface)
+ } else {
+ let m = active.and_then(|id| doc.layer(id)).and_then(|l| l.mask.as_ref())?;
+ solo_mask = true;
+ gray(&m.surface)
+ }
} else if vis < colors {
let fmt = doc.pixel_format();
let mode = fmt.mode;
@@ -137,7 +152,7 @@ pub fn render(doc: &Document, v: &ChannelView, r: Rect, factor: u32, in_color: b
o[3] = a + o[3] * (1.0 - a);
}
};
- let quick = doc.quick_mask.as_ref().filter(|_| !v.quick_mask_hidden);
+ let quick = doc.quick_mask.as_ref().filter(|_| !v.quick_mask_hidden && !solo_quick);
for ch in overlays.into_iter().chain(quick) {
let (color, opacity) = match ch.spot {
// Spot inks show more solid as solidity rises.
@@ -148,7 +163,7 @@ pub fn render(doc: &Document, v: &ChannelView, r: Rect, factor: u32, in_color: b
}
// ⇧⌥-click mask view (#196): the layer mask as Quick Mask's red over the hidden areas
// (Photoshop's Layer Mask Display Options default).
- if let Some((m, MaskViewMode::Overlay)) = layer_mask {
+ if !solo_mask && let Some((m, MaskViewMode::Overlay)) = layer_mask {
tint(sample(&m.surface, r, factor), AlphaChannel::DEFAULT_COLOR, 0.5, &|x| 1.0 - x);
}
Some(out.into_iter().map(|p| Color32::from_rgba_premultiplied(q(p[0]), q(p[1]), q(p[2]), q(p[3]))).collect())
@@ -195,6 +210,7 @@ fn view_key(v: &ChannelView, in_color: bool) -> u64 {
pub fn ensure(app: &mut PhotocraftApp, ctx: &egui::Context, idx: usize) -> Option<egui::TextureId> {
let st = app.session.documents().get(idx)?;
let view = st.channel_view.clone();
+ let active = st.active_layer;
let (revision, last_damage) = (st.revision, st.last_damage);
let id = st.doc.id.0;
if view.is_plain(&st.doc) {
@@ -238,7 +254,7 @@ pub fn ensure(app: &mut PhotocraftApp, ctx: &egui::Context, idx: usize) -> Optio
let d = d.intersect(&doc.bounds());
let r = Rect::new(d.x0.div_euclid(f) * f, d.y0.div_euclid(f) * f, d.x1, d.y1).intersect(&doc.bounds());
if !r.is_empty()
- && let Some(px) = render(&doc, &view, r, factor, in_color)
+ && let Some(px) = render(&doc, &view, r, factor, in_color, active)
{
let img = egui::ColorImage::new([r.width().div_ceil(factor) as usize, r.height().div_ceil(factor) as usize], px);
c.tex.set_partial([(r.x0 / f) as usize, (r.y0 / f) as usize], img, TextureOptions::LINEAR);
@@ -248,7 +264,7 @@ pub fn ensure(app: &mut PhotocraftApp, ctx: &egui::Context, idx: usize) -> Optio
return Some(c.tex.id());
}
let b = doc.bounds();
- let px = render(&doc, &view, b, factor, in_color)?;
+ let px = render(&doc, &view, b, factor, in_color, active)?;
let size = [b.width().div_ceil(factor) as usize, b.height().div_ceil(factor) as usize];
let img = egui::ColorImage::new(size, px);
match app.channel_views.get_mut(&id) {
@@ -285,7 +301,7 @@ mod tests {
fn plain_composite_draws_nothing() {
let s = session();
let st = s.active().unwrap();
- assert!(render(&st.doc, &st.channel_view, st.doc.bounds(), 1, false).is_none());
+ assert!(render(&st.doc, &st.channel_view, st.doc.bounds(), 1, false, st.active_layer).is_none());
}
#[test]
@@ -294,7 +310,7 @@ mod tests {
s.execute("select.rect", json!({"x": 0, "y": 0, "width": 4, "height": 4})).unwrap();
s.execute("select.editInQuickMaskMode", json!({})).unwrap();
let st = s.active().unwrap();
- let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, false).unwrap();
+ let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, false, st.active_layer).unwrap();
assert_eq!(px[0], Color32::TRANSPARENT, "selected: no overlay");
assert_eq!(px[6], Color32::from_rgba_premultiplied(128, 0, 0, 128), "masked: 50% red");
}
@@ -310,14 +326,14 @@ mod tests {
s.execute("view.layerMask", json!({"mode": "gray"})).unwrap();
let st = s.active().unwrap();
let b = st.doc.bounds();
- let full = render(&st.doc, &st.channel_view, b, 1, false).unwrap();
+ let full = render(&st.doc, &st.channel_view, b, 1, false, st.active_layer).unwrap();
assert!(full[0].r() < 10 && full[36].r() > 245 && full[0].a() == 255, "opaque grayscale ramp");
// A sub-rect starting on the factor grid matches the same pixels of the full render.
for f in [1u32, 2, 3] {
- let all = render(&st.doc, &st.channel_view, b, f, false).unwrap();
+ let all = render(&st.doc, &st.channel_view, b, f, false, st.active_layer).unwrap();
let w = b.width().div_ceil(f) as usize;
let r = Rect::new(3 * f as i32, 2 * f as i32, 30, 19);
- let part = render(&st.doc, &st.channel_view, r, f, false).unwrap();
+ let part = render(&st.doc, &st.channel_view, r, f, false, st.active_layer).unwrap();
let pw = r.width().div_ceil(f) as usize;
for (i, p) in part.iter().enumerate() {
assert_eq!(*p, all[(i / pw + 2) * w + i % pw + 3], "factor {f}, pixel {i}");
@@ -325,7 +341,7 @@ mod tests {
}
s.execute("view.layerMask", json!({"mode": "overlay"})).unwrap();
let st = s.active().unwrap();
- let px = render(&st.doc, &st.channel_view, b, 1, false).unwrap();
+ let px = render(&st.doc, &st.channel_view, b, 1, false, st.active_layer).unwrap();
assert!(px[0].r() >= 124 && px[0].g() == 0 && px[0].a() >= 124, "about 50% red over the hidden end: {:?}", px[0]);
assert!(px[36].a() <= 4, "next to nothing over the revealed end: {:?}", px[36]);
}
@@ -347,7 +363,7 @@ mod tests {
let mut best = f64::MAX;
for _ in 0..8 {
let t = std::time::Instant::now();
- let px = render(&st.doc, &st.channel_view, st.doc.bounds(), factor, false).unwrap();
+ let px = render(&st.doc, &st.channel_view, st.doc.bounds(), factor, false, st.active_layer).unwrap();
let img = egui::ColorImage::new([3000, 2000], px);
std::hint::black_box(img);
best = best.min(t.elapsed().as_secs_f64() * 1000.0);
@@ -366,17 +382,17 @@ mod tests {
s.execute("channel.target", json!({"channel": "green"})).unwrap();
let st = s.active().unwrap();
let b = st.doc.bounds();
- assert_eq!(render(&st.doc, &st.channel_view, b, 1, false).unwrap()[0], Color32::from_gray(128));
- assert_eq!(render(&st.doc, &st.channel_view, b, 1, true).unwrap()[0], Color32::from_rgb(0, 128, 0));
+ assert_eq!(render(&st.doc, &st.channel_view, b, 1, false, st.active_layer).unwrap()[0], Color32::from_gray(128));
+ assert_eq!(render(&st.doc, &st.channel_view, b, 1, true, st.active_layer).unwrap()[0], Color32::from_rgb(0, 128, 0));
let mut s = Session::new();
s.execute("file.new", json!({"width": 4, "height": 4, "mode": "cmyk"})).unwrap();
s.execute("edit.fill", json!({"color": "#4080c0"})).unwrap();
s.execute("channel.target", json!({"channel": "cyan"})).unwrap();
let st = s.active().unwrap();
- let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, true).unwrap()[0];
+ let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, true, st.active_layer).unwrap()[0];
assert!(px.r() < 100 && px.g() > 150 && px.b() > 150, "cyan ink, not gray: {px:?}");
- let gray = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, false).unwrap()[0];
+ let gray = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, false, st.active_layer).unwrap()[0];
assert_eq!(gray.r(), gray.g(), "off stays grayscale");
}
@@ -389,12 +405,12 @@ mod tests {
let b = s.active().unwrap().doc.bounds();
s.execute("channel.target", json!({"channel": {"color": 0}})).unwrap();
let st = s.active().unwrap();
- let px = render(&st.doc, &st.channel_view, b, 1, true).unwrap()[0];
+ let px = render(&st.doc, &st.channel_view, b, 1, true, st.active_layer).unwrap()[0];
let spread = px.r().max(px.g().max(px.b())) as i32 - px.r().min(px.g().min(px.b())) as i32;
assert!(spread <= 3, "Lightness stays gray: {px:?}");
s.execute("channel.target", json!({"channel": {"color": 1}})).unwrap();
let st = s.active().unwrap();
- let px = render(&st.doc, &st.channel_view, b, 1, true).unwrap()[0];
+ let px = render(&st.doc, &st.channel_view, b, 1, true, st.active_layer).unwrap()[0];
assert!(px.r() != px.g() || px.g() != px.b(), "a channel is tinted, not gray: {px:?}");
}
@@ -422,18 +438,48 @@ mod tests {
s.execute("channel.new", json!({"fill": "selection"})).unwrap();
let st = s.active().unwrap();
assert_eq!(st.channel_view.target, ChannelTarget::Alpha(0));
- let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, false).unwrap();
+ let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, false, st.active_layer).unwrap();
assert_eq!((px[0], px[5]), (Color32::WHITE, Color32::BLACK));
// Green alone: grayscale of the green channel; with blue hidden only.
s.execute("channel.target", json!({"channel": "green"})).unwrap();
let st = s.active().unwrap();
- let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, false).unwrap();
+ let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, false, st.active_layer).unwrap();
assert_eq!(px[0], Color32::from_gray(128));
s.execute("channel.target", json!({"channel": "composite"})).unwrap();
s.execute("channel.setVisible", json!({"channel": "red", "visible": false})).unwrap();
let st = s.active().unwrap();
- let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 2, false).unwrap();
+ let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 2, false, st.active_layer).unwrap();
assert_eq!(px.len(), 4 * 2);
assert_eq!(px[0], Color32::from_rgb(0, 128, 0));
}
+
+ #[test]
+ fn rgb_off_shows_alpha_quick_mask_or_the_layer_mask() {
+ let mut s = session();
+ s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap();
+ s.execute("channel.new", json!({"fill": "white"})).unwrap();
+ s.execute("channel.setVisible", json!({"channel": "composite", "visible": true})).unwrap();
+ s.execute("channel.setVisible", json!({"channel": "composite", "visible": false})).unwrap();
+ let st = s.active().unwrap();
+ let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, false, st.active_layer).unwrap();
+ assert_eq!(px[0], Color32::WHITE, "Alpha alone is the channel, not the red image");
+
+ let mut q = session();
+ q.execute("edit.fill", json!({"color": "#ff0000"})).unwrap();
+ q.execute("select.editInQuickMaskMode", json!({})).unwrap();
+ q.execute("channel.setVisible", json!({"channel": "composite", "visible": false})).unwrap();
+ let st = q.active().unwrap();
+ let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, false, st.active_layer).unwrap();
+ assert_eq!(px[0], Color32::WHITE, "Quick Mask alone is grayscale, not the red composite");
+
+ let mut m = session();
+ m.execute("layer.new.layer", json!({})).unwrap();
+ m.execute("edit.fill", json!({"color": "#ff0000"})).unwrap();
+ m.execute("layer.layerMask.hideAll", json!({})).unwrap();
+ m.execute("channel.setVisible", json!({"channel": "composite", "visible": false})).unwrap();
+ let st = m.active().unwrap();
+ assert_eq!(st.channel_view.visible_colors(3), 0);
+ let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, false, st.active_layer).unwrap();
+ assert_eq!(px[0], Color32::BLACK, "the hidden mask reads black, not the red layer");
+ }
}
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index d60e774f7..2b6e71e4d 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -1678,12 +1678,22 @@ impl PhotocraftApp {
}
}
- /// The Layers panel targets the active layer's mask, and no alpha channel or Quick Mask
- /// takes over (the engine routes those itself).
+ /// The active layer's mask is what pixel commands edit: the Layers panel targets it, or every
+ /// colour channel is off and that mask is the picture. An alpha channel or Quick Mask wins,
+ /// as the engine routes those itself.
pub fn layer_mask_targeted(&self) -> bool {
- let Some(st) = self.session.active().filter(|_| self.ui.mask_target) else { return false };
+ let Some(st) = self.session.active() else { return false };
let composite = st.channel_view.target == photocraft_engine::channel_cmds::ChannelTarget::Composite && st.doc.quick_mask.is_none();
- composite && st.active_layer.and_then(|id| st.doc.layer(id)).is_some_and(|l| l.mask.is_some())
+ let has_mask = st.active_layer.and_then(|id| st.doc.layer(id)).is_some_and(|l| l.mask.is_some());
+ if !composite || !has_mask {
+ return false;
+ }
+ if self.ui.mask_target {
+ return true;
+ }
+ let colors_off = st.channel_view.visible_colors(photocraft_engine::channel_cmds::color_count(&st.doc)) == 0;
+ let alpha_on = (0..st.doc.channels.len()).any(|i| st.channel_view.alpha_shown(i));
+ colors_off && !alpha_on
}
/// Viewing a layer mask (#196) targets it; a vector-mask target needs a vector mask on the
diff --git a/crates/ui-egui/src/mask_thumbs_ui/tests.rs b/crates/ui-egui/src/mask_thumbs_ui/tests.rs
index b7ef3845c..d6cebb379 100644
--- a/crates/ui-egui/src/mask_thumbs_ui/tests.rs
+++ b/crates/ui-egui/src/mask_thumbs_ui/tests.rs
@@ -333,7 +333,7 @@ fn mask_view(h: &Harness<'_, PhotocraftApp>) -> serde_json::Value {
fn canvas_px(h: &Harness<'_, PhotocraftApp>) -> Option<Vec<egui::Color32>> {
let st = h.state().session.active().unwrap();
- crate::channel_view::render(&st.doc, &st.channel_view, photocraft_geom::Rect::new(0, 0, 200, 100), 1, false)
+ crate::channel_view::render(&st.doc, &st.channel_view, photocraft_geom::Rect::new(0, 0, 200, 100), 1, false, st.active_layer)
}
fn near(a: Rect, b: Rect) -> bool {
diff --git a/crates/ui-egui/src/shortcut_dispatch.rs b/crates/ui-egui/src/shortcut_dispatch.rs
index 7191b366d..ee86520a1 100644
--- a/crates/ui-egui/src/shortcut_dispatch.rs
+++ b/crates/ui-egui/src/shortcut_dispatch.rs
@@ -264,7 +264,11 @@ fn delete_key_command(app: &PhotocraftApp, id: String) -> String {
}
let Some(st) = app.session.active() else { return id };
let composite = st.channel_view.target == photocraft_engine::channel_cmds::ChannelTarget::Composite && st.doc.quick_mask.is_none();
- let mask = app.ui.mask_target && st.active_layer.and_then(|l| st.doc.layer(l)).is_some_and(|l| l.mask.is_some());
+ let has_mask = st.active_layer.and_then(|l| st.doc.layer(l)).is_some_and(|l| l.mask.is_some());
+ let colors_off = st.channel_view.visible_colors(photocraft_engine::channel_cmds::color_count(&st.doc)) == 0;
+ let alpha_on = (0..st.doc.channels.len()).any(|i| st.channel_view.alpha_shown(i));
+ let solo_mask = colors_off && has_mask && !alpha_on && st.doc.quick_mask.is_none();
+ let mask = (app.ui.mask_target || solo_mask) && has_mask;
if mask && composite {
let cmd = if st.doc.selection.is_some() { "edit.fillBackground" } else { "layer.layerMask.delete" };
return cmd.into();