From f627d3010017680647e34016d6b21407434d6859 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 17 Nov 2026 06:53:13 -0800 Subject: [PATCH] Color Wheel: contrast counts a picked mesh point's opacity and follows the Mesh tool's picks color.contrast with no `color` now draws a gradient mesh's first picked point at its own opacity, as the Fill proxy shows it. Picked points of one color at different opacities have no one answer, so the query asks for `opacity` then. The Color Wheel's contrast row keys its kept answer on the picked mesh points too: picking another point changes no document revision, and the row showed the earlier point's ratio. --- crates/engine/src/cmd/colorcmds.rs | 54 +++++++++++++++---- crates/engine/src/tests/colorcontrast.rs | 39 ++++++++++++++ crates/ui-egui/src/panels/color_wheel.rs | 20 +++++-- .../src/panels/tests_color_wheel_contrast.rs | 32 +++++++++++ docs/mcp.md | 4 +- 5 files changed, 133 insertions(+), 16 deletions(-) diff --git a/crates/engine/src/cmd/colorcmds.rs b/crates/engine/src/cmd/colorcmds.rs index 74c10c7dab..9a1c3c328e 100644 --- a/crates/engine/src/cmd/colorcmds.rs +++ b/crates/engine/src/cmd/colorcmds.rs @@ -141,7 +141,7 @@ pub fn specs() -> Vec { "Contrast", [], None, - "{color?: a color (#rrggbbaa and #rgba are refused; default the active Fill or Stroke proxy's color, or its selected gradient stop or freeform point; a picked object's solid color mixed towards the fill that stains its dynamic symbol instance), against?: a color or \"below\" (default: the color VectorCraft draws at the center of the selection's bounds with the selected objects hidden, as the canvas shows it without template layers and guides; a picked object of a dynamic symbol instance hides only itself and is measured at its own center; needs a selection), opacity?: 0..100 (default the opacity the proxy's color is drawn with: the object's opacity times that of its groups and layer (for a picked symbol object, also of its instance and the groups of the symbol's art around it), its fill's or stroke's row (not for type's character paint) and a stop's or point's own, a translucent group or layer drawing the art inside it, the art behind the object included, over what is outside it; 100 for a given `color`)} the WCAG 2 contrast ratio (https://www.w3.org/TR/WCAG22/#dfn-contrast-ratio) of the color, drawn at that opacity over the background with the blend modes the proxy's color is drawn with (its row's, the object's and those of its groups and layer, innermost first, each blending the result with the art below it inside the object, groups and layer around it, the object's rows below its row included (with `against` or `opacity` given, with the background); inside an object, group or layer that isolates its blending, the modes blend only with the art it holds; Normal for a given `color`), against the background, in display sRGB: normal text needs 4.5:1 for AA and 7:1 for AAA, large text 3:1 and 4.5:1, and graphics 3:1 for AA; the unrounded ratio is compared → {ratio, against: \"#rrggbb\" (the background in working RGB, as `against` takes it; the sampled one rounded to 8 bits, so giving it back answers the same ratio unless `opacity` is omitted and a group or layer around the object is translucent, has a blend mode or isolates its blending, or the measured row is translucent or blends over rows below it, since a given background stands for the art below each such level), normalText: {aa, aaa}, largeText: {aa, aaa}, graphics: {aa}}", + "{color?: a color (#rrggbbaa and #rgba are refused; default the active Fill or Stroke proxy's color, or its selected gradient stop or freeform point, or a gradient mesh's first picked point; a picked object's solid color mixed towards the fill that stains its dynamic symbol instance), against?: a color or \"below\" (default: the color VectorCraft draws at the center of the selection's bounds with the selected objects hidden, as the canvas shows it without template layers and guides; a picked object of a dynamic symbol instance hides only itself and is measured at its own center; needs a selection), opacity?: 0..100 (default the opacity the proxy's color is drawn with: the object's opacity times that of its groups and layer (for a picked symbol object, also of its instance and the groups of the symbol's art around it), its fill's or stroke's row (not for type's character paint) and a stop's or point's own, a translucent group or layer drawing the art inside it, the art behind the object included, over what is outside it; 100 for a given `color`)} the WCAG 2 contrast ratio (https://www.w3.org/TR/WCAG22/#dfn-contrast-ratio) of the color, drawn at that opacity over the background with the blend modes the proxy's color is drawn with (its row's, the object's and those of its groups and layer, innermost first, each blending the result with the art below it inside the object, groups and layer around it, the object's rows below its row included (with `against` or `opacity` given, with the background); inside an object, group or layer that isolates its blending, the modes blend only with the art it holds; Normal for a given `color`), against the background, in display sRGB: normal text needs 4.5:1 for AA and 7:1 for AAA, large text 3:1 and 4.5:1, and graphics 3:1 for AA; the unrounded ratio is compared → {ratio, against: \"#rrggbb\" (the background in working RGB, as `against` takes it; the sampled one rounded to 8 bits, so giving it back answers the same ratio unless `opacity` is omitted and a group or layer around the object is translucent, has a blend mode or isolates its blending, or the measured row is translucent or blends over rows below it, since a given background stands for the art below each such level), normalText: {aa, aaa}, largeText: {aa, aaa}, graphics: {aa}}", always, contrast ), @@ -1209,7 +1209,8 @@ fn draws_alike(a: &Drawn, b: &Drawn, given_opacity: bool, below: bool) -> bool { && la.iter().zip(&lb).all(|(x, y)| level(x, y)) } -/// The active proxy's color (its selected gradient stop or freeform point, else the first) and how +/// The active proxy's color (its selected gradient stop or freeform point, else the first, or a +/// gradient mesh's first picked point, each at its own opacity) and how /// it is drawn ([`Drawn`]). With `given_opacity`, the opacities of the selected objects are not /// compared, since the given opacity takes their place; with `below` (the background is sampled), /// objects in different translucent or blending groups or layers differ, since the art below each @@ -1221,7 +1222,19 @@ fn proxy_color(s: &Session, given_opacity: bool, below: bool) -> std::result::Re return Err("the selected objects' colors differ: give `color`".into()); } let (fill, stroke_paint) = s.proxy_paints(); - let source = || s.proxy_source(stroke).map(|(n, item)| drawn_settings(s, &n, item, stroke)).transpose(); + // The Fill proxy shows a gradient mesh's picked points, each drawn at its own opacity. + let points = if stroke { vec![] } else { s.fill_mesh_points() }; + let source = || { + s.proxy_source(stroke) + .map(|(n, item)| { + let d = drawn_settings(s, &n, item, stroke)?; + Ok::<_, String>(match picked_opacities(&n, &points).first() { + Some(o) => at(&d, *o), + None => d, + }) + }) + .transpose() + }; let drawing; // Selected objects that share a color but draw it differently have no one answer. if let Some(st) = s.active().filter(|st| st.selection.symbol_part.is_none()) { @@ -1233,7 +1246,13 @@ fn proxy_color(s: &Session, given_opacity: bool, below: bool) -> std::result::Re let mut drawn = vec![]; for n in st.selection.subjects().iter().filter_map(|id| st.doc.node(*id)) { for (n, item) in shown_sources(n, item, stroke).into_iter().filter(|(c, _)| shown(c.id)) { - drawn.push(drawn_settings(s, n, item, stroke)?); + let d = drawn_settings(s, n, item, stroke)?; + let picked = picked_opacities(n, &points); + if picked.is_empty() { + drawn.push(d); + } else { + drawn.extend(picked.into_iter().map(|o| at(&d, o))); + } } } drawing = match drawn.first() { @@ -1246,27 +1265,40 @@ fn proxy_color(s: &Session, given_opacity: bool, below: bool) -> std::result::Re } else { drawing = source()?.unwrap_or_default(); } - // A stop's or point's opacity applies where its paint is drawn: in its row, else in the object. - let at = |opacity: f32| match drawing.row { - Some((m, o)) => Drawn { row: Some((m, o * opacity)), ..drawing.clone() }, - None => Drawn { opacity: drawing.opacity * opacity, ..drawing.clone() }, - }; let no_color = || "the active fill or stroke has no color: give `color`".to_string(); match if stroke { stroke_paint } else { fill } { Paint::Solid { color, .. } => Ok((stained(s, color), drawing)), Paint::Gradient(g) if g.gradient.kind == vectorcraft_color::GradientKind::Freeform => { let f = g.freeform_on(vectorcraft_geom::Rect::new(0.0, 0.0, 1.0, 1.0)); let i = s.selected_freeform_point().filter(|i| *i < f.points.len()).unwrap_or(0); - f.points.get(i).map(|pt| (pt.color, at(pt.opacity))).ok_or_else(no_color) + f.points.get(i).map(|pt| (pt.color, at(&drawing, pt.opacity))).ok_or_else(no_color) } Paint::Gradient(g) => { let i = s.selected_stop().unwrap_or(0).min(g.gradient.stops.len().saturating_sub(1)); - g.gradient.stops.get(i).map(|st| (st.color, at(st.opacity))).ok_or_else(no_color) + g.gradient.stops.get(i).map(|st| (st.color, at(&drawing, st.opacity))).ok_or_else(no_color) } Paint::None | Paint::Pattern { .. } => Err(no_color()), } } +/// `drawing` with a stop's or point's `opacity`, which applies where its paint is drawn: in its +/// row, else in the object. +fn at(drawing: &Drawn, opacity: f32) -> Drawn { + match drawing.row { + Some((m, o)) => Drawn { row: Some((m, o * opacity)), ..drawing.clone() }, + None => Drawn { opacity: drawing.opacity * opacity, ..drawing.clone() }, + } +} + +/// The opacities of the picked points of gradient mesh `n` ([`Session::fill_mesh_points`]), whose +/// colors the Fill proxy shows for it: empty for another object or with no points picked. +fn picked_opacities(n: &Node, points: &[(NodeId, Vec)]) -> Vec { + match (&n.kind, points.iter().find(|(id, _)| *id == n.id)) { + (NodeKind::Mesh(m), Some((_, v))) => v.iter().filter_map(|i| m.points.get(*i)).map(|q| q.opacity).collect(), + _ => vec![], + } +} + /// The objects whose fill or stroke the proxy shows for selected object `n`, with the appearance /// item each shows: `n` itself when it holds such an item, else every painted object inside it /// ([`Session::proxy_source`] shows the first). diff --git a/crates/engine/src/tests/colorcontrast.rs b/crates/engine/src/tests/colorcontrast.rs index f9d1643a2e..8e91d95e5c 100644 --- a/crates/engine/src/tests/colorcontrast.rs +++ b/crates/engine/src/tests/colorcontrast.rs @@ -610,6 +610,45 @@ fn a_gradient_stop_counts_with_its_own_opacity() { assert!((ratio(&r) - gray).abs() < 1e-3, "the first stop, black at 50 %: {r}"); } +/// Picks the points of the selected gradient mesh inside the box from `a` to `b` with the Lasso tool. +fn pick_points(s: &mut Session, a: (f64, f64), b: (f64, f64)) { + use vectorcraft_tools::{PointerEvent, PointerKind}; + let v = ViewInfo::default(); + s.select_tool("lasso", v).unwrap(); + let pts = [(a.0, a.1), (b.0, a.1), (b.0, b.1), (a.0, b.1), (a.0, b.1)]; + for (i, (x, y)) in pts.iter().enumerate() { + let kind = match i { + 0 => PointerKind::Down, + 4 => PointerKind::Up, + _ => PointerKind::Drag, + }; + s.pointer(&PointerEvent::new(kind, *x, *y), v).unwrap(); + } +} + +#[test] +fn a_picked_mesh_point_counts_with_its_own_opacity() { + let mut s = session(); + s.execute("shape.rectangle", &json!({"x": 50, "y": 50, "width": 100, "height": 100})).unwrap(); + s.execute("paint.setFill", &json!({"color": "#000000"})).unwrap(); + s.execute("object.mesh.create", &json!({"rows": 1, "cols": 1})).unwrap(); + // The top-left point at 50 %, the top-right one opaque. + pick_points(&mut s, (40.0, 40.0), (60.0, 60.0)); + assert_eq!(s.fill_mesh_points().first().map(|(_, v)| v.len()), Some(1), "one point is picked"); + s.execute("transparency.set", &json!({"opacity": 50})).unwrap(); + let r = s.execute("color.contrast", &json!({})).unwrap(); + let gray = vectorcraft_color::wcag::contrast_ratio([0.5; 3], [1.0; 3]); + assert!((ratio(&r) - gray).abs() < 1e-3, "the picked point, black at 50 %: {r}"); + pick_points(&mut s, (140.0, 40.0), (160.0, 60.0)); + let r = s.execute("color.contrast", &json!({})).unwrap(); + assert!((ratio(&r) - 21.0).abs() < 1e-3, "the opaque point: {r}"); + // Both picked: one color at two opacities has no one answer, unless `opacity` is given. + pick_points(&mut s, (40.0, 40.0), (160.0, 60.0)); + let both = s.execute("color.contrast", &json!({})).unwrap_err().to_string(); + assert!(both.contains("differ"), "{both}"); + assert!(s.execute("color.contrast", &json!({"opacity": 100})).is_ok()); +} + #[test] fn below_needs_a_selection_and_bad_params_are_errors() { let mut s = session(); diff --git a/crates/ui-egui/src/panels/color_wheel.rs b/crates/ui-egui/src/panels/color_wheel.rs index 5bc807f4bb..1c22572229 100644 --- a/crates/ui-egui/src/panels/color_wheel.rs +++ b/crates/ui-egui/src/panels/color_wheel.rs @@ -313,9 +313,22 @@ type BehindKey = (u64, u64, Vec, Option>, String, Under); /// What the contrast row's answer depends on: the background, the active proxy's paint (with nothing /// selected it is the default for new art, which no revision counts), the document, its revision, /// the selection and its picked symbol object, the proxy, the appearance item, the selected -/// gradient stop and freeform point, the on-screen Appearance of Black and the color settings. -pub(super) type RatioKey = - (String, String, Option<(u64, u64)>, Vec, Option>, bool, Option, Option, Option, String, Under); +/// gradient stop and freeform point, the on-screen Appearance of Black, the color settings and the +/// picked gradient mesh points, which the active tool keeps without a revision. +pub(super) type RatioKey = ( + String, + String, + Option<(u64, u64)>, + Vec, + Option>, + bool, + Option, + Option, + Option, + String, + Under, + Vec<(NodeId, Vec)>, +); /// The active color settings and profiles generation, which change how a color displays without /// a document revision (Edit › Color Settings, a document's assigned profiles). @@ -382,6 +395,7 @@ fn contrast_row(app: &mut VectorcraftApp, ui: &mut Ui) { app.session.selected_freeform_point(), app.session.prefs.black_on_screen.clone(), under(), + app.session.fill_mesh_points(), ); match pstate::)>>(ui.ctx(), RATIO) { Some((k, r)) if k == key => r, diff --git a/crates/ui-egui/src/panels/tests_color_wheel_contrast.rs b/crates/ui-egui/src/panels/tests_color_wheel_contrast.rs index b07ab67758..c777596f36 100644 --- a/crates/ui-egui/src/panels/tests_color_wheel_contrast.rs +++ b/crates/ui-egui/src/panels/tests_color_wheel_contrast.rs @@ -224,6 +224,38 @@ fn the_row_answers_as_color_contrast_does_inside_a_translucent_group() { assert!(shows(&h, &want), "the row shows {want}, not {given}"); } +/// Clicks the point of the selected gradient mesh at (`x`, `y`) with the Mesh tool, which picks it +/// alone. +fn pick_point(s: &mut Session, x: f64, y: f64) { + use vectorcraft_tools::{PointerEvent, PointerKind}; + let v = vectorcraft_engine::ViewInfo::default(); + s.select_tool("mesh", v).unwrap(); + for kind in [PointerKind::Down, PointerKind::Up] { + s.pointer(&PointerEvent::new(kind, x, y), v).unwrap(); + } +} + +/// The row asks again when the Mesh tool picks another point, which changes no document revision: +/// here a black point at 50 % after an opaque black one. +#[test] +fn the_row_follows_the_picked_mesh_points() { + let mut app = app("#000000"); + app.run("object.mesh.create", json!({"rows": 1, "cols": 1})).unwrap(); + app.run("ui.colorWheel", json!({"contrastBackground": "#ffffff"})).unwrap(); + pick_point(&mut app.session, 40.0, 40.0); + app.run("transparency.set", json!({"opacity": 50})).unwrap(); + pick_point(&mut app.session, 90.0, 40.0); + let mut h = harness(app); + assert!(shows(&h, "21.00:1"), "the opaque top-right point"); + let revision = |h: &Harness<'static, VectorcraftApp>| h.state().session.active().map(|d| d.revision); + let before = revision(&h); + pick_point(&mut h.state_mut().session, 40.0, 40.0); + assert_eq!(revision(&h), before, "a pick is no document change"); + h.run_steps(2); + let gray = wcag::ratio_text(wcag::contrast_ratio([0.5; 3], [1.0; 3])); + assert!(shows(&h, &gray), "the top-left point at 50 % shows {gray}"); +} + #[test] fn ui_color_wheel_sets_the_keys_given_and_refuses_bad_values() { let mut app = app("#000000"); diff --git a/docs/mcp.md b/docs/mcp.md index d287e3fdc1..ed21b763f9 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -1091,8 +1091,8 @@ limit. ([definition](https://www.w3.org/TR/WCAG22/#dfn-contrast-ratio)) of a color against a background, from 1 to 21, and whether it meets AA and AAA for normal text (4.5:1 and 7:1), large text (3:1 and 4.5:1) and graphics (3:1, AA only): `{ratio, against, normalText: {aa, aaa}, largeText: {aa, aaa}, graphics: {aa}}`. The results compare the unrounded -ratio. `color` defaults to the active Fill or Stroke proxy's color (its selected gradient stop or freeform point; for -a picked object of a dynamic symbol instance, its solid color mixed towards the fill that stains the instance), at +ratio. `color` defaults to the active Fill or Stroke proxy's color (its selected gradient stop or freeform point, or +the first picked point of a gradient mesh; for a picked object of a dynamic symbol instance, its solid color mixed towards the fill that stains the instance), at the opacity it is drawn with (the object's opacity times that of its groups and layer, and for a picked object of a dynamic symbol instance also of the instance and the groups of the symbol's art around it, of its fill or stroke row, which type's character paint does not use, and of a stop or point; a translucent group or layer draws the art inside