Merge pull request #18686

This commit is contained in:
autocraft-bot[bot] committed 2026-12-01 15:26:27 +00:00
commit edbbf676fa
2 files changed
+111 -2

No files matched your search

@@ -0,0 +1,91 @@
use photocraft_algo::paint::{FillMode, GradientShape, paint_gradient};
use photocraft_color::BlendMode;
use photocraft_geom::Rect;
use photocraft_raster::{Surface, from_rgba};
use serde_json::json;
use crate::Session;
const SOURCE: [f32; 4] = [0.25, 0.5, 0.75, 1.0];
fn active_surface(s: &Session) -> &Surface {
let state = s.active().unwrap();
state.doc.layer(state.active_layer.unwrap()).unwrap().surface().unwrap()
}
fn document(mode: &str, depth: u8) -> Session {
let mut s = crate::session();
s.execute("file.new", json!({"width": 16, "height": 8, "mode": mode, "depth": depth, "background": "transparent"})).unwrap();
s.edit("Set the three backdrop alphas", |doc, _| {
let surface = doc.layers[0].surface_mut().unwrap();
let format = surface.format();
for (x, alpha) in [(3, 1.0), (4, 0.0), (5, 0.5)] {
surface.write_pixel(x, 3, &from_rgba(&format, [0.8, 0.3, 0.2, alpha]));
}
Ok(())
})
.unwrap();
s
}
#[test]
fn gradient_uses_the_measured_half_opacity_step_only_on_deep_rgb_and_cmyk_pixels() {
for (mode, depth, uses_step) in [("rgb", 8, false), ("rgb", 16, true), ("rgb", 32, true), ("cmyk", 8, false), ("cmyk", 16, true), ("gray", 16, false)] {
for blend in [BlendMode::Normal, BlendMode::Multiply, BlendMode::Screen] {
for (percent, coverage) in [(50.0, if uses_step { 128.0 / 255.0 } else { 0.5 }), (23.5, 0.235)] {
let mut s = document(mode, depth);
let mut wanted = active_surface(&s).clone();
paint_gradient(
&mut wanted,
Rect::new(0, 0, 16, 8),
(2.0, 3.0),
(9.0, 3.0),
GradientShape::Linear,
&[(0.0, SOURCE), (1.0, SOURCE)],
false,
coverage,
FillMode::Blend(blend),
false,
None,
false,
);
s.execute(
"paint.gradient",
json!({"from": [2, 3], "to": [9, 3], "colors": [SOURCE, SOURCE], "opacity": percent, "mode": blend.label(), "dither": false}),
)
.unwrap();
let actual = active_surface(&s);
for x in 3..=5 {
assert_eq!(actual.pixel(x, 3), wanted.pixel(x, 3), "{mode} {depth}-bit, {blend:?}, {percent}%, x={x}");
}
if uses_step && percent == 50.0 {
assert!((actual.rgba(4, 3)[3] - 128.0 / 255.0).abs() < 0.0005);
assert!((actual.rgba(5, 3)[3] - (0.5 + 64.0 / 255.0)).abs() < 0.0005);
}
}
}
}
}
#[test]
fn only_explicit_classic_uses_the_half_opacity_step() {
for (method, coverage) in [("Classic", 128.0 / 255.0), ("linear", 0.5), ("perceptual", 0.5), ("smooth", 0.5)] {
let mut s = document("rgb", 32);
s.execute("paint.gradient", json!({"from": [2, 3], "to": [9, 3], "colors": [SOURCE, SOURCE], "method": method, "opacity": 50, "dither": false}))
.unwrap();
let alpha = active_surface(&s).rgba(4, 3)[3];
assert!((alpha - coverage).abs() < 0.0005, "{method}: alpha {alpha}, expected {coverage}");
}
}
#[test]
fn the_half_opacity_pixel_step_does_not_change_layer_masks() {
let mut s = document("rgb", 32);
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);
}
+20 -2
View File
@@ -1,6 +1,7 @@
//! Paint helpers: Paint Bucket and Gradient tool.
use photocraft_algo::paint::{FillMode, GradientShape, bucket_fill_inks, bucket_fill_region_inks, paint_gradient};
use photocraft_color::{ColorMode, SampleType};
use serde_json::{Value, json};
use crate::commands::{CommandSpec, parse_blend};
@@ -172,7 +173,9 @@ fn gradient(s: &mut Session, p: &Value) -> Result<Value> {
// Photoshop's interpolation (`method`, with `smoothness` 0..=100, default 100) baked into dense
// stops, as for Gradient Overlay: Photoshop 27.11's Classic black → white is 0.037 at t = 1/16
// where straight stops give 0.0625.
let stops = match p.get("method").and_then(Value::as_str).and_then(photocraft_io::GradientMethod::from_name) {
let method_param = p.get("method");
let method = method_param.and_then(Value::as_str).and_then(photocraft_io::GradientMethod::from_name);
let stops = match method {
Some(m) if stops.len() >= 2 => {
let smoothness = (f(p, "smoothness", 100.0) / 100.0).clamp(0.0, 1.0);
// Smooth at 100 % Smoothness bakes the colour and opacity stops apart: the colours by
@@ -210,13 +213,24 @@ fn gradient(s: &mut Session, p: &Value) -> Result<Value> {
let reverse = b(p, "reverse", false);
let opacity = opacity(p, "paint.gradient")?;
let mode = fill_mode(p, "paint.gradient", false)?;
let target = crate::channel_cmds::target_of(p);
// Masks and channels have no transparency to paint behind: Behind would change nothing there.
if mode == FillMode::Behind && crate::channel_cmds::target_of(p) != crate::channel_cmds::Target::Pixels {
if mode == FillMode::Behind && target != crate::channel_cmds::Target::Pixels {
return Err(EngineError::BadParams { cmd: "paint.gradient".into(), msg: "mode `behind` paints only a layer's pixels, not a mask or channel".into() });
}
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))
{
128.0 / 255.0
} else {
opacity
};
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.
@@ -357,3 +371,7 @@ mod gradient_mask_smooth_transparency_tests;
#[cfg(test)]
mod bucket_behind_tests;
#[cfg(test)]
#[path = "gradient_opacity_step_tests.rs"]
mod gradient_opacity_step_tests;