diff --git a/crates/ui-egui/src/dialogs/save_for_web.rs b/crates/ui-egui/src/dialogs/save_for_web.rs index 4ba055b558..6256881398 100644 --- a/crates/ui-egui/src/dialogs/save_for_web.rs +++ b/crates/ui-egui/src/dialogs/save_for_web.rs @@ -1479,17 +1479,44 @@ fn take_shift(d: &mut Dialog) -> Result<(), String> { /// Shift the source color `source` to `to` (`colorTable.shifted`), wherever it is in the image; it /// is no longer web-shifted. Picking the source color itself takes the shift away. The color /// becomes the selection. Nothing changes when either is not a color, and a new source when 256 -/// colors are already shifted is an error that changes nothing. +/// colors are already shifted is an error that changes nothing. Entries the engine drops (a color +/// shifted to itself, a deleted color) do not count, and a new shift writes the map without them. pub(super) fn shift_color(d: &mut Dialog, source: &str, to: &str) -> Result<(), String> { let hex = |h: &str| vectorcraft_color::Color::from_hex(h).map(|c| c.to_hex()); let (Some(source), Some(to)) = (hex(source), hex(to)) else { return Ok(()) }; + let deleted: Vec = d + .fields + .get("colorTable") + .and_then(|t| t.get("deleted")) + .and_then(Value::as_array) + .into_iter() + .flatten() + .filter_map(Value::as_str) + .map(String::from) + .collect(); let map = shifts(d); - if to != source && map.len() >= webexport::MAX_TABLE && !map.keys().any(|k| same_color(k, &source)) { + // Entries are dropped only from a map the engine accepts (every key and value a color, no two + // keys naming one color); any other map is kept whole for the engine to refuse. + let mut keys: Vec = Vec::new(); + let valid = map.iter().all(|(k, v)| match (table_rgb(k), v.as_str().and_then(table_rgb)) { + (Some(k), Some(_)) if !keys.contains(&k) => { + keys.push(k); + true + } + _ => false, + }); + let live = |k: &str, v: &Value| { + let (true, Some(key), Some(to)) = (valid, table_rgb(k), v.as_str().and_then(table_rgb)) else { return true }; + to != key && !deleted.iter().any(|x| table_rgb(x).is_some_and(|x| x == key)) + }; + let count = map.iter().filter(|(k, v)| live(k, v)).count(); + if to != source && count >= webexport::MAX_TABLE && !map.iter().any(|(k, v)| live(k, v) && same_color(k, &source)) { return Err(webexport::too_many("shifted")); } unshift(d, &source); if to != source { let mut map = shifts(d); + map.retain(|k, v| live(k, v)); map.insert(source.clone(), json!(to)); set_table(d, "shifted", Value::Object(map)); } diff --git a/crates/ui-egui/src/dialogs/tests_save_for_web.rs b/crates/ui-egui/src/dialogs/tests_save_for_web.rs index 1c8c6f17be..c8bbd9526b 100644 --- a/crates/ui-egui/src/dialogs/tests_save_for_web.rs +++ b/crates/ui-egui/src/dialogs/tests_save_for_web.rs @@ -921,6 +921,26 @@ fn colour_table_shift_holds_at_most_256_colors() { assert_eq!(d.fields["colorTable"]["shifted"]["#123456"], "#000000"); } +/// Entries the engine drops (a color shifted to itself, a deleted color) do not count toward the +/// 256 shifts, and a new shift writes the map without them. +#[test] +fn colour_table_shift_skips_entries_the_engine_drops() { + let mut full: serde_json::Map = (0..255).map(|i| (format!("#{i:06x}"), json!(format!("#{i:06x}")))).collect(); + full.insert("#abcdef".into(), json!("#000000")); + let mut d = Dialog::new(save_for_web::KIND, json!({"colorTable": {"shifted": full, "deleted": ["#ABCDEF"]}})); + save_for_web::shift_color(&mut d, "#123456", "#000000").unwrap(); + assert_eq!(d.fields["colorTable"]["shifted"], json!({"#123456": "#000000"})); + // A map the engine refuses (a value that is not a color, two keys naming one color) is kept + // whole for the engine to refuse. + for shifted in [json!({"nope": "NOPE"}), json!({"#abcdef": "nope"}), json!({"#f00": "#f00", "#FF0000": "#00ff00"})] { + let mut d = Dialog::new(save_for_web::KIND, json!({"colorTable": {"shifted": shifted.clone(), "deleted": ["#abcdef"]}})); + save_for_web::shift_color(&mut d, "#123456", "#000000").unwrap(); + let mut want = shifted.as_object().cloned().unwrap_or_default(); + want.insert("#123456".into(), json!("#000000")); + assert_eq!(d.fields["colorTable"]["shifted"], serde_json::Value::Object(want), "{shifted}"); + } +} + /// One frame of `app` in `ctx`, 50 ms after the last. fn tick(app: &mut VectorcraftApp, ctx: &egui::Context, events: Vec) { let time = ctx.input(|i| i.time) + 0.05; diff --git a/docs/mcp.md b/docs/mcp.md index 32ae51c7cd..641f705089 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -10568,7 +10568,7 @@ included), and two keys that name one color are errors. Presets and `webExport.s opaque swatch opens the Color Picker on the color shown (on the source color for a color already shifted; an agent sets `__editColor: "#source"`). Its OK shifts the color (picking the source color takes the shift away), and Cancel leaves the table as it was. An OK that would shift a 257th color changes nothing, and the status bar shows the -limit. A key written another way (`#F00`) still names its color, so a new shift or Unshift replaces it. A selected +limit; a key shifted to itself or a deleted color does not count, and a new shift writes `shifted` without them. A key written another way (`#F00`) still names its color, so a new shift or Unshift replaces it. A selected color (`__color`, `__colors`) or list entry written another way also names its table color: Lock, Transparent, Web Shift and Delete Color act on it and write it as the table does, and a `#rrggbbaa` spelling names no table color. Done or Save right after the OK keeps the shift. A shifted swatch shows the source color at its upper left and the new color at its lower