From e674d8b7c2765b6cdc5713e6e3b7b67ef1c526fc Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 21:23:50 -0800 Subject: [PATCH] Document Setup: an unchanged Color Picker OK keeps a grid color's own model The Color Picker opened from a grid color's box wrote its color back to Document Setup's gridColors as a hex code. Since #8630 Document Setup opens the picker on the document's own grid colors, so an unchanged OK on a Lab or CMYK grid color turned it into display RGB, and Document Setup's OK stored that RGB color. The picker now writes a list entry with grid_color_entry, which keeps a color in its own model when its hex code would read back as another color. A scalar color field still gets a hex code. --- crates/ui-egui/src/dialogs/color_picker.rs | 8 ++++--- crates/ui-egui/src/dialogs/document_setup.rs | 25 +++++++++++++++++++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/crates/ui-egui/src/dialogs/color_picker.rs b/crates/ui-egui/src/dialogs/color_picker.rs index 52a6086816..3c11302d2e 100644 --- a/crates/ui-egui/src/dialogs/color_picker.rs +++ b/crates/ui-egui/src/dialogs/color_picker.rs @@ -793,10 +793,10 @@ fn swatch_list(app: &VectorcraftApp, ui: &mut Ui, d: &mut Dialog, current: &Colo fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result { if let Some(field) = d.fields.get(COLOUR_FIELD).and_then(Value::as_str) { let mut parent = super::parent(d).ok_or("the dialog it was opened over is no longer open")?; - let hex = json!(picked(d).color.to_hex()); + let colour = picked(d).color; match d.fields.get(COLOUR_INDEX) { None => { - parent.fields.insert(field.to_string(), hex); + parent.fields.insert(field.to_string(), json!(colour.to_hex())); } Some(index) => { if let Some(list) = d.fields.get(COLOUR_LIST) { @@ -807,7 +807,9 @@ fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result { .and_then(|i| usize::try_from(i).ok()) .and_then(|i| parent.fields.get_mut(field)?.as_array_mut()?.get_mut(i)) .ok_or("no such color")?; - *entry = hex; + // The entry keeps the color in its own model when its hex code would read back as + // another color: a Lab or CMYK color the picker opened on and its OK left unchanged. + *entry = super::document_setup::grid_color_entry(&colour); } } // The dialog previews the new colour when it comes back: end this picker's interaction, as diff --git a/crates/ui-egui/src/dialogs/document_setup.rs b/crates/ui-egui/src/dialogs/document_setup.rs index 575603e926..78fc498ba7 100644 --- a/crates/ui-egui/src/dialogs/document_setup.rs +++ b/crates/ui-egui/src/dialogs/document_setup.rs @@ -232,7 +232,7 @@ fn pending_grid_pair(fields: &Map) -> Option> { /// Color `c` as an entry of the dialog's `gridColors` that `document.setup` reads back as `c`: its /// hex code when that reads back as `c`, else the color in its own model (a Lab or CMYK color, or /// an RGB color whose hex code is converted to display RGB or rounded). -fn grid_color_entry(c: &vectorcraft_color::Color) -> Value { +pub(super) fn grid_color_entry(c: &vectorcraft_color::Color) -> Value { let hex = json!(c.to_hex()); if vectorcraft_engine::cmd::color_value(&hex) == Some(*c) { hex } else { serde_json::to_value(c).unwrap_or(hex) } } @@ -919,6 +919,29 @@ mod tests { } } + /// The Color Picker's OK on a grid color it opened on, unchanged, keeps that color in its own + /// model: a Lab or CMYK color stays Lab or CMYK through Document Setup's OK, and a hex color + /// stays that hex color. The other color stays as it was. + #[test] + fn an_unchanged_grid_color_picker_ok_keeps_the_colors_own_model() { + for (index, grid) in + [(0, json!(["lab 55 60 40", "#cccccc"])), (1, json!(["#ffffff", "cmyk 0 50 100 0"])), (1, json!(["lab 55 60 40", "#123456"]))] + { + let (mut app, ctx) = transparency_tab_on(grid.clone()); + let before = app.session.active().unwrap().doc.setup.grid_colors; + click_grid_box(&mut app, &ctx, index); + let d = app.ui.dialog.as_ref().unwrap(); + assert_eq!((d.kind.as_str(), d.fields.get("color").and_then(vectorcraft_engine::cmd::color_value)), ("colorPicker", Some(before[index]))); + crate::dialogs::confirm(&mut app).unwrap(); + let d = app.ui.dialog.as_ref().unwrap(); + assert_eq!((d.kind.as_str(), dialog_grid(d)), (super::KIND, before.to_vec()), "{grid}"); + crate::dialogs::confirm(&mut app).unwrap(); + assert!(app.ui.dialog.is_none()); + assert_eq!(app.session.active().unwrap().doc.setup.grid_colors, before, "{grid}"); + assert_eq!(undo_steps(&app), 1, "{grid}: only the document.setup that opened the tab on the grid"); + } + } + /// When Grid Colors holds a preset name (in any letter case) or a list that is not two colors, /// the boxes and hex fields show the preset's pair or the document's, a box opens the Color /// Picker on its color, and the picker's OK sets only that entry of a two-color list.