mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-26 22:14:06 +00:00
Document Setup: an unchanged Color Picker OK keeps a grid color's own model
The Color Picker opened from a grid color's box wrote its color back to Document Setup's gridColors as a hex code. Since #8630 Document Setup opens the picker on the document's own grid colors, so an unchanged OK on a Lab or CMYK grid color turned it into display RGB, and Document Setup's OK stored that RGB color. The picker now writes a list entry with grid_color_entry, which keeps a color in its own model when its hex code would read back as another color. A scalar color field still gets a hex code.
This commit is contained in:
@@ -793,10 +793,10 @@ fn swatch_list(app: &VectorcraftApp, ui: &mut Ui, d: &mut Dialog, current: &Colo
|
||||
fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result<Value, String> {
|
||||
if let Some(field) = d.fields.get(COLOUR_FIELD).and_then(Value::as_str) {
|
||||
let mut parent = super::parent(d).ok_or("the dialog it was opened over is no longer open")?;
|
||||
let hex = json!(picked(d).color.to_hex());
|
||||
let colour = picked(d).color;
|
||||
match d.fields.get(COLOUR_INDEX) {
|
||||
None => {
|
||||
parent.fields.insert(field.to_string(), hex);
|
||||
parent.fields.insert(field.to_string(), json!(colour.to_hex()));
|
||||
}
|
||||
Some(index) => {
|
||||
if let Some(list) = d.fields.get(COLOUR_LIST) {
|
||||
@@ -807,7 +807,9 @@ fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result<Value, String> {
|
||||
.and_then(|i| usize::try_from(i).ok())
|
||||
.and_then(|i| parent.fields.get_mut(field)?.as_array_mut()?.get_mut(i))
|
||||
.ok_or("no such color")?;
|
||||
*entry = hex;
|
||||
// The entry keeps the color in its own model when its hex code would read back as
|
||||
// another color: a Lab or CMYK color the picker opened on and its OK left unchanged.
|
||||
*entry = super::document_setup::grid_color_entry(&colour);
|
||||
}
|
||||
}
|
||||
// The dialog previews the new colour when it comes back: end this picker's interaction, as
|
||||
|
||||
@@ -232,7 +232,7 @@ fn pending_grid_pair(fields: &Map<String, Value>) -> Option<Vec<Value>> {
|
||||
/// Color `c` as an entry of the dialog's `gridColors` that `document.setup` reads back as `c`: its
|
||||
/// hex code when that reads back as `c`, else the color in its own model (a Lab or CMYK color, or
|
||||
/// an RGB color whose hex code is converted to display RGB or rounded).
|
||||
fn grid_color_entry(c: &vectorcraft_color::Color) -> Value {
|
||||
pub(super) fn grid_color_entry(c: &vectorcraft_color::Color) -> Value {
|
||||
let hex = json!(c.to_hex());
|
||||
if vectorcraft_engine::cmd::color_value(&hex) == Some(*c) { hex } else { serde_json::to_value(c).unwrap_or(hex) }
|
||||
}
|
||||
@@ -919,6 +919,29 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// The Color Picker's OK on a grid color it opened on, unchanged, keeps that color in its own
|
||||
/// model: a Lab or CMYK color stays Lab or CMYK through Document Setup's OK, and a hex color
|
||||
/// stays that hex color. The other color stays as it was.
|
||||
#[test]
|
||||
fn an_unchanged_grid_color_picker_ok_keeps_the_colors_own_model() {
|
||||
for (index, grid) in
|
||||
[(0, json!(["lab 55 60 40", "#cccccc"])), (1, json!(["#ffffff", "cmyk 0 50 100 0"])), (1, json!(["lab 55 60 40", "#123456"]))]
|
||||
{
|
||||
let (mut app, ctx) = transparency_tab_on(grid.clone());
|
||||
let before = app.session.active().unwrap().doc.setup.grid_colors;
|
||||
click_grid_box(&mut app, &ctx, index);
|
||||
let d = app.ui.dialog.as_ref().unwrap();
|
||||
assert_eq!((d.kind.as_str(), d.fields.get("color").and_then(vectorcraft_engine::cmd::color_value)), ("colorPicker", Some(before[index])));
|
||||
crate::dialogs::confirm(&mut app).unwrap();
|
||||
let d = app.ui.dialog.as_ref().unwrap();
|
||||
assert_eq!((d.kind.as_str(), dialog_grid(d)), (super::KIND, before.to_vec()), "{grid}");
|
||||
crate::dialogs::confirm(&mut app).unwrap();
|
||||
assert!(app.ui.dialog.is_none());
|
||||
assert_eq!(app.session.active().unwrap().doc.setup.grid_colors, before, "{grid}");
|
||||
assert_eq!(undo_steps(&app), 1, "{grid}: only the document.setup that opened the tab on the grid");
|
||||
}
|
||||
}
|
||||
|
||||
/// When Grid Colors holds a preset name (in any letter case) or a list that is not two colors,
|
||||
/// the boxes and hex fields show the preset's pair or the document's, a box opens the Color
|
||||
/// Picker on its color, and the picker's OK sets only that entry of a two-color list.
|
||||
|
||||
Reference in New Issue
Block a user