mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 04:24:51 +00:00
Color Wheel: color vision chips show hex as the Color panel does
- The chips report and show their hex value as the Color panel's hex field writes it (828282), not as #828282. - The saved Show Color Vision Preview setting reads through the Color panel's existing reader (`deserialize_rgb_percent`) instead of a copy. - The key test moves the keyboard focus to each chip with Tab before it presses Enter and Space. - A test checks that `{"view": "box", "channel": "red", "vision": 1}` returns an error and leaves the view and the channel unchanged. - docs/mcp.md says the command returns `{view, channel, vision}` in one place, and that `null` keeps the setting. - The module doc says the vision setting is kept with the preferences.
This commit is contained in:
@@ -10,12 +10,15 @@
|
||||
//! The panel menu's Box View puts a box in the wheel's place: a color field with a vertical
|
||||
//! channel strip at its right, for the channel picked under Box Channel (Hue, Saturation,
|
||||
//! Brightness, Red, Green or Blue). The field shows the other two components on the Color
|
||||
//! Picker's axes. The view and the channel are kept with the preferences (`ui.colorWheel`).
|
||||
//! Picker's axes.
|
||||
//!
|
||||
//! Panel menu › Show Color Vision Preview adds a row under the wheel or the box with the color as
|
||||
//! simulated for protanopia, deuteranopia and tritanopia, through the simulations View › Proof
|
||||
//! Setup uses, and for achromatopsia as the gray with the color's CIE L*. The chips only show the
|
||||
//! simulation.
|
||||
//!
|
||||
//! The view, the channel and Show Color Vision Preview are kept with the preferences
|
||||
//! (`ui.colorWheel`).
|
||||
|
||||
use egui::{Rect, Sense, Stroke, StrokeKind, Ui, UiBuilder, vec2};
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -73,15 +76,12 @@ pub struct Settings {
|
||||
pub view: View,
|
||||
#[serde(serialize_with = "serialize_channel", deserialize_with = "deserialize_channel")]
|
||||
pub channel: Channel,
|
||||
/// Show Color Vision Preview: the row of simulated colors under the wheel or the box.
|
||||
#[serde(deserialize_with = "deserialize_flag")]
|
||||
/// Show Color Vision Preview: the row of simulated colors under the wheel or the box. A saved
|
||||
/// `true` shows the row and any other value hides it.
|
||||
#[serde(deserialize_with = "crate::panels::color::deserialize_rgb_percent")]
|
||||
pub vision: bool,
|
||||
}
|
||||
|
||||
fn deserialize_flag<'de, D: serde::Deserializer<'de>>(d: D) -> Result<bool, D::Error> {
|
||||
Ok(<Value as Deserialize>::deserialize(d)?.as_bool().unwrap_or_default())
|
||||
}
|
||||
|
||||
fn deserialize_view<'de, D: serde::Deserializer<'de>>(d: D) -> Result<View, D::Error> {
|
||||
let v = <Value as Deserialize>::deserialize(d)?;
|
||||
Ok(v.as_str().and_then(View::parse).unwrap_or_default())
|
||||
@@ -104,8 +104,9 @@ pub fn deserialize_settings<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Se
|
||||
}
|
||||
|
||||
/// `ui.colorWheel {view?, channel?, vision?}`: show the wheel or the box, pick the box's channel,
|
||||
/// and show or hide the color vision row. Keys left out keep their values; params that are not an
|
||||
/// object, another key or a value off the list are an error and change nothing. → the settings.
|
||||
/// and show or hide the color vision row. Keys left out or null keep their values; params that are
|
||||
/// not an object, another key or a value off the list are an error and change nothing. → the
|
||||
/// settings.
|
||||
pub(crate) fn command(app: &mut VectorcraftApp, p: &Value) -> Result<Value, String> {
|
||||
const KEYS: [&str; 3] = ["view", "channel", "vision"];
|
||||
match p {
|
||||
@@ -252,15 +253,16 @@ pub(crate) fn bytes(rgb: [f32; 3]) -> [u8; 3] {
|
||||
rgb.map(|v| (v.clamp(0.0, 1.0) * 255.0).round() as u8)
|
||||
}
|
||||
|
||||
/// `#rrggbb` of 0 to 255 values.
|
||||
/// The hex text of 0 to 255 values, in the form of the Color panel's hex field (`E67828`).
|
||||
pub(crate) fn hex([r, g, b]: [u8; 3]) -> String {
|
||||
format!("#{r:02x}{g:02x}{b:02x}")
|
||||
format!("{r:02X}{g:02X}{b:02X}")
|
||||
}
|
||||
|
||||
/// The Show Color Vision Preview row: four chips across the panel with `color` as each color
|
||||
/// vision deficiency shows it. Each chip takes the keyboard focus and reports its name and, with a
|
||||
/// solid color, its hex value as its value to screen readers; its tooltip shows both. A click or a
|
||||
/// key on a chip does nothing. With no solid color the chips are empty and carry only their names.
|
||||
/// key on a chip does nothing. With no solid color the chips are empty and keep their names in the tooltip and for screen
|
||||
/// readers.
|
||||
fn vision_row(ui: &mut Ui, cms: &Cms, color: Option<&Color>) {
|
||||
let t = Tokens::get(ui.ctx());
|
||||
let colors = color.map(|c| vision_colors(cms, c));
|
||||
|
||||
@@ -62,7 +62,7 @@ fn painted(h: &Harness<'static, VectorcraftApp>, cell: Rect) -> Option<Color32>
|
||||
}
|
||||
|
||||
fn color32(hex: &str) -> Color32 {
|
||||
Color32::from_hex(hex).expect("a #rrggbb value")
|
||||
Color32::from_hex(&format!("#{hex}")).expect("an RRGGBB value")
|
||||
}
|
||||
|
||||
/// The protanopia, deuteranopia and tritanopia chips show the fill as View › Proof Setup's
|
||||
@@ -81,18 +81,18 @@ fn the_chips_show_the_proof_setup_simulations_of_the_fill() {
|
||||
}
|
||||
}
|
||||
|
||||
/// The achromatopsia chip of a red fill is the gray with red's CIE L* (D50, about 54.29: #828282),
|
||||
/// The achromatopsia chip of a red fill is the gray with red's CIE L* (D50, about 54.29: 828282),
|
||||
/// and the simulations of a pure red differ from it and from each other.
|
||||
#[test]
|
||||
fn the_achromatopsia_chip_is_the_gray_of_the_colors_lightness() {
|
||||
let cms = cms::active();
|
||||
let [p, d, t, a] = vision_colors(&cms, &Color::rgb(1.0, 0.0, 0.0));
|
||||
assert_eq!(hex(a), "#828282");
|
||||
assert_eq!(hex(a), "828282");
|
||||
assert_ne!(hex(p), hex(d));
|
||||
assert_ne!(hex(t), hex(a));
|
||||
let h = harness(app(json!("#ff0000")));
|
||||
assert_eq!(value(&h, "Achromatopsia").as_deref(), Some("#828282"));
|
||||
assert_eq!(painted(&h, h.get_by_label("Achromatopsia").rect()), Some(color32("#828282")));
|
||||
assert_eq!(value(&h, "Achromatopsia").as_deref(), Some("828282"));
|
||||
assert_eq!(painted(&h, h.get_by_label("Achromatopsia").rect()), Some(color32("828282")));
|
||||
}
|
||||
|
||||
/// A gray fill shows as the same gray in every chip.
|
||||
@@ -139,18 +139,14 @@ fn the_chips_take_the_focus_and_report_their_names_and_values() {
|
||||
for name in NAMES {
|
||||
let node = h.get_by_label(name);
|
||||
assert_eq!(node.accesskit_node().role(), egui::accesskit::Role::Image, "{name}");
|
||||
assert!(node.value().is_some_and(|v| v.starts_with('#') && v.len() == 7), "{name}: {:?}", node.value());
|
||||
assert!(
|
||||
node.value().is_some_and(|v| v.len() == 6 && v.bytes().all(|b| b.is_ascii_digit() || b.is_ascii_uppercase())),
|
||||
"{name}: {:?}",
|
||||
node.value()
|
||||
);
|
||||
assert!(!node.accesskit_node().is_disabled(), "{name}");
|
||||
}
|
||||
// Tab stops on the panel's other controls first.
|
||||
for _ in 0..40 {
|
||||
if h.get_by_label(NAMES[0]).accesskit_node().is_focused() {
|
||||
break;
|
||||
}
|
||||
h.key_press(Key::Tab);
|
||||
h.run_steps(1);
|
||||
}
|
||||
assert!(h.get_by_label(NAMES[0]).accesskit_node().is_focused(), "Tab reaches {}", NAMES[0]);
|
||||
tab_to(&mut h, NAMES[0]);
|
||||
for name in &NAMES[1..] {
|
||||
h.key_press(Key::Tab);
|
||||
h.run_steps(1);
|
||||
@@ -158,6 +154,19 @@ fn the_chips_take_the_focus_and_report_their_names_and_values() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Press Tab until the chip `name` has the keyboard focus. Tab stops on the panel's other controls
|
||||
/// first.
|
||||
fn tab_to(h: &mut Harness<'static, VectorcraftApp>, name: &str) {
|
||||
for _ in 0..40 {
|
||||
if h.get_by_label(name).accesskit_node().is_focused() {
|
||||
break;
|
||||
}
|
||||
h.key_press(Key::Tab);
|
||||
h.run_steps(1);
|
||||
}
|
||||
assert!(h.get_by_label(name).accesskit_node().is_focused(), "Tab reaches {name}");
|
||||
}
|
||||
|
||||
/// A click on a chip, and Enter or Space on a focused chip, leave the fill and the undo history as
|
||||
/// they were.
|
||||
#[test]
|
||||
@@ -167,6 +176,7 @@ fn a_click_or_a_key_on_a_chip_applies_nothing() {
|
||||
for name in NAMES {
|
||||
h.get_by_label(name).click();
|
||||
h.run_steps(2);
|
||||
tab_to(&mut h, name);
|
||||
for key in [Key::Enter, Key::Space] {
|
||||
h.key_press_modifiers(Modifiers::NONE, key);
|
||||
h.run_steps(1);
|
||||
@@ -209,8 +219,9 @@ fn the_panel_menu_turns_the_row_on_and_off() {
|
||||
}
|
||||
|
||||
/// `ui.colorWheel` sets the keys given and reads them with `{}`, and `ui.inspect` reports them as
|
||||
/// `ui.color_wheel`. A value that is not a bool, params that are not an object and another key
|
||||
/// are errors that change nothing.
|
||||
/// `ui.color_wheel`. A `vision` value other than true, false or null, params that are not an object
|
||||
/// and another key are errors that change nothing, the view and the channel given with them
|
||||
/// included.
|
||||
#[test]
|
||||
fn ui_color_wheel_sets_and_reads_the_settings() {
|
||||
let mut app = VectorcraftApp::new(Session::new(), Default::default());
|
||||
@@ -218,6 +229,8 @@ fn ui_color_wheel_sets_and_reads_the_settings() {
|
||||
assert_eq!(app.run("ui.colorWheel", json!({"vision": true})).unwrap(), json!({"view": "wheel", "channel": "hue", "vision": true}));
|
||||
assert_eq!(crate::control::inspect(&app, &egui::Context::default())["ui"]["color_wheel"]["vision"], true);
|
||||
assert_eq!(app.run("ui.colorWheel", json!({"vision": "no"})).unwrap_err(), "`vision` must be true or false");
|
||||
assert_eq!(app.run("ui.colorWheel", json!({"view": "box", "channel": "red", "vision": 1})).unwrap_err(), "`vision` must be true or false");
|
||||
assert_eq!(app.run("ui.colorWheel", json!({})).unwrap(), json!({"view": "wheel", "channel": "hue", "vision": true}));
|
||||
assert_eq!(app.run("ui.colorWheel", json!("junk")).unwrap_err(), "the params must be an object, not \"junk\"");
|
||||
assert_eq!(app.run("ui.colorWheel", json!(5)).unwrap_err(), "the params must be an object, not 5");
|
||||
assert_eq!(
|
||||
|
||||
@@ -735,8 +735,7 @@ pub struct UiState {
|
||||
#[serde(default, deserialize_with = "crate::panels::color::deserialize_rgb_percent")]
|
||||
pub color_rgb_percent: bool,
|
||||
/// Color Wheel panel: the wheel or the box, the box's channel, and the color vision row
|
||||
/// (`ui.colorWheel`,
|
||||
/// [`crate::panels::color_wheel::deserialize_settings`]).
|
||||
/// (`ui.colorWheel`, [`crate::panels::color_wheel::deserialize_settings`]).
|
||||
#[serde(default, deserialize_with = "crate::panels::color_wheel::deserialize_settings")]
|
||||
pub color_wheel: crate::panels::color_wheel::Settings,
|
||||
/// Swatches panel: Show Recent Colors in its panel menu, which draws the panel's Recent Colors
|
||||
|
||||
+3
-4
@@ -623,14 +623,13 @@ and answers true when the fields show in percent.
|
||||
`ui.colorWheel {"view": "box", "channel": "red"}` shows the Color Wheel panel's box (a color field with the channel's
|
||||
strip at its right) in place of the wheel, as the panel menu's Box View and Box Channel do, and `"view": "wheel"`
|
||||
shows the wheel again. Keys left out keep their values. Params that are not an object, another key or a value off
|
||||
the list return an error and change nothing. The command returns `{view, channel}`, and both settings are kept with
|
||||
the preferences.
|
||||
the list return an error and change nothing. The command returns `{view, channel, vision}`, and the settings are
|
||||
kept with the preferences.
|
||||
`ui.colorWheel {"vision": true}` turns on the Color Wheel panel's Show Color Vision Preview, as its panel menu
|
||||
does: a row of four chips under the wheel or the box shows the current color through the protanopia, deuteranopia
|
||||
and tritanopia simulations of `view.proofSetup`, and for achromatopsia as the gray with the color's CIE L*. Each
|
||||
chip reports its name and hex value to screen readers, and a click on a chip does not apply its color. `false`
|
||||
hides the row. A value that is not a bool returns an error and changes nothing. The command returns
|
||||
`{view, channel, vision}`, and the setting is kept with the preferences.
|
||||
hides the row, and `null` keeps the setting. Any other value returns an error and changes nothing.
|
||||
|
||||
In remote desktop mode, `invoke_menu` with `{"command":"ui.sampleScreenColor","params":{}}` shows the system's
|
||||
screen color sampler and returns `null` before a color is picked: the system's sampler on macOS, the XDG desktop
|
||||
|
||||
Reference in New Issue
Block a user