Merge pull request #8147 from dotCooCoo/feat/color-guide-save-selected-cells

Color Guide: Save Color Group to Swatches saves the selected cells as the group
This commit is contained in:
BFlat
2026-11-16 23:05:34 +07:00
committed by GitHub
2 changed files with 177 additions and 10 deletions
+176 -10
View File
@@ -6,7 +6,9 @@
//! ("Set base color to the current color") takes the current color, such as a cell applied from
//! the guide or the selected artwork's color. A click on a cell applies it to the active proxy,
//! without changing the base, and selects it; Shift- or Cmd/Ctrl-click adds
//! cells to the selection that Save Colors as Swatches saves. Limit to Library (the library button,
//! cells to the selection that Save Colors as Swatches saves as swatches and Save Color Group to
//! Swatches saves as one group (without a selection, the group holds the harmony colors). The
//! selection lasts until the guide changes. Limit to Library (the library button,
//! `ui.colorGuideLimit`) snaps every colour to a swatch library or the document's swatches; Edit or
//! Apply Colors opens Recolor Artwork with the harmony colours, limited the same way.
@@ -50,6 +52,33 @@ fn clear_selection(ctx: &egui::Context) {
set_pstate(ctx, "cg-sel", Vec::<(usize, usize)>::new());
}
/// The guide a cell selection was made in: its base, options, harmony rule, library limit and the
/// colors of its grid (which change when the document's swatches limit the guide and are edited).
#[derive(Clone, PartialEq)]
struct SelKey {
base: Color,
opts: GuideOptions,
rule: usize,
limit: String,
grid: Vec<Vec<Color>>,
}
fn sel_key(app: &VectorcraftApp, ctx: &egui::Context, g: &Guide) -> SelKey {
SelKey {
base: base_color(app),
opts: app.ui.color_guide,
rule: pstate(ctx, "cg-harmony"),
limit: app.ui.color_guide_limit.clone(),
grid: g.grid.clone(),
}
}
/// The selected cells while they belong to the guide `g` shown now; none once the base, the
/// options, the harmony rule, the library limit or the grid's colors changed since they were picked.
fn live_selection(app: &VectorcraftApp, ctx: &egui::Context, g: &Guide) -> Vec<(usize, usize)> {
if pstate::<Option<SelKey>>(ctx, "cg-sel-key") == Some(sel_key(app, ctx, g)) { selected(ctx) } else { Vec::new() }
}
/// What the panel's guide is made from: with a library limit, snapping every cell is too slow to
/// redo each frame, so the guide is cached until one of these changes.
#[derive(Clone, PartialEq)]
@@ -128,12 +157,28 @@ fn edit_or_apply(app: &mut VectorcraftApp, g: &Guide) {
/// Save the selected cells' colors as swatches (one undo step), each in its own model.
fn save_selected(app: &mut VectorcraftApp, ctx: &egui::Context) {
let g = guide(app, ctx);
let colors: Vec<Value> = selected(ctx).into_iter().filter_map(|(r, c)| g.grid.get(r)?.get(c)).map(color_json).collect();
let colors: Vec<Value> = live_selection(app, ctx, &g).into_iter().filter_map(|(r, c)| g.grid.get(r)?.get(c)).map(color_json).collect();
if !colors.is_empty() {
app.run("swatch.new", json!({ "colors": colors, "keepModel": true })).ok();
}
}
/// Save Color Group to Swatches: the selected cells as one swatch group, in grid order (row by
/// row, left to right), each cell once; with no cell of the shown guide selected, the harmony
/// colors. The group is named after the harmony rule, saved in one undo step, and each color
/// keeps its own model.
fn save_group(app: &mut VectorcraftApp, ctx: &egui::Context) {
let g = guide(app, ctx);
let mut cells = live_selection(app, ctx, &g);
cells.sort_unstable();
cells.dedup();
let mut colors: Vec<Value> = cells.into_iter().filter_map(|(r, c)| g.grid.get(r)?.get(c)).map(color_json).collect();
if colors.is_empty() {
colors = g.colors.iter().map(color_json).collect();
}
app.run("swatch.newGroup", json!({"name": rule(ctx).label(), "colors": colors, "keepModel": true})).ok();
}
pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
let t = crate::theme::Tokens::get(ui.ctx());
let ctx = ui.ctx().clone();
@@ -141,13 +186,15 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
// leaves it as it is.
let first = base_color(app);
let set = *app.ui.color_guide_base.get_or_insert(first);
// Selected cells belong to the guide they were picked from: a new base clears them.
if pstate::<Option<Color>>(&ctx, "cg-sel-base") != Some(set) {
set_pstate(&ctx, "cg-sel-base", Some(set));
clear_selection(&ctx);
}
let h = rule(&ctx);
let g = guide(app, &ctx);
// Selected cells belong to the guide they were picked from: a new base, new options, another
// harmony rule, another library limit or new grid colors clear them.
let key = SelKey { base: set, ..sel_key(app, &ctx, &g) };
if pstate::<Option<SelKey>>(&ctx, "cg-sel-key").as_ref() != Some(&key) {
set_pstate(&ctx, "cg-sel-key", Some(key));
clear_selection(&ctx);
}
let base = g.colors[0];
let mut chosen: Option<Color> = None;
ui.horizontal(|ui| {
@@ -245,8 +292,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
save_selected(app, &ctx);
}
if widgets::icon_button(ui, "dc-folder", tl!("Save color group to Swatch panel"), false, 24.0).clicked() {
let cs: Vec<_> = g.colors.iter().map(color_json).collect();
app.run("swatch.newGroup", json!({"name": h.label(), "colors": cs, "keepModel": true})).ok();
save_group(app, &ctx);
}
});
if let Some(c) = chosen {
@@ -267,7 +313,7 @@ pub fn menu(app: &mut VectorcraftApp, ui: &mut Ui) {
}
ui.separator();
let ctx = ui.ctx().clone();
if menu_item(ui, tl!("Save Colors as Swatches"), !selected(&ctx).is_empty(), false) {
if menu_item(ui, tl!("Save Colors as Swatches"), !live_selection(app, &ctx, &guide(app, &ctx)).is_empty(), false) {
save_selected(app, &ctx);
}
if menu_item(ui, tl!("Color Guide Options…"), true, false) {
@@ -420,6 +466,126 @@ mod tests {
assert_eq!(saved, [g.grid[0][0], g.grid[0][6]]);
}
/// The colors of the document's newest swatch group, and the group's name.
fn last_group(app: &VectorcraftApp) -> (String, Vec<Color>) {
let g = app.session.doc().unwrap().doc.swatch_groups.last().cloned().expect("a swatch group");
(g.name, g.swatches.iter().filter_map(|w| w.paint.color()).collect())
}
/// With cells selected, Save Color Group to Swatches saves exactly those cells as one group, in
/// grid order and each cell once, in one undo step that Undo removes.
#[test]
fn save_color_group_saves_the_selected_cells_in_grid_order() {
let mut app = app();
let ctx = egui::Context::default();
frame(&mut app, &ctx);
let st = app.session.doc().unwrap();
let (groups, swatches, undo) = (st.doc.swatch_groups.len(), st.doc.swatches.len(), st.history.undo.len());
set_pstate(&ctx, "cg-sel", vec![(1usize, 4usize), (0, 8), (0, 0), (1, 4)]);
save_group(&mut app, &ctx);
let g = guide(&app, &ctx);
let st = app.session.doc().unwrap();
assert_eq!(st.doc.swatch_groups.len(), groups + 1, "one new group");
assert_eq!(st.doc.swatches.len(), swatches, "no loose swatches");
assert_eq!(st.history.undo.len(), undo + 1, "one undo step");
let (name, made) = last_group(&app);
assert_eq!(name, rule(&ctx).label());
assert_eq!(made, [g.grid[0][0], g.grid[0][8], g.grid[1][4]]);
app.run("edit.undo", json!({})).unwrap();
assert_eq!(app.session.doc().unwrap().doc.swatch_groups.len(), groups, "Undo removes the group");
}
/// With no cell selected, or only cells outside the shown grid, the group holds the harmony
/// colors.
#[test]
fn save_color_group_without_a_selection_saves_the_harmony_colors() {
let mut app = app();
let ctx = egui::Context::default();
frame(&mut app, &ctx);
let g = guide(&app, &ctx);
save_group(&mut app, &ctx);
assert_eq!(last_group(&app).1, g.colors);
set_pstate(&ctx, "cg-sel", vec![(99usize, 0usize), (0, 99)]);
save_group(&mut app, &ctx);
assert_eq!(last_group(&app).1, g.colors, "cells outside the grid are ignored");
}
/// A guide limited to a library saves its snapped cells, and an RGB cell stays RGB in a CMYK
/// document.
#[test]
fn save_color_group_saves_limited_cells_in_their_own_model() {
let mut app = app();
app.run("file.documentColorMode", json!({"mode": "cmyk"})).unwrap();
app.run("paint.setFill", json!({"color": "#c04020", "keepModel": true})).unwrap();
app.run("ui.colorGuideLimit", json!({"library": "web-safe-216"})).unwrap();
let ctx = egui::Context::default();
frame(&mut app, &ctx);
set_pstate(&ctx, "cg-sel", vec![(0usize, 1usize), (0, 7)]);
save_group(&mut app, &ctx);
let g = guide(&app, &ctx);
let made = last_group(&app).1;
assert_eq!(made, [g.grid[0][1], g.grid[0][7]]);
assert!(made.iter().all(|c| matches!(c, Color::Rgb { .. })), "{made:?}");
let members = library_hexes(&app, "web-safe-216");
assert!(made.iter().all(|c| members.contains(&c.to_hex())), "the snapped colors");
}
/// A selection made in one guide is not saved from another: new options, the variation-mode
/// menu, the harmony rule or the library limit clear it.
#[test]
fn a_changed_guide_drops_the_cell_selection() {
let mut app = app();
let ctx = egui::Context::default();
frame(&mut app, &ctx);
let pick = |ctx: &egui::Context| set_pstate(ctx, "cg-sel", vec![(0usize, 1usize), (0, 2)]);
type Change = fn(&mut VectorcraftApp, &egui::Context);
let changes: [Change; 4] = [
|app, _| app.ui.color_guide.steps = 6,
|app, _| app.ui.color_guide.variation = Variation::WarmCool,
|_, ctx| set_pstate(ctx, "cg-harmony", 3usize),
|app, _| {
app.run("ui.colorGuideLimit", json!({"library": "web-safe-216"})).unwrap();
},
];
for (i, change) in changes.iter().enumerate() {
frame(&mut app, &ctx);
pick(&ctx);
assert_eq!(live_selection(&app, &ctx, &guide(&app, &ctx)).len(), 2, "change {i}: the selection holds");
change(&mut app, &ctx);
let g = guide(&app, &ctx);
assert!(live_selection(&app, &ctx, &g).is_empty(), "change {i}: nothing is saved from the old guide");
save_group(&mut app, &ctx);
assert_eq!(last_group(&app).1, g.colors, "change {i}: the harmony colors");
frame(&mut app, &ctx);
assert!(selected(&ctx).is_empty(), "change {i}: the panel clears the selection");
}
}
/// Limited to the document's swatches, painting art keeps the cell selection, and a new swatch
/// that changes the grid's colors drops it.
#[test]
fn a_new_document_swatch_drops_the_cell_selection() {
let mut app = app();
app.run("shape.rectangle", json!({"x": 0, "y": 0, "width": 10, "height": 10})).unwrap();
choose_base(&mut app, Color::from_hex("#3a7bd5").unwrap());
app.run("ui.colorGuideLimit", json!({"library": "document"})).unwrap();
let ctx = egui::Context::default();
frame(&mut app, &ctx);
set_pstate(&ctx, "cg-sel", vec![(0usize, 1usize), (0, 2)]);
app.run("paint.setFill", json!({"color": "#102030"})).unwrap();
frame(&mut app, &ctx);
assert_eq!(selected(&ctx).len(), 2, "an edit that leaves the grid as it is keeps the selection");
let before = guide(&app, &ctx);
app.run("swatch.new", json!({"colors": ["#3a7bd5"]})).unwrap();
let g = guide(&app, &ctx);
assert_ne!(g.grid, before.grid, "the new swatch changes the grid");
assert!(live_selection(&app, &ctx, &g).is_empty(), "nothing is saved from the old grid");
save_group(&mut app, &ctx);
assert_eq!(last_group(&app).1, g.colors);
frame(&mut app, &ctx);
assert!(selected(&ctx).is_empty(), "the panel clears the selection");
}
/// Every colour of the panel's guide.
fn shown(app: &VectorcraftApp, ctx: &egui::Context) -> Vec<String> {
let g = guide(app, ctx);
+1
View File
@@ -1691,6 +1691,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur
- **RGB input to other commands in a CMYK document:** type colors (`text.create`, `text.setStyle`, `text.setRangeStyle`), a Shape Builder hex fill, `paint.editGradient` stops, freeform gradient points, gradient mesh points (the Mesh tool gives a new point the fill's color in its own model), the Symbol Stainer's color and the swatch commands (`swatch.new {colors}`, `swatch.newGroup`, `swatch.editGroup`, `swatch.edit`) separate RGB through the color settings as `paint.setFill` does: with Generic CMYK, `text.setStyle {fill: "#53aadf"}` gives C62 M19 Y0 K0. A color a swatch already has keeps its model, and a swatch color given with `mode` is converted into that mode only.
- **Generic CMYK (SWOP-like):** each ink's 40% and 50% tints gain as [CGATS TR 001](https://registry.color.org/cmyk-registry/cgats_tr_001) gives for SWOP printing, and separations stay within SWOP's [300% total ink limit](https://en.wikipedia.org/wiki/Specifications_for_Web_Offset_Publications). The paper, solid and overprint colors, the black-generation curve and the black point are VectorCraft's own values: RGB black separates to 90% black under neutral cyan, magenta and yellow that fill the ink limit (C75 M68 Y67 K90).
- **Swatches made from a shown color in a CMYK document:** the Color panel menu's Create New Swatch, an Alt-click on the Swatches panel's New Swatch button, the New Swatch dialog for a gradient, a Fill or Stroke proxy or the Gradient panel's thumbnail dropped on the Swatches panel (or Alt-dropped on a swatch of its kind), and the Gradient panel's Add to Swatches and Save to Swatches keep each color's model, as the Color Guide's and Color Themes' saved colors do: an RGB object's color gives an RGB swatch, and a gradient's RGB stops stay RGB.
- **Color Guide color groups:** with variation cells selected, the Color Guide's Save Color Group to Swatches button saves those cells as one swatch group, and with none selected it saves the harmony colors, as the [help page](https://help.adobe.com/archive/en_US/illustrator/cs5/illustrator_cs5_help.pdf) describes saving several selected variations together (printed page 135). The group takes the cells in grid order (row by row, left to right, each cell once), is named after the harmony rule and is one undo step; these are VectorCraft's own rules. A selection lasts until the base, the Color Guide Options, the variation mode, the harmony rule, the library limit or the colors the grid shows (for example after a new swatch while the guide is limited to the document's swatches) change.
- **Sample Color from Screen:** an eyedropper button in the Color Picker and `ui.sampleScreenColor` (which can be given a key in Keyboard Shortcuts) pick a color anywhere on the screen with the system's sampler: `NSColorSampler` on macOS, the XDG desktop portal's [PickColor](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Screenshot.html) on Linux, and the EyeDropper API in browsers that have it. The color becomes the fill or the stroke as one undo step, or the Color Picker's color, converted from display sRGB into the working RGB space. On Linux, a session without a portal that has PickColor, a portal that does not return from the call within 25 seconds (VectorCraft's own limit) or leaves the bus before it answers, and a portal that ends the request another way each show a status message and end the wait. Windows, the BSDs and other browsers have no sampler yet; the button is gray there.
- **Stroke visibility (#1253):** adding paint to a previously unpainted zero-width stroke restores the existing 1 pt default (`PaintDefaults::default`, `Appearance::set_stroke`), for the selected appearance item, the new-art defaults and Swap Fill/Stroke. Positive widths are kept; recoloring a painted zero-width stroke keeps its explicit zero. A real Eyedropper-to-new-shape sequence reproduces the zero-width inheritance. Five engine regressions cover creation, defaults, targeted items, preserved widths and one-step Undo/Redo (`crates/engine/tests/it/stroke_visibility.rs`).
- **Gradients:** lossless, validated gradient params; gradients follow rotate, reflect, shear, non-uniform scale and distortions; the Gradient tool edits the fill or stroke of the active proxy, type included; an interactive on-canvas annotator (move, length/angle, rotate, add/move/delete/duplicate stops, midpoints, a stop popover, Delete and arrow keys). The Gradient panel has the Fill/Stroke proxy, a gradient-swatch dropdown with Save to Swatches, midpoint locations, Alt-drag to copy or swap stops, swatch drops on the ramp and a stop eyedropper; copied appearances and graphic styles place gradients on each target's own bounds; gradient handles, both ends of a vector drawn on the art and the start of a clicked one snap as other drawn points do (path segments, construction guides and View › Snap to Glyph's guides included) and honour Constrain Angle. Gradient stops stay linked to global and spot swatches. Freeform gradients (points and lines) are edited on the canvas; radial gradients have an extent ellipse, an aspect handle and an off-centre focal point (SVG `fx`/`fy`, PDF two-point radials). A stroke's gradient runs within, along or across it. Object ▸ Expand turns gradients into N objects or a gradient mesh.