Skip to content

VectorCraft: An open-source, vector art tool written by scratch in pure Rust

Recolor: a new color group keeps Global Adjust colors in Limit to Library

dotCooCoo1c6720a85c43
diff --git a/crates/ui-egui/src/dialogs/recolor.rs b/crates/ui-egui/src/dialogs/recolor.rs
index 1f319ca96..70e69a78c 100644
--- a/crates/ui-egui/src/dialogs/recolor.rs
+++ b/crates/ui-egui/src/dialogs/recolor.rs
@@ -353,12 +353,23 @@ fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result<Value, String> {
let mut d = d.clone();
sync(app, &mut d);
if d.bool("__newGroup") {
- // The new colors with the Global Adjust, as `recolor.apply` gives them to a group.
+ // The new colors with the Global Adjust, snapped to Limit to Library, as `recolor.apply`
+ // gives them to a group.
let adjust = global_adjust(adjust_values(&d));
+ let limit = d.str("limitTo");
+ let palette = Some(limit).filter(|l| !l.is_empty()).and_then(|l| swatchlib::limit_palette(&app.session, &l));
let colors: Vec<Value> = rows(&d)
.into_iter()
.filter(|r| !excluded(r))
- .map(|r| to_color(&r).filter(|_| !adjust.is_identity()).map_or_else(|| r["to"].clone(), |c| key(&adjust.apply(c))))
+ .map(|r| {
+ to_color(&r).filter(|_| !adjust.is_identity()).map_or_else(
+ || r["to"].clone(),
+ |c| {
+ let c = adjust.apply(c);
+ key(&palette.as_ref().map_or(c, |p| p.nearest(c)))
+ },
+ )
+ })
.collect();
return form::commit_preview(app, "swatch.newGroup", json!({"name": d.str("groupName"), "colors": colors, "keepModel": true}));
}
diff --git a/crates/ui-egui/src/tests/recolor.rs b/crates/ui-egui/src/tests/recolor.rs
index 1786b7a61..0e7fe849b 100644
--- a/crates/ui-egui/src/tests/recolor.rs
+++ b/crates/ui-egui/src/tests/recolor.rs
@@ -642,6 +642,24 @@ fn global_adjust_changes_every_color_at_once() {
}
}
+/// Colors opened alone with Limit to Library: OK saves the new colors with the Global Adjust as a
+/// new color group, each snapped to the library again, as `recolor.apply` gives them to a group.
+#[test]
+fn global_adjust_on_a_new_group_keeps_its_colors_in_the_library() {
+ let mut app = app();
+ app.run("ui.recolorDialog", json!({"colors": ["#123456", "#abcdef"], "library": ""})).unwrap();
+ assert_eq!(field(&app, "limitTo"), "web-safe-216");
+ let web = |k: &Value| vectorcraft_engine::cmd::color_value(k).unwrap().to_rgb().iter().all(|v| ((v * 5.0).round() - v * 5.0).abs() < 1e-3);
+ let rows: Vec<Value> = field(&app, "rows").as_array().unwrap().iter().map(|r| r["to"].clone()).collect();
+ assert!(rows.iter().all(web), "the rows open snapped: {rows:?}");
+ set(&mut app, "globalAdjust", json!({"brightness": -60}));
+ dialogs::confirm(&mut app).unwrap();
+ let keys = group_keys(&app, "Color Group");
+ assert_eq!(keys.len(), 2);
+ assert_ne!(keys, rows, "the Global Adjust changed the colors");
+ assert!(keys.iter().all(web), "every group color is a library color: {keys:?}");
+}
+
/// The Assign tab's per-row hex fields are named by their row, not all "Color": a screen reader can
/// tell the second from the first.
#[test]