Use Normal for unsupported gradient modes on RGB layer masks

This commit is contained in:
yoriswind committed 2026-12-01 02:36:14 -08:00
1 parent c5ca44cd2f
commit 099df3d4be
2 files changed
+88

No files matched your search

@@ -0,0 +1,74 @@
use photocraft_color::BlendMode;
use serde_json::{Value, json};
use crate::{Session, channel_cmds};
const MASK_LEVEL: f32 = 128.0 / 255.0;
fn seeded_surface(mode: &str, depth: u8, target: &Value) -> Session {
let mut s = crate::session();
s.execute("file.new", json!({"width": 40, "height": 8, "mode": mode, "depth": depth, "background": "transparent"})).unwrap();
match target.as_str() {
Some("mask") => s.execute("layer.layerMask.revealAll", json!({})).unwrap(),
Some("quickMask") => s.execute("select.editInQuickMaskMode", json!({"on": true})).unwrap(),
Some("pixels") => Value::Null,
_ => s.execute("channel.new", json!({})).unwrap(),
};
let active = s.active().unwrap().active_layer;
let params = json!({"target": target});
s.edit("Seed the backdrop", |doc, _| {
let (surface, _) = channel_cmds::target_surface(doc, active, &params)?;
let gray = [MASK_LEVEL];
let rgba = [MASK_LEVEL, MASK_LEVEL, MASK_LEVEL, 1.0];
let sample: &[f32] = if target == "pixels" { &rgba } else { &gray };
for x in 0..40 {
surface.write_pixel(x, 3, sample);
}
Ok(())
})
.unwrap();
s
}
fn gradient_row(mut s: Session, target: &Value, blend: &str) -> Vec<f32> {
let params = json!({
"from": [4, 3], "to": [32, 3], "stops": [[0, "#000000"], [1, "#ffffff"]],
"target": target, "mode": blend, "method": "smooth", "dither": false
});
s.execute("paint.gradient", params.clone()).unwrap();
let state = s.active().unwrap();
let surface = channel_cmds::read_target_surface(&state.doc, state.active_layer, &params).unwrap();
(0..40).map(|x| surface.pixel(x, 3)[0]).collect()
}
#[test]
fn nonseparable_gradient_modes_match_normal_throughout_rgb_layer_masks() {
let target = json!("mask");
for depth in [8, 16] {
let normal = gradient_row(seeded_surface("rgb", depth, &target), &target, "normal");
assert_eq!(normal[0], 0.0);
assert_eq!(normal[39], 1.0);
for mode in ["darkerColor", "lighterColor", "hue", "saturation", "color", "luminosity"] {
assert_eq!(gradient_row(seeded_surface("rgb", depth, &target), &target, mode), normal, "{depth}-bit {mode}");
}
let darken = gradient_row(seeded_surface("rgb", depth, &target), &target, "darken");
assert_eq!(darken[0], 0.0);
assert!((darken[39] - MASK_LEVEL).abs() < 1.0 / 65535.0);
}
}
#[test]
fn outside_the_rgb_mask_depths_darker_color_still_keeps_the_darker_backdrop() {
for (mode, depth, target) in [
("rgb", 8, json!("pixels")),
("rgb", 8, json!("quickMask")),
("rgb", 8, json!({"channel": 0})),
("rgb", 32, json!("mask")),
("gray", 16, json!("mask")),
] {
let normal = gradient_row(seeded_surface(mode, depth, &target), &target, "normal");
assert_eq!(normal[39], 1.0, "{mode} {depth}-bit {target}");
let row = gradient_row(seeded_surface(mode, depth, &target), &target, BlendMode::DarkerColor.label());
assert!((row[39] - MASK_LEVEL).abs() < 1.0 / 65535.0, "{mode} {depth}-bit {target}");
}
}
+14
View File
@@ -209,6 +209,16 @@ fn gradient(s: &mut Session, p: &Value) -> Result<Value> {
s.edit(step_name(p), |doc, _| {
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());
// The RGB 8- and 16-bit layer mask uses Normal for modes that need a colour backdrop.
let mode = if matches!(mode, FillMode::Blend(blend) if !blend.is_separable())
&& crate::channel_cmds::target_of(p) == crate::channel_cmds::Target::Mask
&& doc.mode == photocraft_color::ColorMode::Rgb
&& matches!(doc.depth, photocraft_color::SampleType::U8 | photocraft_color::SampleType::U16)
{
FillMode::Blend(photocraft_color::BlendMode::Normal)
} else {
mode
};
let (surf, lock) = crate::channel_cmds::target_surface(doc, layer, p)?;
paint_gradient(surf, area, from, to, shape, &stops, reverse, opacity, mode, dither, sel.as_ref(), lock);
Ok(())
@@ -317,6 +327,10 @@ pub fn specs() -> Vec<CommandSpec> {
#[cfg(test)]
mod tests;
#[cfg(test)]
#[path = "gradient_mask_greyed_modes_tests.rs"]
mod gradient_mask_greyed_modes_tests;
#[cfg(test)]
mod bucket_no_op_history_tests;