Include opaque default pixels in Lab Replace Color

This commit is contained in:
yoriswind committed 2026-11-01 15:56:39 -08:00
1 parent e378246991
commit 6152cffe4e
2 files changed
+42 -2

No files matched your search

@@ -80,12 +80,17 @@ pub(super) fn apply(s: &mut Session, p: &Value) -> Result<Option<Value>> {
return Ok(Some(Value::Null));
}
s.edit("Replace Color", |doc, _| {
let canvas = doc.bounds();
let selected = doc.selection.clone();
let surface = crate::commands::paint_surface(doc, id, &Value::Null)?;
let bounds = edit_bounds(surface.content_bounds(), selected.as_ref());
let transparent_default = format.alpha && surface.default_pixel().last().is_some_and(|a| a.is_nan() || *a <= 0.0);
let content = surface.content_bounds();
// A nontransparent default is visible across the canvas even without stored tiles.
let extent = if transparent_default { content } else { content.union(&canvas) };
let bounds = edit_bounds(extent, selected.as_ref());
// Transparent gaps cannot match. Visit stored tiles instead of the possibly
// enormous rectangle between two distant pixels.
let stored = if format.alpha && surface.default_pixel().last().is_some_and(|a| a.is_nan() || *a <= 0.0) {
let stored = if transparent_default {
let mut tiles = crate::allocation::capacity(surface.tiles().count(), "Replace Color tile coordinates")?;
tiles.extend(surface.tiles().map(|(tile, _)| *tile));
Some(tiles)
@@ -492,6 +492,41 @@ fn lab_replace_visits_only_stored_tiles_when_the_default_is_transparent() {
assert_eq!(s.active().unwrap().doc, before);
}
/// Opaque defaults remain visible when the surface has no tiles or only a small exception.
#[test]
fn lab_replace_includes_visible_default_pixels_with_and_without_alpha() {
for depth in [8, 16, 32] {
for (alpha, exception) in [(false, false), (false, true), (true, false), (true, true)] {
let mut s = fixture("lab", depth);
s.edit("Opaque default Lab", |doc, active| {
let surface = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap();
let format = photocraft_doc::PixelFormat { alpha, ..surface.format() };
let channels = format.channels();
*surface = photocraft_raster::Surface::with_default(format, &[0.5, 128.0 / 255.0, 128.0 / 255.0, 1.0][..channels]);
if exception {
surface.fill_rect(photocraft_doc::Rect::new(1, 0, 2, 1), &[0.25, 128.0 / 255.0, 128.0 / 255.0, 1.0][..channels]);
} else {
assert!(surface.content_bounds().is_empty());
}
Ok(())
})
.unwrap();
let before = s.active().unwrap().doc.clone();
let original = raw(&s);
s.execute("image.adjustments.replaceColor", json!({"labMinimum":[49,0,0],"labMaximum":[51,0,0],"fuzziness":0,"lightness":100})).unwrap();
for (i, (pixel, original)) in raw(&s).chunks_exact(3 + usize::from(alpha)).zip(original.chunks_exact(3 + usize::from(alpha))).enumerate() {
if exception && i == 1 {
assert_eq!(pixel, original);
} else {
assert!(pixel[0] > 0.99, "{depth}/{alpha}/{exception} pixel {i}: {pixel:?}");
}
}
assert!(s.undo());
assert_eq!(s.active().unwrap().doc, before);
}
}
}
/// Existing RGB replacement and ordinary gradient export retain their native behavior.
#[test]
fn legacy_rgb_replace_color_and_ordinary_gradient_exports_are_unchanged() {