diff --git a/crates/color/src/css.rs b/crates/color/src/css.rs index 6fa17e0df1..c8b1e84ab2 100644 --- a/crates/color/src/css.rs +++ b/crates/color/src/css.rs @@ -36,8 +36,8 @@ pub struct CssColor { /// [`MAX_CHARS`] characters. Values are clamped where CSS clamps them when it parses them: red, green /// and blue to 0..=255 (0..=100 %), alpha to 0..=1, a negative HSL saturation to 0, OKLCH lightness /// to 0..=1 and chroma to 0 or more. `none` reads as 0, as CSS reads a missing component when it -/// converts a color. Spaces, tabs and line breaks may surround the text and its arguments; other -/// whitespace is not CSS whitespace and makes the text invalid. An `hsl()` color whose red, green or +/// converts a color. Spaces, tabs, line breaks and form feeds may surround the text and its +/// arguments; other whitespace is not CSS whitespace and makes the text invalid. An `hsl()` color whose red, green or /// blue falls outside 0..=1 has that channel clipped, and an `oklch()` color outside sRGB keeps its /// lightness and hue and takes the largest chroma that fits ([`oklab::from_oklch`]); CSS maps such /// colors into the gamut by its own rules instead. An `hsl()` color whose saturation and lightness @@ -66,8 +66,9 @@ pub fn parse(text: &str) -> Option { } } -/// `s` without the CSS whitespace (spaces, tabs and line breaks) around it -/// (). +/// `s` without the CSS whitespace (spaces, tabs, line breaks and form feeds, which CSS reads as line +/// breaks) around it (, +/// ). fn css_trim(s: &str) -> &str { s.trim_matches(|c: char| c.is_ascii_whitespace()) } @@ -341,7 +342,9 @@ mod tests { fn only_spaces_tabs_and_line_breaks_are_whitespace() { assert_eq!(bytes("\t rgb(51,\n102 ,\t153)\r\n"), ([51, 102, 153], None)); assert_eq!(bytes("rgb(51 102 153 /\t0.5 )"), ([51, 102, 153], Some(128))); - for bad in ["rgb(51,\u{a0}102,153)", "rgb(51,102,153\u{3000})", "\u{a0}#336699", "#336699\u{2003}", "rgb(0 0 0 /\u{a0}1)"] { + assert_eq!(bytes("\u{c}#336699\u{c}"), ([51, 102, 153], None), "CSS reads a form feed as a line break"); + assert_eq!(bytes("rgb(51\u{c}102\u{c}153)"), ([51, 102, 153], None)); + for bad in ["rgb(51,\u{a0}102,153)", "rgb(51,102,153\u{3000})", "\u{a0}#336699", "#336699\u{2003}", "rgb(0 0 0 /\u{a0}1)", "\u{b}#336699"] { assert_eq!(parse(bad), None, "{bad:?}"); } } diff --git a/crates/engine/src/cmd/colorcmds.rs b/crates/engine/src/cmd/colorcmds.rs index 071f2ed3f7..8890f9eec9 100644 --- a/crates/engine/src/cmd/colorcmds.rs +++ b/crates/engine/src/cmd/colorcmds.rs @@ -141,7 +141,7 @@ pub fn specs() -> Vec { "Parse Color", [], None, - "{text: a hex color or a CSS color (a hex color is #rgb, #rgba, #rrggbb or #rrggbbaa, and the # may be left out; a CSS color is rgb() or rgba() or hsl() or hsla(), space-separated with an optional / alpha or in the comma-separated legacy form, or oklch(), as CSS Color 4 defines them: https://www.w3.org/TR/css-color-4/#hex-notation, https://www.w3.org/TR/css-color-4/#rgb-functions, https://www.w3.org/TR/css-color-4/#the-hsl-notation, https://www.w3.org/TR/css-color-4/#specifying-oklab-oklch; at most 48 characters, the spaces, tabs and line breaks around it not counted)} the color the text gives → {color: an RGB color, the numbers written for a hex color, rgb() or hsl() and the sRGB of an oklch() color, whose chroma is lowered to fit sRGB; hex: \"#rrggbb\" of it; opacity: 0..100, the text's alpha, or null when the text gives none (CSS reads that as 100)}; values are clamped where CSS clamps them when it parses them, and an hsl() channel outside 0..255 is clipped; an hsl() color whose saturation and lightness are so large that a channel overflows to an infinity is an error, as is any other text, and so is oklch() while the working RGB space is not sRGB", + "{text: a hex color or a CSS color (a hex color is #rgb, #rgba, #rrggbb or #rrggbbaa, and the # may be left out; a CSS color is rgb() or rgba() or hsl() or hsla(), space-separated with an optional / alpha or in the comma-separated legacy form, or oklch(), as CSS Color 4 defines them: https://www.w3.org/TR/css-color-4/#hex-notation, https://www.w3.org/TR/css-color-4/#rgb-functions, https://www.w3.org/TR/css-color-4/#the-hsl-notation, https://www.w3.org/TR/css-color-4/#specifying-oklab-oklch; at most 48 characters, the spaces, tabs, line breaks and form feeds around it not counted)} the color the text gives → {color: an RGB color, the numbers written for a hex color, rgb() or hsl() and the sRGB of an oklch() color, whose chroma is lowered to fit sRGB; hex: \"#rrggbb\" of it; opacity: 0..100, the text's alpha, or null when the text gives none (CSS reads that as 100)}; values are clamped where CSS clamps them when it parses them, and an hsl() channel outside 0..255 is clipped; an hsl() color whose saturation and lightness are so large that a channel overflows to an infinity is an error, as is any other text, and so is oklch() while the working RGB space is not sRGB", always, parse_color ), diff --git a/crates/ui-egui/src/panels/color_wheel.rs b/crates/ui-egui/src/panels/color_wheel.rs index c6872ac689..a0f0584ef2 100644 --- a/crates/ui-egui/src/panels/color_wheel.rs +++ b/crates/ui-egui/src/panels/color_wheel.rs @@ -445,16 +445,25 @@ fn hex_shown(app: &VectorcraftApp, tgt: &Target, color: Option<&Color>) -> Strin hex_text(color, live.then_some(o)) } -/// The Show Hex Field row: a `#` and a field as wide as the row, showing `shown`. A committed text -/// goes through `color.parse`; a text it refuses shows its error in the status bar and changes -/// nothing. True when a text was committed. -fn hex_row(app: &mut VectorcraftApp, ui: &mut Ui, shown: &str) -> bool { +/// The hex field's id for `tgt`: it names the document, the selected objects or text characters, +/// the appearance item, the proxy and the gradient stop or freeform point, but not the color, so a +/// text typed for one of them is dropped when another one is targeted, even one showing the same hex. +fn hex_field_id(app: &VectorcraftApp, tgt: &Target) -> impl std::hash::Hash + use<> { + let o = Owner::of(app, tgt, Color::rgb8(0, 0, 0)); + ("color-wheel-hex", o.doc, o.selection, o.text_selection, o.appearance_item, o.fill, o.slot) +} + +/// The Show Hex Field row for `tgt`: a `#` and a field as wide as the row, showing `shown`. A +/// committed text goes through `color.parse`; a text it refuses shows its error in the status bar +/// and changes nothing. True when a text was committed. +fn hex_row(app: &mut VectorcraftApp, ui: &mut Ui, tgt: &Target, shown: &str) -> bool { let mut typed = None; + let id = hex_field_id(app, tgt); ui.horizontal(|ui| { let t = Tokens::get(ui.ctx()); ui.label(egui::RichText::new("#").size(15.0).color(t.text)); let width = ui.available_width(); - typed = widgets::text_field_named(ui, "color-wheel-hex", Some(tl!("Hex or CSS color")), Some(shown), width, 1); + typed = widgets::text_field_named(ui, id, Some(tl!("Hex or CSS color")), Some(shown), width, 1); }); let Some(text) = typed else { return false }; match app.run("color.parse", json!({ "text": text })) { @@ -760,7 +769,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { ui.add_space(4.0); // A pick or an OKLCH edit read in the frame that commits a text was made on the color // before the text, so it is not applied. - if hex_row(app, ui, &hex) { + if hex_row(app, ui, &tgt, &hex) { return; } } diff --git a/crates/ui-egui/src/panels/tests_color_wheel_hex.rs b/crates/ui-egui/src/panels/tests_color_wheel_hex.rs index 7518cf6d2d..e53a7d203e 100644 --- a/crates/ui-egui/src/panels/tests_color_wheel_hex.rs +++ b/crates/ui-egui/src/panels/tests_color_wheel_hex.rs @@ -285,6 +285,35 @@ fn a_wheel_click_after_typing_applies_both() { assert_eq!(undo_count(h.state()), before + 2); } +/// A text typed for one rectangle is dropped when the selection changes to another showing the same +/// hex: Enter then changes neither of them. +#[test] +fn a_text_typed_for_one_object_does_not_go_to_another_with_the_same_hex() { + let mut app = app(); + let a = selected(&app)[0]; + app.run("shape.rectangle", json!({"x": 100, "y": 100, "width": 50, "height": 50})).unwrap(); + app.run("paint.setFill", json!({"color": "#ff0000"})).unwrap(); + let b = selected(&app)[0]; + app.run("select.set", json!({"ids": [a]})).unwrap(); + let before = undo_count(&app); + let mut h = harness(app); + h.get_by_role_and_label(Role::TextInput, "Hex or CSS color").click(); + h.run_steps(1); + h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A); + h.run_steps(1); + h.get_by_role_and_label(Role::TextInput, "Hex or CSS color").type_text("#33669980"); + h.run_steps(1); + h.state_mut().run("select.set", json!({"ids": [b]})).unwrap(); + h.run_steps(2); + h.key_press(egui::Key::Enter); + h.run_steps(2); + for id in [a, b] { + assert_eq!(rgb8(&fill_color(h.state(), id)), [255, 0, 0]); + } + assert_eq!(h.state().session.proxy_opacity(false), Some(1.0)); + assert_eq!(undo_count(h.state()), before, "nothing was applied"); +} + /// A press on the opacity slider that takes the focus from an edited field applies the text and /// keeps the opacity the press set: a gradient stop takes both. #[test] diff --git a/docs/mcp.md b/docs/mcp.md index 258f173cd8..b383cff868 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -701,15 +701,15 @@ the `#`, or a CSS [`rgb()` or `rgba()`](https://www.w3.org/TR/css-color-4/#rgb-f [`oklch()`](https://www.w3.org/TR/css-color-4/#specifying-oklab-oklch) color. The functions take the space-separated form with an optional `/ alpha`, and `rgb()` and `hsl()` also take the comma-separated legacy form. Hues take `deg`, `grad`, `rad` and `turn`, and `none` reads as 0. The text has at most 48 characters, not counting -the spaces, tabs and line breaks around it; other whitespace makes the text an error. A hex color, `rgb()` and -`hsl()` give their numbers as the document's RGB numbers, as `#rrggbb` does in every command, and -`oklch()` gives its sRGB color, with the chroma lowered to fit sRGB as OKLCH Sliders lower it; `oklch()` is an error -while the working RGB space is not sRGB. Values are clamped where CSS clamps them when it parses them (red, green -and blue to 0 to 255, the alpha to 0 to 1, a negative saturation to 0, OKLCH lightness to 0 to 1 and a negative -chroma to 0), and an `hsl()` color's red, green or blue outside 0 to 255 is clipped. An `hsl()` color whose -saturation and lightness are so large that a channel overflows to an infinity is an error. `opacity` is the alpha -as a percentage, or `null` when the text gives none. Any other text is an error. The color parameters of the other -commands still refuse `#rrggbbaa` and `#rgba`. +the spaces, tabs, line breaks and form feeds around it; other whitespace, such as a vertical tab or a no-break space, +makes the text an error. A hex color, `rgb()` and `hsl()` give their numbers as the document's RGB numbers, as +`#rrggbb` does in every command, and `oklch()` gives its sRGB color, with the chroma lowered to fit sRGB as OKLCH +Sliders lower it; `oklch()` is an error while the working RGB space is not sRGB. Values are clamped where CSS clamps +them when it parses them (red, green and blue to 0 to 255, the alpha to 0 to 1, a negative saturation to 0, OKLCH +lightness to 0 to 1 and a negative chroma to 0), and an `hsl()` color's red, green or blue outside 0 to 255 is +clipped. An `hsl()` color whose saturation and lightness are so large that a channel overflows to an infinity is an +error. `opacity` is the alpha as a percentage, or `null` when the text gives none. Any other text is an error. The +color parameters of the other commands still refuse `#rrggbbaa` and `#rgba`. 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