Styles: overlays keep anti-aliased edges clean (#416)

Cause: the interior styles were blended over the layer body with the
content's coverage folded into their opacity. On an edge pixel at 50 %
coverage a Gradient or Color Overlay went over the half-transparent body
as a half-transparent layer, so the result kept a quarter of the layer's
own fill and its alpha rose to 0.75: a light rim (the white fill
showing through) on styled text.

Fix: blend_clipped composites the interior styles (Gradient Overlay,
Color Overlay, Satin, Inner Glow, Inner Shadow and the inside of
Stroke) onto the layer within its shape: the body is divided by the
content coverage, blended with the style at its own opacity, and
multiplied back, so the alpha is kept and an edge pixel mixes the style
with the layer's colour inside the shape only, as a clipped layer does.
Fill Opacity and Blend Interior Styles as Group behave as before.

Tests: tests_styles::overlays_leave_antialiased_edges_clean (a masked
square with half-covered edge columns; Color Overlay, Gradient Overlay
and an inside Stroke now match a plain layer of the overlay colour;
Color Overlay's edge was 0.41 red against 0.23 before).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
bflatastic
2026-10-09 23:17:32 +07:00
co-authored by Claude Opus 5.5
parent 88e2e6470a
commit 36ef45077a
4 changed files with 112 additions and 22 deletions
+63 -2
View File
@@ -1,10 +1,10 @@
//! Layer Styles pixel tests.
use effectcraft_color::Label;
use effectcraft_keyframe::{Gradient, Keyframe, Value};
use effectcraft_keyframe::{Gradient, Keyframe, ShapePath, Value};
use effectcraft_project::build::{self, Ids};
use effectcraft_project::styles::{self as st, GROUP};
use effectcraft_project::{Comp, ItemId, ItemKind, Layer, LayerSource, Project, Solid};
use effectcraft_project::{Comp, ItemId, ItemKind, Layer, LayerSource, MaskMode, Project, Solid};
use effectcraft_time::{FrameRate, Tick};
use crate::{Image, NoFootage, RenderOpts, Renderer, render_frame};
@@ -182,6 +182,67 @@ fn gradient_overlay_follows_angle() {
assert!(img.get(81, 50)[0] > 0.9 && img.get(118, 50)[0] < 0.1);
}
/// Overlays replace the layer's colour inside its alpha: an anti-aliased edge pixel mixes the
/// background with the overlay colour only, exactly as a layer of that colour would, with no
/// fringe of the layer's own colour (#416).
#[test]
fn overlays_leave_antialiased_edges_clean() {
let (blue, bg) = ([0.2, 0.533, 1.0], [0.25, 0.22, 0.45]);
// A mask half a pixel inside the square's left and right sides: in layer space, where the
// styles run, columns 80 and 119 are half covered.
let edge = |color: [f32; 3], f: &dyn Fn(&mut Project, &Comp, &mut Layer)| {
let (p, cid) = scene(color, Some(bg), |p, c, l| {
let mut next = p.next_id;
let pts = vec![[0.5, 0.0], [39.5, 0.0], [39.5, 40.0], [0.5, 40.0]];
let sp = ShapePath { in_tangents: vec![[0.0; 2]; 4], out_tangents: vec![[0.0; 2]; 4], vertices: pts, closed: true, feather: Vec::new() };
let m = build::mask(&mut Ids(&mut next), "Mask 1", sp, MaskMode::Add, [255, 255, 0]);
p.next_id = next;
l.props.group_mut("masks").unwrap().children.push(m.into());
f(p, c, l);
});
let img = render(&p, cid);
[img.get(80, 50), img.get(119, 50), img.get(100, 50)]
};
let plain = edge(blue, &|_, _, _| {});
assert!(near(plain[0][0], 0.5 * (blue[0] + bg[0]), 0.01), "half covered: {:?}", plain[0]);
let color = Value::Color([blue[0] as f64, blue[1] as f64, blue[2] as f64, 1.0]);
let flat =
Gradient { colors: vec![(0.0, [blue[0], blue[1], blue[2], 1.0]), (1.0, [blue[0], blue[1], blue[2], 1.0])], opacities: vec![(0.0, 1.0), (1.0, 1.0)] };
let styled: [(&str, Box<dyn Fn(&mut Project, &Comp, &mut Layer)>); 3] = [
(
"Color Overlay",
Box::new(|p, c, l| {
add(p, c, l, "colorOverlay");
set(l, "colorOverlay/color", color.clone());
}),
),
(
"Gradient Overlay",
Box::new(|p, c, l| {
add(p, c, l, "gradientOverlay");
set(l, "gradientOverlay/colors", Value::Gradient(flat.clone()));
}),
),
(
"Stroke (inside)",
Box::new(|p, c, l| {
add(p, c, l, "stroke");
set(l, "stroke/position", Value::Enum(1));
set(l, "stroke/size", Value::Scalar(25.0));
set(l, "stroke/color", color.clone());
}),
),
];
for (name, f) in &styled {
let px = edge([1.0; 3], f.as_ref());
for (a, b) in px.iter().zip(&plain) {
for ch in 0..4 {
assert!(near(a[ch], b[ch], 0.01), "{name}: {a:?} against the plain layer {b:?}");
}
}
}
}
fn bevel_scene(angle: f64) -> Image {
let (p, cid) = scene([0.5; 3], None, |p, c, l| {
add(p, c, l, "bevelEmboss");