Merge pull request #18839

Co-authored-by: autocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com>
This commit is contained in:
davidmann4andautocraft-bot[bot] authored and autocraft-bot[bot] committed 2026-12-02 01:07:07 +00:00
commit 3b33bf3029
4 files changed
+102 -9

No files matched your search

+2
View File
@@ -19,6 +19,8 @@ mod gradient_alpha_method_tests;
mod gradient_behind_mode_tests;
#[cfg(test)]
mod gradient_mask_history_name_tests;
#[cfg(test)]
mod gradient_mask_opacity_half_tests;
pub mod paint_cmds;
pub mod pick_cmds;
pub mod redeye_cmds;
@@ -0,0 +1,85 @@
//! #18669: at 50% opacity a gradient covers 128/255 of a layer mask.
use photocraft_raster::Surface;
use serde_json::{Value, json};
use crate::Session;
fn document(mode: &str, depth: u32) -> Session {
let mut s = crate::session();
s.execute("file.new", json!({"width": 64, "height": 8, "mode": mode, "depth": depth, "background": "transparent"})).unwrap();
s
}
fn gradient(opacity: f64, target: Value, method: Option<&str>) -> Value {
let mut p = json!({"from": [8, 4], "to": [56, 4], "colors": ["#000000", "#ffffff"], "opacity": opacity, "target": target, "dither": false});
if let Some(method) = method {
p["method"] = json!(method);
}
p
}
fn target<'a>(s: &'a Session, p: &Value) -> &'a Surface {
let state = s.active().unwrap();
crate::channel_cmds::read_target_surface(&state.doc, state.active_layer, p).unwrap()
}
fn layer_mask_row(mode: &str, depth: u32, opacity: f64, method: Option<&str>) -> [f32; 3] {
let mut s = document(mode, depth);
s.execute("layer.layerMask.revealAll", json!({})).unwrap();
let p = gradient(opacity, json!("mask"), method);
s.execute("paint.gradient", p.clone()).unwrap();
[4, 32, 60].map(|x| target(&s, &p).pixel(x, 4)[0])
}
#[test]
fn half_opacity_leaves_127_levels_of_white_on_the_layer_mask() {
for (mode, depth, step) in [
("rgb", 8, 1.0 / 255.0),
("rgb", 16, 1.0 / 65535.0),
("rgb", 32, 0.0),
("gray", 8, 1.0 / 255.0),
("gray", 16, 1.0 / 65535.0),
("gray", 32, 0.0),
("cmyk", 8, 1.0 / 255.0),
("lab", 8, 1.0 / 255.0),
] {
// The desktop dispatches the Classic gradient without a method. The report used Smooth.
for method in [None, Some("smooth")] {
let case = format!("{mode}/{depth} {method:?}");
let [black, middle, white] = layer_mask_row(mode, depth, 50.0, method);
let [full_black, full_middle, full_white] = layer_mask_row(mode, depth, 100.0, method);
assert_eq!(full_black, 0.0, "{case} at full opacity");
assert!((full_white - 1.0).abs() <= 0.00001, "{case}: white end at full opacity {full_white}");
assert!((black - 127.0 / 255.0).abs() <= 0.5 * step + 0.00001, "{case}: black end left {black} white");
let expected_middle = 1.0 - (1.0 - full_middle) * (128.0 / 255.0);
assert!((middle - expected_middle).abs() <= step + 0.00001, "{case}: ramp midpoint {middle}, expected {expected_middle}");
assert!((white - 1.0).abs() <= 0.00001, "{case}: white end left {white} white");
}
}
}
#[test]
fn other_opacity_and_non_layer_mask_targets_keep_the_requested_coverage() {
// Include a value that rounds to 50% after the command converts to f32.
for opacity in [49.9, 50.000_000_1] {
let [black, _, _] = layer_mask_row("rgb", 32, opacity, Some("smooth"));
assert!((f64::from(black) - (1.0 - opacity / 100.0)).abs() < 0.00001, "{opacity}% opacity left {black} white");
}
let mut s = document("rgb", 32);
let pixels = gradient(50.0, json!("pixels"), Some("smooth"));
s.execute("paint.gradient", pixels.clone()).unwrap();
assert!((target(&s, &pixels).pixel(4, 4)[3] - 0.5).abs() < 0.00001, "pixel coverage");
s.execute("channel.new", json!({"fill": "white"})).unwrap();
let alpha = gradient(50.0, json!({"channel": 0}), Some("smooth"));
s.execute("paint.gradient", alpha.clone()).unwrap();
assert!((target(&s, &alpha).pixel(4, 4)[0] - 0.5).abs() < 0.00001, "saved alpha channel");
s.execute("select.all", json!({})).unwrap();
s.execute("select.editInQuickMaskMode", json!({"on": true})).unwrap();
let quick_mask = gradient(50.0, json!("quickMask"), Some("smooth"));
s.execute("paint.gradient", quick_mask.clone()).unwrap();
assert!((target(&s, &quick_mask).pixel(4, 4)[0] - 0.5).abs() < 0.00001, "Quick Mask");
}
@@ -80,12 +80,12 @@ fn only_explicit_classic_uses_the_half_opacity_step() {
}
#[test]
fn the_half_opacity_pixel_step_does_not_change_layer_masks() {
let mut s = document("rgb", 32);
fn the_half_opacity_pixel_step_does_not_change_deep_cmyk_layer_masks() {
let mut s = document("cmyk", 16);
s.execute("layer.layerMask.revealAll", json!({})).unwrap();
s.execute("paint.gradient", json!({"from": [2, 3], "to": [9, 3], "colors": ["#000000", "#ffffff"], "opacity": 50, "target": "mask", "dither": false}))
.unwrap();
let state = s.active().unwrap();
let mask = &state.doc.layer(state.active_layer.unwrap()).unwrap().mask.as_ref().unwrap().surface;
assert_eq!((mask.pixel(2, 3)[0] * 255.0).round(), 128.0);
assert_eq!((mask.pixel(2, 3)[0] * 65535.0).round(), 32768.0);
}
+12 -6
View File
@@ -221,12 +221,18 @@ fn gradient(s: &mut Session, p: &Value) -> Result<Value> {
let dither = b(p, "dither", true); // Photoshop dithers gradients by default (reduces banding).
let layer = if p.get("layer").is_some() { Some(crate::commands::layer_param(s, p)?) } else { s.active().and_then(|st| st.active_layer) };
s.edit(step_name(p), |doc, _| {
// At the measured Classic 50% setting, deep RGB and 16-bit CMYK pixels use 8-bit source coverage.
let opacity = if target == crate::channel_cmds::Target::Pixels
&& (method_param.is_none() || method == Some(photocraft_io::GradientMethod::Classic))
&& p.get("opacity").and_then(Value::as_f64) == Some(50.0)
&& matches!((doc.mode, doc.depth), (ColorMode::Rgb, SampleType::U16 | SampleType::F32) | (ColorMode::Cmyk, SampleType::U16))
{
// The measured 50% setting uses 8-bit source coverage for these layer masks and Classic pixel gradients.
let opacity = if p.get("opacity").and_then(Value::as_f64) == Some(50.0)
&& match target {
crate::channel_cmds::Target::Mask => {
matches!((doc.mode, doc.depth), (ColorMode::Rgb | ColorMode::Grayscale, _) | (ColorMode::Cmyk | ColorMode::Lab, SampleType::U8))
}
crate::channel_cmds::Target::Pixels => {
(method_param.is_none() || method == Some(photocraft_io::GradientMethod::Classic))
&& matches!((doc.mode, doc.depth), (ColorMode::Rgb, SampleType::U16 | SampleType::F32) | (ColorMode::Cmyk, SampleType::U16))
}
_ => false,
} {
128.0 / 255.0
} else {
opacity