Merge pull request #10533 from yoriswind/codex/stamp-two-color-output

Stamp: choose ink or paper without intermediate gray
This commit is contained in:
FredApps authored and GitHub committed 2026-12-02 01:22:44 +01:00
commit ff7c499d2c
2 files changed
+64 -1

No files matched your search

+2 -1
View File
@@ -1168,7 +1168,8 @@ fn apply_sketch(e: &GalleryEffect, win: &mut Win, ctx: &Ctx) -> bool {
let mut l = win.lum();
blur1(&mut l, w, h, 0.3 + g("smoothness") * 0.3);
for i in 0..win.c.len() {
win.c[i] = mix(bg, fg, smoothstep(bal + 0.02, bal - 0.02, l[i]));
// Smooth the shapes without introducing colors between the ink and paper.
win.c[i] = if l[i] < bal { fg } else { bg };
}
}
F::TornEdges => {
+62
View File
@@ -31,3 +31,65 @@ fn colored_pencil_honours_session_background() {
fn neon_glow_honours_session_background() {
assert_ne!(one("neonGlow", "#ffffff"), one("neonGlow", "#0000ff"), "filter.gallery.neonGlow must honour the session background");
}
/// Stamp uses the selected ink and paper without adding intermediate colors (#9912).
#[test]
fn stamp_uses_only_the_selected_colors() {
for depth in [8, 16, 32] {
for (fg, bg, ink, paper) in [("#000000", "#ffffff", [0.0, 0.0, 0.0], [1.0, 1.0, 1.0]), ("#ff0000", "#0000ff", [1.0, 0.0, 0.0], [0.0, 0.0, 1.0])] {
for smoothness in [1, 5, 50] {
let mut s = Session::new();
s.execute("file.new", json!({"width": 16, "height": 16, "depth": depth, "background": "#808080"})).unwrap();
s.execute("tools.setColors", json!({"foreground": fg, "background": bg})).unwrap();
s.execute("filter.gallery.stamp", json!({"lightDarkBalance": 25, "smoothness": smoothness})).unwrap();
for p in image(&mut s).as_chunks::<4>().0 {
assert!(p[..3] == ink || p[..3] == paper, "{depth}-bit, smoothness {smoothness}: Stamp added an intermediate color: {p:?}");
assert_eq!(p[3], 1.0);
}
}
}
}
}
/// Balance controls the chosen color, with the selection boundary and one-step Undo intact.
#[test]
fn stamp_balance_selection_and_undo() {
for depth in [8, 16, 32] {
for (balance, expected) in [(0, 1.0), (40, 0.0), (50, 0.0)] {
let mut s = Session::new();
s.execute("file.new", json!({"width": 16, "height": 16, "depth": depth, "background": "#808080"})).unwrap();
s.execute("select.rect", json!({"x": 4, "y": 4, "width": 8, "height": 8, "antiAlias": false})).unwrap();
let before = s.active().unwrap().doc.clone();
let history = s.active().unwrap().history.past_len();
let outside = px(&mut s, 2, 2);
s.execute("filter.gallery.stamp", json!({"lightDarkBalance": balance, "smoothness": 1})).unwrap();
assert_eq!(px(&mut s, 8, 8), [expected, expected, expected, 1.0]);
assert_eq!(px(&mut s, 2, 2), outside);
assert_eq!(s.active().unwrap().history.past_len(), history + 1);
s.undo();
assert_eq!(*s.active().unwrap().doc, *before);
}
}
}
/// Smoothness changes spatial detail without adding intermediate colors.
#[test]
fn stamp_smoothness_changes_spatial_detail() {
for depth in [8, 16, 32] {
let render = |smoothness| {
let mut s = Session::new();
s.execute("file.new", json!({"width": 16, "height": 16, "depth": depth, "background": "#ffffff"})).unwrap();
s.execute("select.rect", json!({"x": 6, "y": 6, "width": 4, "height": 4, "antiAlias": false})).unwrap();
s.execute("edit.fill", json!({"contents": "color", "color": "#000000"})).unwrap();
s.execute("select.deselect", json!({})).unwrap();
s.execute("filter.gallery.stamp", json!({"lightDarkBalance": 25, "smoothness": smoothness})).unwrap();
image(&mut s)
};
let fine = render(1);
let smooth = render(50);
assert_ne!(fine, smooth, "{depth}-bit: Smoothness must change the spatial result");
for p in fine.as_chunks::<4>().0.iter().chain(smooth.as_chunks::<4>().0.iter()) {
assert!(p[..3] == [0.0; 3] || p[..3] == [1.0; 3]);
}
}
}