Color Table: pixels past a shrunk table render black

An index past the new table's end took the new table's nearest colour;
it now renders 0,0,0, as in the reference application (#14342).
Duplicate entries still map by nearest colour until the document keeps
each pixel's index.
This commit is contained in:
Sven Lafebre committed 2026-11-15 10:49:54 +01:00
1 parent c162cb6d63
commit cb7f9770f5
2 files changed
+78 -1

No files matched your search

+5 -1
View File
@@ -488,7 +488,8 @@ fn color_table(s: &mut Session, p: &Value) -> Result<Value> {
} else {
c[3]
};
let nc = if i < table.colors.len() { table.rgb(i) } else { table.rgb(table.nearest([c[0], c[1], c[2]])) };
// An index past a shrunk table has no colour: black, as in Photoshop (#14342).
let nc = table.rgb(i);
let mut enc = [0.0f32; 8];
let m = photocraft_raster::from_rgba_into(&fmt, [nc[0], nc[1], nc[2], alpha], &mut enc);
q.copy_from_slice(&enc[..m]);
@@ -830,3 +831,6 @@ pub fn display_document(doc: &Document) -> Option<Document> {
#[cfg(test)]
mod tests;
#[cfg(test)]
mod color_table_index_tests;
@@ -0,0 +1,73 @@
//! Image › Mode › Color Table: pixels past a shrunk table turn black, and a table without
//! duplicates recolours as before (#14342).
use super::*;
const RED: [f32; 4] = [1.0, 0.0, 0.0, 1.0];
const GREEN: [f32; 4] = [0.0, 1.0, 0.0, 1.0];
const BLUE: [f32; 4] = [0.0, 0.0, 1.0, 1.0];
const YELLOW: [f32; 4] = [1.0, 1.0, 0.0, 1.0];
/// The epic's test document, scaled down: four solid bars red, green, blue and yellow, converted
/// to Indexed Color with an Exact palette, no dither and no transparency.
fn bars() -> Session {
let mut s = Session::new();
s.execute("file.new", json!({"width": 8, "height": 4, "background": "white"})).unwrap();
s.edit("setup", |doc, active| {
let b = doc.bounds();
let surf = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap();
let fmt = surf.format();
let mut data = Vec::new();
for _ in b.y0..b.y1 {
for x in b.x0..b.x1 {
data.extend(photocraft_raster::from_rgba(&fmt, [RED, GREEN, BLUE, YELLOW][x as usize / 2]));
}
}
surf.write_region(b, &data);
Ok(())
})
.unwrap();
s.execute("image.mode.indexedColor", json!({"palette": "exact", "forced": "none", "dither": "none", "transparency": false})).unwrap();
s
}
fn table(s: &Session) -> ColorTable {
s.active().unwrap().doc.color_table.clone().unwrap()
}
/// The colour of each bar (one pixel per bar, from the middle row).
fn bar_colours(s: &Session) -> [[f32; 4]; 4] {
let surf = s.active().unwrap().doc.layers[0].surface().unwrap();
[0, 2, 4, 6].map(|x| surf.rgba(x, 2))
}
fn index_of(t: &ColorTable, c: [f32; 4]) -> usize {
t.colors.iter().position(|e| e.map(|v| f32::from(v) / 255.0) == [c[0], c[1], c[2]]).unwrap()
}
#[test]
fn pixels_past_a_shrunk_table_turn_black() {
let mut s = bars();
let before = (table(&s), bar_colours(&s));
let indices = [RED, GREEN, BLUE, YELLOW].map(|c| index_of(&before.0, c));
assert_eq!(before.0.colors.len(), 4);
let new = [[0.0, 1.0, 1.0, 1.0], [1.0, 0.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0]];
s.execute("image.mode.colorTable", json!({"colors": ["#00ffff", "#ff00ff", "#ffffff"]})).unwrap();
let want = indices.map(|i| new.get(i).copied().unwrap_or([0.0, 0.0, 0.0, 1.0]));
assert_eq!(bar_colours(&s), want, "indices {indices:?}: the bar past the table is black, the others take their entry");
assert_eq!(s.active().unwrap().history.undo_label(), Some("Color Table"));
s.execute("edit.undo", json!({})).unwrap();
assert_eq!((table(&s), bar_colours(&s)), before);
}
/// Regression check for "a table without duplicates recolours exactly as today": passes on main too.
#[test]
fn editing_one_entry_recolours_only_its_pixels() {
let mut s = bars();
let before = bar_colours(&s);
let blue = index_of(&table(&s), BLUE);
s.execute("image.mode.colorTable", json!({"entries": {blue.to_string(): "#ff00ff"}})).unwrap();
assert_eq!(bar_colours(&s), [RED, GREEN, [1.0, 0.0, 1.0, 1.0], YELLOW]);
s.execute("edit.undo", json!({})).unwrap();
assert_eq!(bar_colours(&s), before);
}