diff --git a/crates/doc/src/bitmap.rs b/crates/doc/src/bitmap.rs index 55c1bd5..06d4785 100644 --- a/crates/doc/src/bitmap.rs +++ b/crates/doc/src/bitmap.rs @@ -309,12 +309,16 @@ pub fn pencil( dirty } +/// Whether a pixel belongs with the seed: transparent with transparent (by alpha), visible with visible by colour +/// alone, so a stroke's soft, half-transparent edge goes with the stroke. fn close(a: &[u8], b: [u8; 4], tol: u8) -> bool { - // Fully transparent pixels match each other whatever their colour channels say. - if a[3] == 0 && b[3] == 0 { - return true; + if b[3] == 0 { + return a[3] <= tol; } - (0..4).all(|i| (a[i] as i16 - b[i] as i16).unsigned_abs() <= tol as u16) + if a[3] == 0 { + return false; + } + (0..3).all(|i| (a[i] as i16 - b[i] as i16).unsigned_abs() <= tol as u16) } /// The region around (x, y) whose pixels are within `tol` of its colour (4-connected). diff --git a/crates/doc/tests/bitmap.rs b/crates/doc/tests/bitmap.rs index cfcbce0..d55ba6d 100644 --- a/crates/doc/tests/bitmap.rs +++ b/crates/doc/tests/bitmap.rs @@ -55,6 +55,17 @@ fn wand_selects_a_region_and_delete_clears_only_it() { assert!(!m.outline().is_empty()); } +#[test] +fn wand_and_delete_take_a_stroke_with_its_soft_edge() { + let mut r = Raster::new(120, 60); + let b = Brush { radius: 8.0, hardness: 0.3, color: [31, 42, 68, 255], opacity: 1.0, erase: false }; + bitmap::brush(&mut r, &[(10.0, 30.0, 1.0), (110.0, 30.0, 1.0)], &b, None); + assert!(r.rgba.chunks_exact(4).any(|p| p[3] > 0 && p[3] < 255), "the stroke has a soft edge"); + let m = bitmap::region(&r, 60, 30, 24); + bitmap::clear(&mut r, Some(&m)); + assert!(r.rgba.chunks_exact(4).all(|p| p[3] == 0), "pixels of the stroke were left behind"); +} + #[test] fn lasso_and_rect_masks() { let m = Mask::polygon(100, 100, &[(10.0, 10.0), (90.0, 10.0), (10.0, 90.0)]);