From 488843d0991bb13a425b155bcc1f7e5c2b334cf3 Mon Sep 17 00:00:00 2001 From: x-labb <154824493+x-labb@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:34:59 +0100 Subject: [PATCH 1/2] fix(engine): the Spectrum and system Color Table presets run in the reference order (#14344) Spectrum ran red to magenta, System (Mac OS) black first with white at index 215, and System (Windows) had white at index 19 and ended with a gray. The reference application's tables run magenta to red, white down through the web cube to black last, and black through the static colours to white last. Only the order of the tables changes; Indexed Color conversion with the system palettes keeps each pixel's colour. --- crates/algo/src/quantize.rs | 17 ++- crates/engine-core/src/cmd/mode_cmds.rs | 6 +- .../mode_cmds/color_table_presets_tests.rs | 103 ++++++++++++++++++ 3 files changed, 119 insertions(+), 7 deletions(-) create mode 100644 crates/engine-core/src/cmd/mode_cmds/color_table_presets_tests.rs diff --git a/crates/algo/src/quantize.rs b/crates/algo/src/quantize.rs index 1ddb62c8e5..07409f727e 100644 --- a/crates/algo/src/quantize.rs +++ b/crates/algo/src/quantize.rs @@ -89,9 +89,11 @@ pub fn web_palette() -> Vec { v } -/// Classic Mac OS system palette: the web cube plus 10-step red, green, blue and gray ramps. +/// Classic Mac OS system palette: the web cube plus 10-step red, green, blue and gray ramps. The +/// table runs from white down through the cube (FFFFFF, FFFFCC, FFFF99, …), then the four ramps, +/// and ends with black (#14344). pub fn mac_palette() -> Vec { - let mut v = web_palette(); + let mut v: Vec = web_palette().into_iter().skip(1).rev().collect(); // Ramps use the values between cube steps (17-unit spacing, skipping multiples of 51). let steps: Vec = (1..=15u8).map(|k| k * 17).filter(|x| x % 51 != 0).collect(); for &s in steps.iter().rev() { @@ -106,11 +108,13 @@ pub fn mac_palette() -> Vec { for &s in steps.iter().rev() { v.push([s, s, s]); } + v.push([0, 0, 0]); v.truncate(256); v } -/// Windows default palette: the 20 static system colours plus the web cube and a gray ramp. +/// Windows default palette: the 20 static system colours plus the web cube and a gray ramp. The +/// table starts with black and the static colours and ends with white (#14344). pub fn windows_palette() -> Vec { let mut v: Vec = vec![ [0, 0, 0], @@ -132,20 +136,21 @@ pub fn windows_palette() -> Vec { [0, 0, 255], [255, 0, 255], [0, 255, 255], - [255, 255, 255], ]; + const WHITE: Rgb8 = [255, 255, 255]; for c in web_palette() { - if !v.contains(&c) { + if c != WHITE && !v.contains(&c) { v.push(c); } } let mut g = 8u8; - while v.len() < 256 { + while v.len() < 255 { if !v.contains(&[g, g, g]) { v.push([g, g, g]); } g = g.wrapping_add(12); } + v.push(WHITE); v } diff --git a/crates/engine-core/src/cmd/mode_cmds.rs b/crates/engine-core/src/cmd/mode_cmds.rs index 1523b965f5..64c074d1dd 100644 --- a/crates/engine-core/src/cmd/mode_cmds.rs +++ b/crates/engine-core/src/cmd/mode_cmds.rs @@ -487,8 +487,9 @@ pub fn preset_table(name: &str) -> Option> { "grayscale" => ramp(&|t| [t; 3]), // Black → red → yellow → white, the incandescence scale. "blackBody" => ramp(&|t| [(t * 3.0).min(1.0), (t * 3.0 - 1.0).clamp(0.0, 1.0), (t * 3.0 - 2.0).clamp(0.0, 1.0)]), + // From magenta at index 0 down to red at index 255 (#14344). "spectrum" => ramp(&|t| { - let h = t * 300.0 / 60.0; + let h = (1.0 - t) * 300.0 / 60.0; let x = 1.0 - (h % 2.0 - 1.0).abs(); match h as i32 { 0 => [1.0, x, 0.0], @@ -900,3 +901,6 @@ mod color_table_index_tests; #[cfg(test)] #[path = "rotate_canvas_tiny_angle_tests.rs"] mod rotate_canvas_tiny_angle_tests; + +#[cfg(test)] +mod color_table_presets_tests; diff --git a/crates/engine-core/src/cmd/mode_cmds/color_table_presets_tests.rs b/crates/engine-core/src/cmd/mode_cmds/color_table_presets_tests.rs new file mode 100644 index 0000000000..af3b2555d7 --- /dev/null +++ b/crates/engine-core/src/cmd/mode_cmds/color_table_presets_tests.rs @@ -0,0 +1,103 @@ +//! #14344: the Spectrum table runs from magenta (index 0) to red (index 255), System (Mac OS) +//! from white down through the web cube to black last, System (Windows) from black through the +//! static colours to white last. Indexed Color conversion with the system palettes still gives +//! each pixel the same colour; only the order of the table changes. + +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]; + +/// Four solid bars red, green, blue and yellow, converted to Indexed Color with `palette`, no +/// dither and no transparency. +fn bars(palette: &str) -> 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": palette, "forced": "none", "dither": "none", "transparency": false})).unwrap(); + s +} + +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 unique(table: &[[u8; 3]]) -> bool { + let mut sorted = table.to_vec(); + sorted.sort_unstable(); + sorted.dedup(); + sorted.len() == table.len() +} + +#[test] +fn spectrum_runs_from_magenta_to_red() { + let t = preset_table("spectrum").unwrap(); + assert_eq!(t.len(), 256); + let [first, last] = [t[0], t[255]]; + assert!(first[0] > first[1] && first[2] > first[1], "index 0 is magenta-ish: {first:?}"); + assert!(last[0] > last[1] && last[0] > last[2], "index 255 is red: {last:?}"); +} + +#[test] +fn applying_spectrum_makes_the_bars_magenta_ish() { + let mut s = bars("exact"); + s.execute("image.mode.colorTable", json!({"table": "spectrum"})).unwrap(); + for c in bar_colours(&s) { + assert!(c[0] > c[1] && c[2] > c[1], "{c:?}"); + } +} + +#[test] +fn system_mac_runs_from_white_to_black() { + let t = preset_table("systemMac").unwrap(); + assert_eq!(t.len(), 256); + assert!(unique(&t)); + assert_eq!(&t[..4], &[[255, 255, 255], [255, 255, 204], [255, 255, 153], [255, 255, 102]]); + assert_eq!(t[255], [0, 0, 0]); +} + +#[test] +fn system_windows_runs_from_black_to_white() { + let t = preset_table("systemWindows").unwrap(); + assert_eq!(t.len(), 256); + assert!(unique(&t)); + assert_eq!(&t[..5], &[[0, 0, 0], [128, 0, 0], [0, 128, 0], [128, 128, 0], [0, 0, 128]]); + assert_eq!(t[255], [255, 255, 255]); +} + +#[test] +fn the_system_palettes_keep_their_colours() { + // Only the order changed: the same colours as the cube plus the ramps (Mac OS) and the static + // colours plus the cube (Windows). + let cube = photocraft_algo::quantize::web_palette(); + let mac = preset_table("systemMac").unwrap(); + assert!(cube.iter().all(|c| mac.contains(c)), "the whole web cube"); + assert!([[17, 0, 0], [0, 238, 0], [0, 0, 119], [34, 34, 34]].iter().all(|c| mac.contains(c)), "the ramps"); + let windows = preset_table("systemWindows").unwrap(); + assert!(cube.iter().all(|c| windows.contains(c)), "the whole web cube"); + assert!([[192, 220, 192], [166, 202, 240], [255, 251, 240], [160, 160, 164]].iter().all(|c| windows.contains(c)), "the static colours"); +} + +#[test] +fn indexed_conversion_with_the_system_palettes_gives_each_pixel_its_colour() { + for palette in ["systemMac", "systemWindows"] { + let s = bars(palette); + assert_eq!(bar_colours(&s), [RED, GREEN, BLUE, YELLOW], "{palette}"); + } +} From 2aa6e065713c2f9a298d774a06d7322cc3655080 Mon Sep 17 00:00:00 2001 From: x-labb <154824493+x-labb@users.noreply.github.com> Date: Wed, 25 Nov 2026 16:56:45 +0100 Subject: [PATCH 2/2] Color Table preset tests: start from the installed command registry (#14344) --- .../engine-core/src/cmd/mode_cmds/color_table_presets_tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/engine-core/src/cmd/mode_cmds/color_table_presets_tests.rs b/crates/engine-core/src/cmd/mode_cmds/color_table_presets_tests.rs index af3b2555d7..ff5c2f0853 100644 --- a/crates/engine-core/src/cmd/mode_cmds/color_table_presets_tests.rs +++ b/crates/engine-core/src/cmd/mode_cmds/color_table_presets_tests.rs @@ -13,7 +13,7 @@ const YELLOW: [f32; 4] = [1.0, 1.0, 0.0, 1.0]; /// Four solid bars red, green, blue and yellow, converted to Indexed Color with `palette`, no /// dither and no transparency. fn bars(palette: &str) -> Session { - let mut s = Session::new(); + let mut s = crate::session(); s.execute("file.new", json!({"width": 8, "height": 4, "background": "white"})).unwrap(); s.edit("setup", |doc, active| { let b = doc.bounds();