Grade soft brush texture edges in Hard Mix and Color Dodge

This commit is contained in:
yoriswind committed 2026-11-28 09:11:17 -08:00
1 parent 0331a92ef9
commit 94eb3ca7a2
3 files changed
+86 -1

No files matched your search

@@ -0,0 +1,68 @@
//! #17314: own 16x16 two-grey fixture; published measured profiles are acceptance samples.
use photocraft_color::PixelFormat;
use photocraft_raster::Surface;
use crate::{BrushSettings, GrayTile, MaskMode, Pattern, StrokePoint, Texture, render_stroke};
fn paint(mode: MaskMode, hardness: f32, flow: f32, depth: f32) -> Surface {
let pattern = GrayTile::from_fn(16, 16, |x, _| if x < 8 { 64.0 / 255.0 } else { 192.0 / 255.0 });
let brush = BrushSettings {
size: 60.0,
hardness,
flow,
seed: 17314,
texture: Texture { enabled: true, pattern: Pattern::Tile(pattern), mode, depth, each_tip: true, ..Texture::default() },
..BrushSettings::default()
};
let mut surface = Surface::new(PixelFormat::RGBA32F);
render_stroke(&mut surface, &brush, &[StrokePoint::new(100.0, 100.0, 1.0)], None, false, 1.0);
surface
}
fn grey(surface: &Surface, x: i32) -> f32 {
255.0 * (1.0 - surface.rgba(x, 100)[3])
}
#[test]
fn hard_mix_grades_the_measured_soft_dab_rim_at_both_pattern_levels() {
let surface = paint(MaskMode::HardMix, 0.0, 1.0, 1.0);
let measured = [255, 255, 248, 196, 132, 255, 255, 255, 188, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 144, 232, 255, 255, 92, 164, 224, 255, 255, 255];
for (x, expected) in (70..=130).step_by(2).zip(measured) {
let actual = grey(&surface, x);
assert!((actual - expected as f32).abs() <= 8.0, "x={x}: {actual}, measured {expected}");
}
}
#[test]
fn color_dodge_keeps_the_measured_soft_shoulder() {
let surface = paint(MaskMode::ColorDodge, 0.0, 1.0, 1.0);
// The issue elides the middle; assert only its explicitly supplied coordinates.
let samples = [
(70, 218), (72, 116), (74, 71), (76, 22), (78, 0), (80, 127), (82, 100), (84, 73), (86, 44), (88, 0),
(110, 0), (112, 29), (114, 58), (116, 87), (118, 115), (120, 0), (122, 0), (124, 49), (126, 94), (128, 213), (130, 223),
];
for (x, expected) in samples {
let actual = grey(&surface, x);
assert!((actual - expected as f32).abs() <= 8.0, "x={x}: {actual}, measured {expected}");
}
}
#[test]
fn hard_dab_flow_and_zero_depth_keep_their_existing_values() {
for mode in [MaskMode::HardMix, MaskMode::ColorDodge] {
for (flow, expected) in [(1.0, 0.0), (0.5, 127.5)] {
let surface = paint(mode, 1.0, flow, 1.0);
for x in [100, 108] {
assert!((grey(&surface, x) - expected).abs() <= 2.0);
}
}
let zero = paint(mode, 0.0, 1.0, 0.0);
let plain = BrushSettings { size: 60.0, hardness: 0.0, seed: 17314, ..BrushSettings::default() };
let mut reference = Surface::new(PixelFormat::RGBA32F);
render_stroke(&mut reference, &plain, &[StrokePoint::new(100.0, 100.0, 1.0)], None, false, 1.0);
for x in 60..=140 {
assert!((zero.rgba(x, 100)[3] - reference.rgba(x, 100)[3]).abs() < 1e-6, "{mode:?}, x={x}");
}
}
}
+3
View File
@@ -417,3 +417,6 @@ mod mixer_load_supply_tests;
#[cfg(test)]
mod smoothing_catch_up_default_tests;
#[cfg(test)]
mod brush_texture_hard_mix_tests;
+15 -1
View File
@@ -194,7 +194,11 @@ pub fn texture_combine(mode: MaskMode, v: f32, t: f32, d: f32) -> f32 {
}
let r = match mode {
MaskMode::Subtract => v - d * t,
MaskMode::ColorDodge => v / (1.0 - d * t).max(1e-3),
MaskMode::ColorDodge => {
// A small soft-coverage headroom matches the measured shoulder without changing solid tips.
let texture = t * (1.0 - (1.0 - v) / 32.0);
v / (1.0 - d * texture).max(1e-3)
}
MaskMode::ColorBurn => 1.0 - (1.0 - v) / (1.0 - d + d * t).max(1e-3),
// Photoshop steps the depth in 1/255ths here, which shows at the lowest depths.
MaskMode::Height => HEIGHT_REACH * (d * 255.0).round() / 255.0 * v - t,
@@ -202,6 +206,16 @@ pub fn texture_combine(mode: MaskMode, v: f32, t: f32, d: f32) -> f32 {
let reach = HEIGHT_REACH * (d * 255.0).round() / 255.0 * v;
if reach < 1.0 { reach * (1.0 - t) } else { reach - t }
}
MaskMode::HardMix => {
if v >= 1.0 {
// Preserve the existing hard-tip endpoints, including a zero-valued pattern.
mask_combine(mode, v, t, d)
} else {
// A finite transition through the height threshold, fitted to the measured soft rim.
let mixed = (4.0 * v - 3.0 * (1.0 - t)).clamp(0.0, 1.0);
v + (mixed - v) * d
}
}
_ => mask_combine(mode, v, t, d),
};
if r.is_finite() { r.clamp(0.0, 1.0) } else { 0.0 }