diff --git a/crates/engine/src/cmd/recolor.rs b/crates/engine/src/cmd/recolor.rs index 73d53945eb..52877ae542 100644 --- a/crates/engine/src/cmd/recolor.rs +++ b/crates/engine/src/cmd/recolor.rs @@ -43,7 +43,7 @@ pub fn specs() -> Vec { "Recolor Artwork", ["Edit", "Edit Colors"], None, - "{map: rows [{from: [colour keys (recolor.colors) or \"#rrggbb\" (every colour shown as that hex); may be empty: an empty row recolours nothing, adds no library swatch and gives `group` no colour unless `added`], to: colour (hex, key, {c,m,y,k} or {l,a,b}) | null (in from, to and groupColors, #rrggbbaa and #rgba are refused), exclude?: false (the row's colours are kept), tint?: 100 (percent; with limitTo or swatch, art linked to the global or spot swatch the new color is takes this share of its tint), added?: false (an empty row's new color is a color added to `group`), swatch?: {name, library?: swatch library id or name; \"document\" (default)} (the swatch the new color was picked from: of the swatches with the name, the first with the row's new color (as a color key); while it is a global or spot swatch and there is no globalAdjust (or limitTo is given), the row's art takes the swatch as with limitTo, linked to it at its tints; a process swatch, or a swatch or library that is not there, links nothing; with limitTo the library's swatch of the row's new color comes first; a global swatch of `group` that is rewritten with another color links nothing, and the row's art keeps the row's new color; a `swatch` that is not {name, library?} is an error)}] | {\"key or #rrggbb\": colour, …} (one row per colour), method?: \"exact\" (default)|\"preserveTints\"|\"scaleTints\"|\"tintsShades\"|\"hueShift\" (how a row's colours take its new colour, relative to the row's darkest, average or most saturated colour), limitTo?: swatch library id or name; or \"document\" (the document's swatches): new colours snap to its nearest colour, group?: colour group to rewrite, groupColors?: [colour | null | \"keep\"] (what the color swatches of `group` are rewritten with, in order: a gradient, pattern or None swatch keeps its place and paint; a null removing the color swatch at its place and \"keep\" leaving that swatch its colour, without the Global Adjust (past the color swatches it adds nothing); default: the new colors of the rows with current colors, in order, then those of the empty rows with `added`, excluded rows left out), rename?: the group's new name, includeImages?: true, includePatterns?: true, globalAdjust?: {saturation?: 0, brightness?: 0, temperature?: 0, luminosity?: 0} (Global Adjust: percent from -100 to 100, 0 changes nothing; every colour a row that isn't excluded recolours, and the group's new colours, take the same change after the row's new colour: saturation scales HSB saturation by 1 + saturation/100, brightness moves HSB brightness that share of the way to full or to black, temperature adds 0.2 × temperature/100 to red and takes it from blue, luminosity moves Lab lightness that share of the way to 100 or 0 keeping a and b; with limitTo each row's new colour takes the change before it snaps to the library)} recolour the selection (gradients, text, meshes, image pixels and pattern tiles included; each new colour keeps the model of the one it replaces (model?: \"keep\" default | \"new\" (the new colour's own model, so a CMYK colour replacing RGB art gives CMYK art) | \"document\" (the document's colour mode)), except a library swatch's with limitTo: art takes the swatch itself, linked to a global or spot colour at its tints, and the swatch joins the document's swatches; art linked to a global swatch of `group` that is rewritten with its row's new color stays linked to it, at its tint, unless the row converts the color into the art's model; other recolored art, and art in a row linked to a global swatch of `group` that is rewritten with another color, keeps its row's new color and loses its swatch link) and the group, as one undo step (none when nothing changes). On a path directly in a graph series, the graph also stores the new paint of the topmost visible fill and stroke, and on a graph label the new paint of its characters and rows for every label of its kind, and on a graph's axis line, tick mark or radar ring its rows for every line of its kind (docs/mcp.md) → {changed, swatches: [names added to the document's swatches], swatchesNotFound?: [names of rows' `swatch` that are not there, which linked nothing], relinked?: with group, the other paints linked to its global swatches that took their new colors, note?: set when a colour kept a model other than its new colour's (see model), notChanged: {locked, hidden} (as edit.colors.invert), warning?: set when notChanged counts any object}", + "{map: rows [{from: [colour keys (recolor.colors) or \"#rrggbb\" (every colour shown as that hex); may be empty: an empty row recolours nothing, adds no library swatch and gives `group` no colour unless `added`], to: colour (hex, key, {c,m,y,k} or {l,a,b}) | null (in from, to and groupColors, #rrggbbaa and #rgba are refused), exclude?: false (the row's colours are kept), tint?: 100 (percent; with limitTo or swatch, art linked to the global or spot swatch the new color is takes this share of its tint), added?: false (an empty row's new color is a color added to `group`), swatch?: {name, library?: swatch library id or name; \"document\" (default)} (the swatch the new color was picked from: of the swatches with the name, the first with the row's new color (as a color key); while it is a global or spot swatch and there is no globalAdjust (or limitTo is given), the row's art takes the swatch as with limitTo, linked to it at its tints; a process swatch, or a swatch or library that is not there, links nothing; with limitTo the library's swatch of the row's new color comes first; a global swatch of `group` that is rewritten with another color links nothing, and the row's art keeps the row's new color; a null `swatch` is no swatch, and any other `swatch` that is not {name, library?} is an error)}] | {\"key or #rrggbb\": colour, …} (one row per colour), method?: \"exact\" (default)|\"preserveTints\"|\"scaleTints\"|\"tintsShades\"|\"hueShift\" (how a row's colours take its new colour, relative to the row's darkest, average or most saturated colour), limitTo?: swatch library id or name; or \"document\" (the document's swatches): new colours snap to its nearest colour, group?: colour group to rewrite, groupColors?: [colour | null | \"keep\"] (what the color swatches of `group` are rewritten with, in order: a gradient, pattern or None swatch keeps its place and paint; a null removing the color swatch at its place and \"keep\" leaving that swatch its colour, without the Global Adjust (past the color swatches it adds nothing); default: the new colors of the rows with current colors, in order, then those of the empty rows with `added`, excluded rows left out), rename?: the group's new name, includeImages?: true, includePatterns?: true, globalAdjust?: {saturation?: 0, brightness?: 0, temperature?: 0, luminosity?: 0} (Global Adjust: percent from -100 to 100, 0 changes nothing; every colour a row that isn't excluded recolours, and the group's new colours, take the same change after the row's new colour: saturation scales HSB saturation by 1 + saturation/100, brightness moves HSB brightness that share of the way to full or to black, temperature adds 0.2 × temperature/100 to red and takes it from blue, luminosity moves Lab lightness that share of the way to 100 or 0 keeping a and b; with limitTo each row's new colour takes the change before it snaps to the library)} recolour the selection (gradients, text, meshes, image pixels and pattern tiles included; each new colour keeps the model of the one it replaces (model?: \"keep\" default | \"new\" (the new colour's own model, so a CMYK colour replacing RGB art gives CMYK art) | \"document\" (the document's colour mode)), except a library swatch's with limitTo: art takes the swatch itself, linked to a global or spot colour at its tints, and the swatch joins the document's swatches; art linked to a global swatch of `group` that is rewritten with its row's new color stays linked to it, at its tint, unless the row converts the color into the art's model; other recolored art, and art in a row linked to a global swatch of `group` that is rewritten with another color, keeps its row's new color and loses its swatch link) and the group, as one undo step (none when nothing changes). On a path directly in a graph series, the graph also stores the new paint of the topmost visible fill and stroke, and on a graph label the new paint of its characters and rows for every label of its kind, and on a graph's axis line, tick mark or radar ring its rows for every line of its kind (docs/mcp.md) → {changed, swatches: [names added to the document's swatches], swatchesNotFound?: [names of rows' `swatch` that are not there, which linked nothing], relinked?: with group, the other paints linked to its global swatches that took their new colors, note?: set when a colour kept a model other than its new colour's (see model), notChanged: {locked, hidden} (as edit.colors.invert), warning?: set when notChanged counts any object}", has_doc, apply ), diff --git a/crates/ui-egui/src/dialogs/color_picker.rs b/crates/ui-egui/src/dialogs/color_picker.rs index 9209c854e0..f45fadf201 100644 --- a/crates/ui-egui/src/dialogs/color_picker.rs +++ b/crates/ui-egui/src/dialogs/color_picker.rs @@ -321,7 +321,7 @@ pub(crate) fn check_confirm(app: &VectorcraftApp, host: &craft::ui::dialog::Dial } /// Why a picker's OK answers nothing over Recolor Artwork. -fn rows_changed() -> String { +pub(super) fn rows_changed() -> String { tl!("The Recolor Artwork rows changed while the Color Picker was open").to_string() } @@ -436,6 +436,8 @@ fn finish( { super::recolor::answer_swatch(dialogs, *parent, key, swatch); } + // The pump applies the answer later; it drops it when the row changed in between. + super::recolor::answer_row(dialogs, *parent, open.row.clone()); if !super::save_for_web::take_palette_pick(app, *parent, key, open.start, r.picked.color) { dialogs.with(|h| h.answer_color(*parent, key, r.picked.color)); } diff --git a/crates/ui-egui/src/dialogs/recolor.rs b/crates/ui-egui/src/dialogs/recolor.rs index 83d815c561..01860accb2 100644 --- a/crates/ui-egui/src/dialogs/recolor.rs +++ b/crates/ui-egui/src/dialogs/recolor.rs @@ -1529,9 +1529,12 @@ struct RowFeed { impl RowFeed { fn of(app: &VectorcraftApp, d: &Fields, links: &RowLinks, row: &Value) -> RowFeed { let to = to_color(row); - let spot = to.filter(|_| d.bool("__art")).and_then(|c| linked_spot(app, d, links, row, &c)); + // `recolor.apply` skips an excluded row, so its art links no spot swatch and Tint has nothing to set. + let spot = to.filter(|_| d.bool("__art") && !excluded(row)).and_then(|c| linked_spot(app, d, links, row, &c)); + // A swatch OK removes from the group links nothing afterwards, so the row offers no Tint. + let tint_swatch = spot.as_ref().filter(|w| !links.removed.contains(&w.name)).map(|w| w.name.clone()); let color = spot.as_ref().and_then(|w| w.paint.color()).or(to).unwrap_or_default(); - RowFeed { shown: new_shown(app, d, links, row), linked: linked(app, color), spot: spot.map(|w| w.name) } + RowFeed { shown: new_shown(app, d, links, row), linked: linked(app, color), spot: tint_swatch } } } @@ -1741,6 +1744,14 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) { if let Some(d) = host.get_mut_by_id::(id) && let Some((row, c)) = d.picked.take() { + // A row that changed between the picker's OK and this pass takes neither the colour nor the swatch. + if let Some(was) = d.picked_row.take() + && rows(&d.fields).get(row) != Some(&was) + { + d.picked_swatch = None; + app.status(super::color_picker::rows_changed()); + return; + } // A colour picked as RGB keeps the model of the row's new colour (CMYK in a CMYK document). let picked = Color::rgb(c.r, c.g, c.b); let was = rows(&d.fields).get(row).and_then(to_color); @@ -1863,6 +1874,16 @@ pub(super) fn answer_swatch(dialogs: &craft::ui::shell::WindowDialogs, parent: D }); } +/// The row as it was when the Color Picker's OK answered it, which the pump compares with the row +/// before it gives the row the answer. +pub(super) fn answer_row(dialogs: &craft::ui::shell::WindowDialogs, parent: DialogId, identity: Option) { + dialogs.with(|host| { + if let Some(d) = host.get_mut_by_id::(parent) { + d.picked_row = identity; + } + }); +} + /// Row `key` of Recolor Artwork `parent`, every field of it, or `None` when `parent` is not Recolor /// Artwork or has no such row. A Color Picker opened for the row answers it only while this is unchanged. pub(super) fn row_identity(dialogs: &craft::ui::shell::WindowDialogs, parent: DialogId, key: &str) -> Option { @@ -1971,6 +1992,9 @@ pub(crate) struct RecolorDialog { /// The row and the swatch the picker's colour was picked from ([`answer_swatch`]), until the pump /// gives the row that colour. picked_swatch: Option<(usize, Option<(Value, Color)>)>, + /// The answered row as it was when the picker answered ([`answer_row`]); the pump drops the + /// answer when the row has changed since. + picked_row: Option, /// The Color Theme Picker's canvas tool, which the canvas runs while the picker is on. theme_tool: edit_tab::ThemePickTool, } @@ -1990,6 +2014,7 @@ impl RecolorDialog { color_request: None, picked: None, picked_swatch: None, + picked_row: None, theme_tool: edit_tab::ThemePickTool::default(), } } @@ -2616,6 +2641,7 @@ fn new_shown(app: &VectorcraftApp, d: &Fields, links: &RowLinks, row: &Value) -> // `recolor.apply` skips an excluded row, so without a library its chip takes no Global Adjust. let effective = if excluded(row) && links.library.is_none() { to } else { links.effective_color(&to) }; if d.bool("__art") + && !excluded(row) && let Some(c) = linked_spot(app, d, links, row, &to).and_then(|w| w.paint.color()) { spot_tinted(app, c, row_tint(row)) @@ -2707,6 +2733,8 @@ struct RowLinks { library: Option<(vectorcraft_color::recolor::Palette, Vec)>, adjust: GlobalAdjust, rewritten: Vec, + /// The names of the color group's global swatches that OK's rewrite removes ([`removed_from_group`]). + removed: Vec, } impl RowLinks { @@ -2714,7 +2742,7 @@ impl RowLinks { let lib = d.str("limitTo"); let library = if lib.is_empty() { None } else { limit_palette_before_preview(app, &lib).zip(limit_swatches(app, &lib)) }; let rewritten = rewritten_to_another(app, d, library.as_ref().map(|(palette, _)| palette)); - Self { library, adjust: global_adjust(adjust_values(d)), rewritten } + Self { library, adjust: global_adjust(adjust_values(d)), rewritten, removed: removed_from_group(app, d) } } /// The color `recolor.apply` gives the art for new color `c`: `c` after the Global Adjust, @@ -2814,6 +2842,23 @@ fn rewritten_to_another(app: &VectorcraftApp, d: &Fields, palette: Option<&vecto .collect() } +/// The names of the color group's global swatches that OK's rewrite removes (a `null` in +/// `groupColors` of [`apply_params`]). `recolor.apply` gives art linked to one its color at its +/// tint and no link, so its row's chip keeps the tint and the row offers no Tint slider. +fn removed_from_group(app: &VectorcraftApp, d: &Fields) -> Vec { + let p = apply_params(d); + let (Some(group), Some(colors)) = (p["group"].as_str(), p["groupColors"].as_array()) else { return Vec::new() }; + let Some(doc) = doc_before_preview(app) else { return Vec::new() }; + let Some(g) = doc.swatch_groups.iter().find(|g| g.name == group) else { return Vec::new() }; + g.swatches + .iter() + .filter(|w| w.paint.color().is_some()) + .zip(colors) + .filter(|(w, v)| v.is_null() && w.global && !w.is_reserved()) + .map(|(w, _)| w.name.clone()) + .collect() +} + /// The name and library key of `row`'s `swatch` when it is `{name, library?}` with a name, as /// `recolor.apply` reads it. fn swatch_ref(row: &Value) -> Option<(&str, &str)> { @@ -4706,6 +4751,57 @@ mod tests { assert_eq!(row_shown(&rc.app(), &adjusted, 0).0, None, "no Tint under a Global Adjust"); } + /// A Color Picker answer that waits for the pump goes only to the row it answered: a row that + /// `ui.dialog.set` changed in between takes neither the color nor the swatch, and the status + /// line says why. An unchanged row takes both. + #[test] + fn a_waiting_picker_answer_skips_a_row_that_changed() { + let mut rc = gray_with_ink(json!([{"from": ["rgb 128 128 128"], "to": "rgb 128 128 128"}])); + let green = Srgba { r: 0.0, g: 1.0, b: 0.0, a: 1.0 }; + let answer = |rc: &Recolor, was: Value| { + rc.with_dialog(|_, d| { + d.picked = Some((0, green)); + d.picked_swatch = Some((0, Some((json!({"name": "Ink", "library": "document"}), Color::cmyk(0.0, 1.0, 0.0, 0.0))))); + d.picked_row = Some(was); + }) + .unwrap(); + }; + let was = rc.rows()[0].clone(); + answer(&rc, was); + let changed = json!([{"from": ["rgb 128 128 128"], "to": "rgb 0 0 255"}]); + rc.with_dialog(|_, d| { + d.fields.fields.insert("rows".into(), changed.clone()); + }) + .unwrap(); + rc.frame(); + let r = rc.rows()[0].clone(); + assert_eq!(r["to"], changed[0]["to"], "the changed row keeps its color: {r}"); + assert!(r.get(SWATCH).is_none(), "and takes no swatch: {r}"); + assert!(rc.app().ui.status.contains("rows changed"), "{}", rc.app().ui.status); + let was = rc.rows()[0].clone(); + answer(&rc, was); + rc.frame(); + let r = rc.rows()[0].clone(); + assert_eq!(r[SWATCH], json!({"name": "Ink", "library": "document"}), "an unchanged row takes the answer: {r}"); + } + + /// An excluded row keeps its picked spot swatch and tint, but `recolor.apply` skips its art, so + /// the row offers no Tint slider. + #[test] + fn an_excluded_row_offers_no_tint() { + let rows = json!([{"from": ["rgb 128 128 128"], "to": "cmyk 0 100 0 0", "swatch": {"name": "Ink", "library": "document"}, "tint": 40.0}]); + let mut rc = gray_with_ink(rows.clone()); + let fields = rc.fields(); + assert_eq!(row_shown(&rc.app(), &fields, 0).0.as_deref(), Some("Ink"), "Tint while the row recolors"); + let mut excluded = rows; + excluded[0]["exclude"] = json!(true); + rc.set("rows", excluded).unwrap(); + rc.settled(); + let fields = rc.fields(); + assert_eq!(row_shown(&rc.app(), &fields, 0).0, None, "no Tint for an excluded row"); + assert_eq!(rc.rows()[0]["tint"], json!(40.0), "the row keeps its tint"); + } + /// A swatch picked for a row under Limit to Library, whose color the library snaps to another /// one, is not kept: the row's new color is no longer the swatch's color. #[test] @@ -4805,10 +4901,10 @@ mod tests { #[test] fn the_effective_color_takes_the_global_adjust_without_a_library() { let red = Color::rgb(1.0, 0.0, 0.0); - let darker = RowLinks { library: None, adjust: global_adjust([0.0, -50.0, 0.0, 0.0]), rewritten: Vec::new() }; + let darker = RowLinks { library: None, adjust: global_adjust([0.0, -50.0, 0.0, 0.0]), rewritten: Vec::new(), removed: Vec::new() }; assert_eq!(darker.effective_color(&red), darker.adjust.apply(red)); assert_ne!(darker.effective_color(&red), red, "brightness -50 darkens the color"); - let plain = RowLinks { library: None, adjust: global_adjust([0.0; 4]), rewritten: Vec::new() }; + let plain = RowLinks { library: None, adjust: global_adjust([0.0; 4]), rewritten: Vec::new(), removed: Vec::new() }; assert_eq!(plain.effective_color(&red), red); } @@ -4856,6 +4952,22 @@ mod tests { rc.settled(); let fields = rc.fields(); assert_eq!(row_shown(&rc.app(), &fields, 1), (None, ink), "no Tint slider and the full color once Ink takes green"); + // A row with no new color removes Ink from the group (`null` in `groupColors`). + rc.set("rows", json!([{"from": ["cmyk 0 100 0 0"], "to": null}, rows("")[1].clone()])).unwrap(); + rc.settled(); + let fields = rc.fields(); + assert_eq!(apply_params(&fields)["groupColors"][0], Value::Null, "{}", apply_params(&fields)); + assert_eq!(row_shown(&rc.app(), &fields, 1), (None, shown), "no Tint slider, and the chip at 40% once Ink is removed"); + // OK gives the gray square that 40% tint of the ink, with no link. + let mut app = rc.app(); + let _ = app.session.cancel_interaction(); + app.session.execute(CMD, &apply_params(&fields)).unwrap(); + let doc = &app.session.doc().unwrap().doc; + let nodes = doc.layers.iter().filter_map(|l| l.children()).flatten(); + let paints: Vec<_> = nodes.map(|n| doc.node(n.id).unwrap().appearance.fill_paint()).collect(); + let at_chip = |c: &Color| ColorKey::of(c) == ColorKey::of(&shown); + let unlinked = paints.iter().any(|p| matches!(p, vectorcraft_color::Paint::Solid { color, swatch: None, .. } if at_chip(color))); + assert!(unlinked, "the art takes the chip's color unlinked: {paints:?}"); } /// The new colors of the rows. diff --git a/docs/mcp.md b/docs/mcp.md index c80b69db1e..ce203a3080 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -6837,7 +6837,7 @@ other recolored art does. A row whose new color is picked from a global or spot swatch in the Color Picker's swatch list in Recolor Artwork carries it when the dialog has art and no earlier solid swatch (other than a linked tint) in the same library has the same name and color, so with no Limit to Library the art still takes the swatch picked; the dialog drops a row's `swatch` when the row's new color changes. A -`swatch` that is not `{name, library?}` is an error. `recolor.randomize` +`swatch` of `null` is the same as no `swatch`; any other `swatch` that is not `{name, library?}` is an error. `recolor.randomize` moves a row's `tint` and `swatch` with its new color and removes them from a color it changes, and moves a row's `model` with its new color. `swatch.editGroup {group, colors, rename?}` rewrites a colour group in one undo step (art linked to its global swatches follows); a `null` in `colors` removes the group's swatch at that place, and given the group's own colors in order and no new name, it records no undo step. A color key (`recolor.colors`) that