Preserve near-black brush texture in Hard Mix

This commit is contained in:
yoriswind committed 2026-11-30 23:40:32 -08:00
1 parent 3fc81b6a30
commit 3e5013cea5
2 files changed
+63 -5

No files matched your search

@@ -3,7 +3,7 @@
use photocraft_color::PixelFormat;
use photocraft_raster::Surface;
use crate::{BrushSettings, GrayTile, MaskMode, Pattern, StrokePoint, Texture, TipShape, render_stroke};
use crate::{BrushContext, BrushSettings, GrayTile, MaskMode, Pattern, StrokePoint, Texture, TipShape, 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 });
@@ -97,6 +97,55 @@ fn hard_mix_zero_texture_keeps_its_existing_endpoint() {
}
}
#[test]
fn hard_mix_keeps_legacy_soft_tip_coverage_for_black_and_near_black_texture() {
let near_black = 1.0 / 255.0;
for t in [0.0, near_black / 2.0, near_black] {
for v in [0.9, 0.999, 1.0] {
for depth in [0.0, 0.25, 1.0] {
let actual = crate::render::texture_combine(MaskMode::HardMix, v, t, depth);
let expected = crate::render::mask_combine(MaskMode::HardMix, v, t, depth);
assert_eq!(actual, expected, "coverage {v}, texture {t}, depth {depth}");
}
}
}
// Stepping above the first grey level must not reintroduce the jump at the reported rim coverage.
assert_eq!(crate::render::texture_combine(MaskMode::HardMix, 0.9, 2.0 / 255.0, 1.0), 0.0);
let mut brush = BrushSettings { size: 60.0, hardness: 0.0, seed: 17314, ..BrushSettings::default() };
let stroke = [StrokePoint::new(100.0, 100.0, 1.0)];
let mut plain = Surface::new(PixelFormat::RGBA32F);
render_stroke(&mut plain, &brush, &stroke, None, false, 1.0);
let pattern = GrayTile::from_fn(16, 16, |x, _| if x < 8 { 0.0 } else { near_black });
for scale in [1.0, 1.5] {
brush.texture = Texture {
enabled: true,
pattern: Pattern::Tile(pattern.clone()),
mode: MaskMode::HardMix,
depth: 1.0,
scale,
each_tip: true,
..Texture::default()
};
let context = BrushContext::new(&brush);
if scale > 1.0 {
assert!((70..=130).any(|x| {
let sample = context.texture_at(x, 100);
sample > 0.0 && sample < near_black * 0.9
}));
}
let mut actual = Surface::new(PixelFormat::RGBA32F);
render_stroke(&mut actual, &brush, &stroke, None, false, 1.0);
for x in 70..=130 {
let v = plain.rgba(x, 100)[3];
let t = context.texture_at(x, 100);
let expected = crate::render::mask_combine(MaskMode::HardMix, v, t, 1.0);
assert_eq!(actual.rgba(x, 100)[3], expected, "x={x}, scale {scale}, coverage {v}, texture {t}");
}
}
}
#[test]
fn hard_tip_rims_preserve_legacy_texture_modes_for_both_each_tip_paths() {
// The same bitmap must retain legacy math at either ignored hardness value, not just
+13 -4
View File
@@ -213,12 +213,21 @@ fn texture_combine_for_tip(mode: MaskMode, v: f32, t: f32, d: f32, legacy_tip: b
if reach < 1.0 { reach * (1.0 - t) } else { reach - t }
}
MaskMode::HardMix => {
if legacy_tip || v >= 1.0 || t <= 0.0 {
// Preserve the existing hard-tip endpoints, including a zero-valued pattern.
const NEAR_BLACK: f32 = 1.0 / 255.0;
const DARKEST_MEASURED: f32 = 64.0 / 255.0;
if legacy_tip || v >= 1.0 || t <= NEAR_BLACK {
// Preserve the hard-tip endpoints and the legacy threshold for near-black textures.
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);
let mixed = if t < DARKEST_MEASURED {
// Narrow the measured ramp around the legacy threshold as the pattern approaches black.
let width = (t - NEAR_BLACK) / (DARKEST_MEASURED - NEAR_BLACK);
let threshold = 1.0 - t;
threshold + 4.0 * (v - threshold) / width
} else {
4.0 * v - 3.0 * (1.0 - t)
};
let mixed = mixed.clamp(0.0, 1.0);
v + (mixed - v) * d
}
}