diff --git a/crates/engine-core/src/pixel_fill.rs b/crates/engine-core/src/pixel_fill.rs index 3bde7cb187..95f2ae4bd9 100644 --- a/crates/engine-core/src/pixel_fill.rs +++ b/crates/engine-core/src/pixel_fill.rs @@ -230,13 +230,21 @@ pub fn keep_in_color_table(table: &photocraft_doc::ColorTable, pre: &Surface, su let w = px.len(); quantize(&mut px, w, &table.colors, Dither::None, 0.0, table.transparent.map(usize::from)); let mut mapped = changed.into_iter().zip(px).peekable(); + let mut moved = false; for (i, q) in data.chunks_exact_mut(n).enumerate() { if let Some((_, c)) = mapped.next_if(|(j, _)| *j == i) { let m = from_rgba_into(&fmt, c, &mut enc); - q.iter_mut().zip(enc.iter().take(m)).for_each(|(d, v)| *d = *v); + for (d, v) in q.iter_mut().zip(enc.iter().take(m)) { + moved |= (*d - *v).abs() > 1e-6; + *d = *v; + } } } - surf.write_region(t, &data); + // Already table colours (a snapped fill colour's shared solid tiles): writing them back + // would copy every shared tile for nothing. + if moved { + surf.write_region(t, &data); + } } } diff --git a/crates/engine-paint/src/cmd/fill_cmds.rs b/crates/engine-paint/src/cmd/fill_cmds.rs index 973b5d378b..79b4928700 100644 --- a/crates/engine-paint/src/cmd/fill_cmds.rs +++ b/crates/engine-paint/src/cmd/fill_cmds.rs @@ -187,12 +187,13 @@ fn indexed_table(doc: &photocraft_doc::Document, p: &Value) -> Option [f32; 4] { - let mut px = [c]; + let mut px = [[c[0], c[1], c[2], 1.0]]; photocraft_algo::quantize::quantize(&mut px, 1, &table.colors, photocraft_algo::quantize::Dither::None, 0.0, table.transparent.map(usize::from)); - let [c] = px; - c + let [[r, g, b, _]] = px; + [r, g, b, c[3]] } /// A fill colour: the inks it was picked as when it was picked as CMYK (#4196), else plain sRGB. diff --git a/crates/engine-paint/src/cmd/fill_cmds/indexed_palette_tests.rs b/crates/engine-paint/src/cmd/fill_cmds/indexed_palette_tests.rs index 0829874ec2..3f4b2e665c 100644 --- a/crates/engine-paint/src/cmd/fill_cmds/indexed_palette_tests.rs +++ b/crates/engine-paint/src/cmd/fill_cmds/indexed_palette_tests.rs @@ -178,3 +178,47 @@ fn other_colour_modes_still_blend_and_keep_the_colour() { let c = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().rgba(10, 10); assert_eq!([0, 1, 2].map(|i| (c[i] * 255.0).round() as u8), [200, 30, 30], "an RGB fill keeps its colour"); } + +#[test] +fn dissolve_takes_a_partly_transparent_colour_alpha_as_its_odds() { + // A table with a transparent entry, which maps a mostly transparent colour to transparency. + let share = |alpha: f64| { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 200, "height": 100, "background": "transparent"})).unwrap(); + s.execute("edit.fill", json!({"color": "#0000ff"})).unwrap(); + s.execute("select.rect", json!({"x": 0, "y": 0, "width": 10, "height": 10, "antiAlias": false})).unwrap(); + s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); + s.execute("select.rect", json!({"x": 190, "y": 0, "width": 10, "height": 10, "antiAlias": false})).unwrap(); + s.execute("edit.clear", json!({})).unwrap(); + s.execute("image.mode.indexedColor", json!({"palette": "exact", "dither": "none", "transparency": true})).unwrap(); + assert!(s.active().unwrap().doc.color_table.as_ref().is_some_and(|t| t.transparent.is_some()), "the table has a transparent entry"); + select(&mut s); + s.execute(CMD, json!({"contents": "color", "color": [1.0, 0.0, 0.0, alpha], "mode": "dissolve"})).unwrap(); + let inside: Vec<[u8; 3]> = pixels(&s).into_iter().filter(|((x, y), _)| SEL.contains(*x, *y)).map(|(_, c)| c).collect(); + assert!(inside.iter().all(|c| *c == RED || *c == BLUE), "alpha {alpha}: every pixel is the snapped fill or untouched"); + inside.iter().filter(|c| **c == RED).count() as f64 / inside.len() as f64 + }; + let (quarter, three_quarters) = (share(0.25), share(0.75)); + assert!(quarter > 0.1 && quarter < 0.4, "alpha 0.25 paints about a quarter, got {quarter}"); + assert!(three_quarters > 0.6 && three_quarters < 0.9, "alpha 0.75 paints about three quarters, got {three_quarters}"); +} + +#[test] +fn a_whole_canvas_fill_keeps_its_shared_solid_tiles() { + // Two colours in the table, then black over the white 512 × 512 canvas: every 256 px tile is + // the fill's one shared tile, and the table step doesn't copy it per tile. + let mut s = crate::session(); + s.execute("file.new", json!({"width": 512, "height": 512, "background": "white"})).unwrap(); + s.execute("select.rect", json!({"x": 0, "y": 0, "width": 10, "height": 10, "antiAlias": false})).unwrap(); + s.execute("edit.fill", json!({"color": "#000000"})).unwrap(); + s.execute("image.mode.indexedColor", json!({"palette": "exact", "dither": "none", "transparency": false})).unwrap(); + s.execute("select.deselect", json!({})).unwrap(); + // The shared tile is for a layer whose transparency may change, not the Background. + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute(CMD, json!({"contents": "black"})).unwrap(); + let st = s.active().unwrap(); + let surf = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap(); + let tiles: Vec<_> = surf.tiles().map(|(_, t)| t).collect(); + assert_eq!(tiles.len(), 4); + assert!(tiles.iter().all(|t| std::sync::Arc::ptr_eq(t, tiles[0])), "the filled tiles share one buffer"); +}