mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-25 20:01:07 +01:00
Save for Web: the color table's selection matches a color written another way
- A selected color or list entry written another way (`#00F` for `#0000ff`) now names its table color: Lock, Transparent, Web Shift and Delete Color act on it and write it as the table does, and the ring and button labels follow it. - Delete Color compares the table's lists as colors, so deleting the only color left is refused when the selection or a list is written another way, and a deleted color is taken out of the Lock, Transparent and Web Shift lists however they write it. - A Ctrl-click on a selected color takes out every selection entry that names it. - A spelling with alpha (`#rrggbbaa`) names no table color, as the engine reads the table. Docs: docs/mcp.md.
This commit is contained in:
1 parent
c51fc47b30
commit
c0f91bc820
3 files changed
+123
-23
No files matched your search
@@ -1219,7 +1219,7 @@ fn color_table(ui: &mut egui::Ui, d: &mut Dialog, s: Option<&WebSettings>) -> Re
|
||||
}
|
||||
// The transparent entry itself isn't a colour to edit.
|
||||
let pickable = !c.transparent || *mapped;
|
||||
let ringed = pickable && picked.iter().any(|h| h.eq_ignore_ascii_case(&hex(c.source)));
|
||||
let ringed = pickable && picked.iter().any(|h| same_color(h, &hex(c.source)));
|
||||
p.rect_stroke(r, 0.0, egui::Stroke::new(if ringed { 2.0 } else { 1.0 }, if ringed { t.accent } else { t.border }), egui::StrokeKind::Outside);
|
||||
if !pickable {
|
||||
continue;
|
||||
@@ -1242,14 +1242,17 @@ fn color_table(ui: &mut egui::Ui, d: &mut Dialog, s: Option<&WebSettings>) -> Re
|
||||
d.fields.insert("__editColor".into(), json!(source));
|
||||
d.fields.insert("__editStart".into(), json!(start));
|
||||
}
|
||||
let shifted = |c: &str| table.shifted.keys().any(|k| k.eq_ignore_ascii_case(c));
|
||||
let shifted = |c: &str| table.shifted.keys().any(|k| same_color(k, c));
|
||||
// The colors the table shows, by source: Lock, Transparent, Web Shift and Delete Color act only
|
||||
// on these.
|
||||
let palette: Vec<String> =
|
||||
shot.colors.iter().filter(|c| !c.transparent).map(|c| hex(c.source)).chain(shot.mapped.iter().map(|c| hex(*c))).collect();
|
||||
let shown = palette.iter().any(|p| p.eq_ignore_ascii_case(&selected));
|
||||
// The selected color as the table writes it; the buttons act on that spelling.
|
||||
let shown_as = palette.iter().find(|p| same_color(p, &selected)).map(|p| p.to_ascii_lowercase());
|
||||
let shown = shown_as.is_some();
|
||||
let selected = shown_as.unwrap_or(selected);
|
||||
ui.add_space(4.0);
|
||||
let on = |list: &[String]| list.iter().any(|h| h.eq_ignore_ascii_case(&selected));
|
||||
let on = |list: &[String]| list.iter().any(|h| same_color(h, &selected));
|
||||
ui.horizontal(|ui| {
|
||||
ui.add_enabled_ui(shown, |ui| {
|
||||
for (list, on_label, off_label) in [
|
||||
@@ -1320,9 +1323,18 @@ fn color_table(ui: &mut egui::Ui, d: &mut Dialog, s: Option<&WebSettings>) -> Re
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The selected colors (by source) that `palette`, the colors the table shows, holds.
|
||||
/// The selected colors (by source) that `palette`, the colors the table shows, holds, each as
|
||||
/// `palette` writes it, once.
|
||||
pub(super) fn selected_in(d: &Dialog, palette: &[String]) -> Vec<String> {
|
||||
selection(d).into_iter().filter(|c| palette.iter().any(|p| p.eq_ignore_ascii_case(c))).collect()
|
||||
let mut out: Vec<String> = Vec::new();
|
||||
for c in selection(d) {
|
||||
if let Some(p) = palette.iter().find(|p| same_color(p, &c)).map(|p| p.to_ascii_lowercase())
|
||||
&& !out.contains(&p)
|
||||
{
|
||||
out.push(p);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The colour table's selected colors, by source: `__colors`, else `__color`.
|
||||
@@ -1342,7 +1354,7 @@ pub(super) fn selection(d: &Dialog) -> Vec<String> {
|
||||
/// or taken out, and the anchor moves to the last color left in the selection.
|
||||
pub(super) fn pick(d: &mut Dialog, order: &[String], color: &str, shift: bool, command: bool) {
|
||||
let color = color.to_ascii_lowercase();
|
||||
let at = |c: &str| order.iter().position(|o| o.eq_ignore_ascii_case(c));
|
||||
let at = |c: &str| order.iter().position(|o| same_color(o, c));
|
||||
if shift && let (Some(a), Some(b)) = (at(&d.str("__color")), at(&color)) {
|
||||
let range: Vec<String> = order[a.min(b)..=a.max(b)].iter().map(|c| c.to_ascii_lowercase()).collect();
|
||||
d.fields.insert("__colors".into(), json!(range));
|
||||
@@ -1350,15 +1362,13 @@ pub(super) fn pick(d: &mut Dialog, order: &[String], color: &str, shift: bool, c
|
||||
}
|
||||
let mut set = selection(d);
|
||||
let anchor = if command {
|
||||
match set.iter().position(|c| *c == color) {
|
||||
Some(i) => {
|
||||
set.remove(i);
|
||||
set.last().cloned()
|
||||
}
|
||||
None => {
|
||||
set.push(color.clone());
|
||||
Some(color)
|
||||
}
|
||||
if set.iter().any(|c| same_color(c, &color)) {
|
||||
// Every entry that names the color, however it is written.
|
||||
set.retain(|c| !same_color(c, &color));
|
||||
set.last().cloned()
|
||||
} else {
|
||||
set.push(color.clone());
|
||||
Some(color)
|
||||
}
|
||||
} else {
|
||||
set = vec![color.clone()];
|
||||
@@ -1384,6 +1394,9 @@ pub(super) fn delete_colors(d: &mut Dialog, palette: &[String]) -> Result<(), St
|
||||
d.fields.remove("__color");
|
||||
return Ok(());
|
||||
}
|
||||
// Each entry as `#rrggbb` when it names a color, so entries written another way compare equal.
|
||||
let norm = |c: &str| table_rgb(c).unwrap_or_else(|| c.to_ascii_lowercase());
|
||||
let picked: Vec<String> = picked.iter().map(|c| norm(c)).collect();
|
||||
let list = |d: &Dialog, key: &str| -> Vec<String> {
|
||||
d.fields
|
||||
.get("colorTable")
|
||||
@@ -1392,12 +1405,12 @@ pub(super) fn delete_colors(d: &mut Dialog, palette: &[String]) -> Result<(), St
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.filter_map(Value::as_str)
|
||||
.map(str::to_ascii_lowercase)
|
||||
.map(norm)
|
||||
.collect()
|
||||
};
|
||||
let mut custom = list(d, "customPalette");
|
||||
if custom.is_empty() {
|
||||
custom = palette.iter().map(|c| c.to_ascii_lowercase()).collect();
|
||||
custom = palette.iter().map(|c| norm(c)).collect();
|
||||
}
|
||||
let mut deleted = list(d, "deleted");
|
||||
for c in &picked {
|
||||
@@ -1491,16 +1504,25 @@ fn shifts(d: &Dialog) -> Map<String, Value> {
|
||||
d.fields.get("colorTable").and_then(|t| t.get("shifted")).and_then(Value::as_object).cloned().unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Whether `a` and `b` name one color, however each is written; text that is not a color matches
|
||||
/// the same text in any case.
|
||||
/// Whether `a` and `b` name one color, however each is written, as the engine reads the table's
|
||||
/// colors; other text, a spelling with alpha included (four or eight hex digits), matches only the
|
||||
/// same text in any case.
|
||||
fn same_color(a: &str, b: &str) -> bool {
|
||||
let rgb = |h: &str| vectorcraft_color::Color::from_hex(h).map(|c| c.to_hex());
|
||||
match (rgb(a), rgb(b)) {
|
||||
match (table_rgb(a), table_rgb(b)) {
|
||||
(Some(a), Some(b)) => a == b,
|
||||
_ => a.eq_ignore_ascii_case(b),
|
||||
}
|
||||
}
|
||||
|
||||
/// The color `h` names as `#rrggbb`, read as the engine reads the table's colors: `None` for text
|
||||
/// that is not a color and for a spelling with alpha (four or eight hex digits).
|
||||
fn table_rgb(h: &str) -> Option<String> {
|
||||
let t = h.trim();
|
||||
let digits = t.strip_prefix('#').unwrap_or(t);
|
||||
let alpha = matches!(digits.len(), 4 | 8) && digits.chars().all(|c| c.is_ascii_hexdigit());
|
||||
if alpha { None } else { vectorcraft_color::Color::from_hex(t).map(|c| c.to_hex()) }
|
||||
}
|
||||
|
||||
/// Take the shift by hand of the source color `source` away: every key that names that color.
|
||||
pub(super) fn unshift(d: &mut Dialog, source: &str) {
|
||||
let mut map = shifts(d);
|
||||
|
||||
@@ -798,6 +798,51 @@ fn colour_table_selection_and_delete_color() {
|
||||
assert_eq!((&d.fields["colorTable"]["deleted"], &d.fields["colorTable"]["customPalette"]), (&json!([]), &json!([])));
|
||||
}
|
||||
|
||||
/// A selection written another way (`#00F`) is deleted as the table writes it, so deleting the
|
||||
/// table's only color is refused with nothing changed; a spelling with alpha names no table color.
|
||||
#[test]
|
||||
fn delete_color_matches_a_selection_written_another_way() {
|
||||
let mut d = Dialog::new(save_for_web::KIND, json!({}));
|
||||
let shown: Vec<String> = ["#0000ff"].map(String::from).to_vec();
|
||||
d.fields.insert("__colors".into(), json!(["#00F", "#0000FF"]));
|
||||
assert_eq!(save_for_web::selected_in(&d, &shown), ["#0000ff"]);
|
||||
let before = d.fields.clone();
|
||||
assert_eq!(save_for_web::delete_colors(&mut d, &shown), Err(vectorcraft_engine::cmd::webexport::KEEP_ONE.to_string()));
|
||||
assert_eq!(d.fields, before);
|
||||
d.fields.insert("__colors".into(), json!(["#0000ff80"]));
|
||||
assert!(save_for_web::selected_in(&d, &shown).is_empty());
|
||||
}
|
||||
|
||||
/// A Ctrl-click on a selected color takes out every selection entry that names it, however each is
|
||||
/// written (`#00F`, `#0000ff`).
|
||||
#[test]
|
||||
fn command_click_deselects_every_spelling_of_a_color() {
|
||||
let order: Vec<String> = ["#0000ff", "#ff0000"].map(String::from).to_vec();
|
||||
let mut d = Dialog::new(save_for_web::KIND, json!({}));
|
||||
d.fields.insert("__colors".into(), json!(["#ff0000", "#00F", "#0000ff"]));
|
||||
save_for_web::pick(&mut d, &order, "#0000ff", false, true);
|
||||
assert_eq!(save_for_web::selection(&d), ["#ff0000"]);
|
||||
assert_eq!(d.fields["__color"], json!("#ff0000"));
|
||||
}
|
||||
|
||||
/// Delete Color compares the table's lists as colors: with blue mapped to transparent as `#00f`,
|
||||
/// deleting red (selected as `#f00`), the only other color, is refused with nothing changed, and a
|
||||
/// deleted color written another way in `locked` (`blue`, ` #0000FF `) is taken out of the list.
|
||||
#[test]
|
||||
fn delete_color_compares_lists_written_another_way() {
|
||||
let shown: Vec<String> = ["#0000ff", "#ff0000"].map(String::from).to_vec();
|
||||
let mut d = Dialog::new(save_for_web::KIND, json!({"colorTable": {"transparent": ["#00f"]}}));
|
||||
d.fields.insert("__color".into(), json!("#f00"));
|
||||
let before = d.fields.clone();
|
||||
assert_eq!(save_for_web::delete_colors(&mut d, &shown), Err(vectorcraft_engine::cmd::webexport::KEEP_ONE.to_string()));
|
||||
assert_eq!(d.fields, before);
|
||||
let mut d = Dialog::new(save_for_web::KIND, json!({"colorTable": {"locked": ["blue", " #0000FF ", "#ff0000"]}}));
|
||||
d.fields.insert("__color".into(), json!("#00F"));
|
||||
save_for_web::delete_colors(&mut d, &shown).unwrap();
|
||||
assert_eq!(d.fields["colorTable"]["locked"], json!(["#ff0000"]));
|
||||
assert_eq!(d.fields["colorTable"]["deleted"], json!(["#0000ff"]));
|
||||
}
|
||||
|
||||
/// Delete Color takes only selected colors the table shows: a selection the table no longer holds
|
||||
/// (after a change of reduction or color count, or set through `ui.dialog.set`) deletes nothing,
|
||||
/// fixes no palette and is cleared, and a selection partly in the table deletes only what it shows.
|
||||
@@ -1023,6 +1068,37 @@ fn table_buttons_skip_a_selected_color_the_table_does_not_show() {
|
||||
assert!(has(&app, "locked", "#0000ff"), "{}", table(&app));
|
||||
}
|
||||
|
||||
/// A list entry written another way (`#00F`) names the selected color: the buttons show Unlock,
|
||||
/// Unshift and Restore, and a click takes the color out of the list.
|
||||
#[test]
|
||||
fn table_buttons_match_a_color_written_another_way() {
|
||||
let dir = vectorcraft_testkit::temp_dir("sfw-short-hex");
|
||||
let (mut app, _) = app(&dir, false);
|
||||
app.run("shape.rectangle", json!({"x": 40, "y": 0, "width": 20, "height": 40})).unwrap();
|
||||
app.run("paint.setFill", json!({"color": "#0000ff"})).unwrap();
|
||||
app.run("file.saveForWeb", json!({})).unwrap();
|
||||
for (k, v) in [("format", json!("gif")), ("antiAlias", json!("none")), ("dither", json!("none"))] {
|
||||
set(&mut app, k, v);
|
||||
}
|
||||
let table = |app: &VectorcraftApp| app.ui.dialog.as_ref().and_then(|d| d.fields.get("colorTable").cloned()).unwrap_or_default();
|
||||
for (list, label) in [("locked", "Unlock"), ("webShift", "Unshift"), ("transparent", "Restore")] {
|
||||
set(&mut app, "colorTable", json!({ list: ["#00F"] }));
|
||||
set(&mut app, "__color", json!("#0000ff"));
|
||||
click_text(&mut app, label);
|
||||
assert_eq!(table(&app)[list], json!([]), "{list}");
|
||||
}
|
||||
// Selected as `#00F`, a click adds the color as the table writes it.
|
||||
set(&mut app, "colorTable", json!({}));
|
||||
set(&mut app, "__color", json!("#00F"));
|
||||
click_text(&mut app, "Lock");
|
||||
assert_eq!(table(&app)["locked"], json!(["#0000ff"]));
|
||||
// A spelling with alpha names no table color: the buttons stay off and nothing is added.
|
||||
set(&mut app, "colorTable", json!({}));
|
||||
set(&mut app, "__color", json!("#0000ff80"));
|
||||
click_text(&mut app, "Lock");
|
||||
assert_eq!(table(&app).get("locked"), None, "{}", table(&app));
|
||||
}
|
||||
|
||||
/// Click the dialog's text `label` with the pointer (frames a quarter second apart).
|
||||
fn click_text(app: &mut VectorcraftApp, label: &str) {
|
||||
let ctx = egui::Context::default();
|
||||
|
||||
+3
-1
@@ -10568,7 +10568,9 @@ 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. Done or Save
|
||||
limit. 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
|
||||
right, with the lock mark, and Lock stays on until the color is unshifted. Web Shift replaces a shift by hand, and
|
||||
Unshift All Colors in the table's menu clears `shifted` and `webShift`. Only Web Colors in the picker starts off.
|
||||
|
||||
Reference in new issue
Block a user