Gradient mesh: a color dropped or eyedropped on a point or patch recolors it

With the Fill proxy active, a solid color dragged from the Color or
Swatches panel or a Fill proxy onto a selected gradient mesh recolors the
mesh point under the pointer (within 5 screen pixels), else the four
points around the patch there. With a mesh selected and a solid fill
color set, a plain Eyedropper click does the same with the fill color.
Both run object.mesh.setPointColor as one undo step. Another object on
top of the mesh at that place keeps the earlier behavior, and Object
Selection by Path Only does not stop a drop inside a patch.
This commit is contained in:
dotCooCoo
2026-11-14 21:17:04 -08:00
parent 0c1ac3bdc5
commit f4934644ab
6 changed files with 231 additions and 1 deletions
+10
View File
@@ -66,6 +66,16 @@ pub fn part_points(m: &GradientMesh, p: Point, point_tol: f64, line_tol: f64) ->
Some(vec![m.idx(r, c), m.idx(r, c + 1), m.idx(r + 1, c), m.idx(r + 1, c + 1)])
}
/// The points of gradient mesh `m` that a color dropped at `p`, or an Eyedropper click there,
/// recolors: the point within `point_tol`, else the four points around the patch under `p`.
pub fn color_points(m: &GradientMesh, p: Point, point_tol: f64) -> Option<Vec<usize>> {
if let Some(i) = m.point_near(p, point_tol) {
return Some(vec![i]);
}
let (r, c, ..) = m.locate(p)?;
Some(vec![m.idx(r, c), m.idx(r, c + 1), m.idx(r + 1, c), m.idx(r + 1, c + 1)])
}
/// What a press on a mesh grabs: the mesh, the point and (on the focused point) a handle.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct MeshGrab {
+94
View File
@@ -10,6 +10,10 @@
//! blend's steps, an envelope's distorted contents). Clicking an image samples its pixels
//! (averaged over the options' raster sample size; a CMYK image gives its ink amounts).
//!
//! With a gradient mesh selected, a click on one of its points gives that point the current solid
//! fill color, and a click inside one of its patches gives it to the patch's four points
//! (`object.mesh.setPointColor`).
//!
//! The Gradient panel's eyedropper sets the `stop` option (the tool to return to): the next click
//! samples the colour under the cursor into the selected gradient stop (`paint.sampleColor
//! {stop}`) and switches back to that tool.
@@ -453,6 +457,21 @@ fn image_color_at(s: &Sampler, n: &Node, p: Point) -> Option<Color> {
Some(Color::rgb(r, g, b))
}
/// The selected, editable gradient mesh and its points that an Eyedropper click at `p` recolors
/// ([`crate::meshedit::color_points`]): the topmost visible object within reach of `p` (locked art
/// included, clipping applied) must be that mesh, and the points are its point within reach of
/// `p`, else the four points around its patch under `p`.
fn selected_mesh_points(cx: &ToolContext, p: Point) -> Option<(NodeId, Vec<usize>)> {
let tol = cx.tol(5.0);
let opt = sample_options(cx);
let leaf = hit_test_visible(cx.doc, p, HitOptions { tol: opt.tol.max(tol), ..opt })?.leaf;
if !cx.selection.contains(leaf) || !cx.doc.editable()(leaf) {
return None;
}
let Some(NodeKind::Mesh(m)) = cx.doc.node(leaf).map(|n| &n.kind) else { return None };
Some((leaf, crate::meshedit::color_points(m, p, tol)?))
}
impl Tool for EyedropperTool {
fn id(&self) -> &'static str {
"eyedropper"
@@ -471,6 +490,17 @@ impl Tool for EyedropperTool {
return vec![Action::Dialog("brushKeyColor".into(), json!({ "color": color_json(&c) })), Action::SwitchTool(back)];
}
let stop = self.stop.as_ref().zip(cx.gradient_stop);
// A plain click on a selected gradient mesh gives the fill color to the point there, else
// to the four points of the patch there.
if stop.is_none()
&& !ev.mods.alt
&& !ev.mods.shift
&& let Paint::Solid { color, .. } = &cx.paint.fill
&& let Some((id, points)) = selected_mesh_points(cx, ev.pos)
{
let params = json!({ "id": id.0, "points": points, "color": color_json(color), "keepModel": true });
return vec![Action::Exec("object.mesh.setPointColor".into(), params)];
}
// Reading a source is independent of its editability. Only Alt's reverse operation
// writes to the clicked object; Shift+Alt appends its appearance to the selection.
let source = if ev.mods.alt && !ev.mods.shift && stop.is_none() {
@@ -1353,6 +1383,70 @@ mod tests {
assert_eq!(sampled_hex(&a), "#404040");
}
/// With a gradient mesh selected, a click on one of its points gives that point the fill color,
/// and a click inside a patch gives it to the patch's four points. Unselected, a click copies
/// the mesh's appearance as before, and Shift still samples.
#[test]
fn a_click_on_a_selected_mesh_gives_its_point_or_patch_the_fill_color() {
use vectorcraft_doc::{Document, GradientMesh};
let mut d = Document::new(500.0, 500.0);
let l = d.layers[0].id;
let id = d.alloc_id();
let mesh = GradientMesh::from_surface(2, 2, &|u, v| Point::new(100.0 + 100.0 * u, 100.0 + 100.0 * v), &|_, _| Color::WHITE);
d.insert(Some(l), 0, Node::new(id, NodeKind::Mesh(mesh))).unwrap();
let mut p = paint();
p.fill = Paint::solid(Color::rgb(1.0, 0.0, 0.0));
let red = color_json(&Color::rgb(1.0, 0.0, 0.0));
let set = |points: Value| {
vec![Action::Exec("object.mesh.setPointColor".into(), json!({"id": id.0, "points": points, "color": red, "keepModel": true}))]
};
let mut s = Selection::default();
let click = |s: &Selection, p: &crate::PaintDefaults, x: f64, y: f64, m: Mods| {
EyedropperTool::default().pointer(&cx(&d, s, p), &PointerEvent::new(PointerKind::Down, x, y).with_mods(m))
};
let plain = Mods::default();
let a = click(&s, &p, 125.0, 125.0, plain);
assert_eq!(a, vec![Action::Exec("appearance.copyFrom".into(), json!({"source": id.0}))], "an unselected mesh");
s.set([id]);
assert_eq!(click(&s, &p, 151.0, 149.0, plain), set(json!([4])), "the point near the click");
assert_eq!(click(&s, &p, 125.0, 125.0, plain), set(json!([0, 1, 3, 4])), "the patch's four points");
assert_eq!(click(&s, &p, 175.0, 175.0, plain), set(json!([4, 5, 7, 8])));
let shift = Mods { shift: true, ..Default::default() };
assert!(matches!(&click(&s, &p, 125.0, 125.0, shift)[0], Action::Exec(c, _) if c == "paint.sampleColor"));
let alt = Mods { alt: true, ..Default::default() };
assert!(matches!(&click(&s, &p, 125.0, 125.0, alt)[0], Action::Exec(c, v) if c == "appearance.copyFrom" && v["reverse"] == true));
// Off the mesh, or with a fill that is not a solid color, nothing goes to its points.
assert!(click(&s, &p, 400.0, 400.0, plain).is_empty());
p.fill = Paint::None;
assert!(matches!(&click(&s, &p, 125.0, 125.0, plain)[0], Action::Exec(c, _) if c == "appearance.copyFrom"));
}
/// An unselected object on top of a selected mesh's point keeps the click: it copies that
/// object's appearance, and the mesh's points keep their colors.
#[test]
fn a_click_on_an_object_over_a_selected_mesh_copies_that_object() {
use vectorcraft_doc::{Document, GradientMesh};
let mut d = Document::new(500.0, 500.0);
let l = d.layers[0].id;
let id = d.alloc_id();
let mesh = GradientMesh::from_surface(2, 2, &|u, v| Point::new(100.0 + 100.0 * u, 100.0 + 100.0 * v), &|_, _| Color::WHITE);
d.insert(Some(l), 0, Node::new(id, NodeKind::Mesh(mesh))).unwrap();
let top = d.alloc_id();
let mut cover = (*filled_rect(vectorcraft_geom::Rect::new(140.0, 140.0, 160.0, 160.0), Paint::solid(Color::BLACK))).clone();
cover.id = top;
d.insert(Some(l), usize::MAX, cover).unwrap();
let mut p = paint();
p.fill = Paint::solid(Color::rgb(1.0, 0.0, 0.0));
let mut s = Selection::default();
s.set([id]);
let a = EyedropperTool::default().pointer(&cx(&d, &s, &p), &PointerEvent::new(PointerKind::Down, 150.0, 150.0));
assert_eq!(a, vec![Action::Exec("appearance.copyFrom".into(), json!({"source": top.0}))]);
// Locked, it is still what the click samples.
d.node_mut(top).unwrap().locked = true;
let a = EyedropperTool::default().pointer(&cx(&d, &s, &p), &PointerEvent::new(PointerKind::Down, 150.0, 150.0));
assert_eq!(a, vec![Action::Exec("appearance.copyFrom".into(), json!({"source": top.0}))], "a locked object on top");
}
/// Finding the point in a mesh counts the points it looks at in every patch: a 20 × 20 mesh
/// takes more than a thousand units of work.
#[test]
+36 -1
View File
@@ -50,7 +50,9 @@ pub(super) fn context_menu(app: &mut VectorcraftApp, resp: &egui::Response, xf:
/// A panel drag ([`widgets::PanelDrag`]) dropped on art acts on the object under the pointer,
/// selected or not: a paint (swatches, a Fill/Stroke proxy, the Gradient panel's thumbnail) goes
/// to its active proxy (`paint.setFill`/`paint.setStroke` with its `ids`; a gradient fits it), the
/// to its active proxy (`paint.setFill`/`paint.setStroke` with its `ids`; a gradient fits it; a
/// solid fill color on a selected gradient mesh recolors the mesh point under the pointer, else the
/// four points of the patch there, with `object.mesh.setPointColor`), the
/// Appearance panel's thumbnail gives the object (the topmost one hit) the appearance it carries
/// (`appearance.copyFrom`), a graphic style is applied to it (`graphicStyle.apply`; with Alt, on
/// top of its appearance), and a brush is applied to it (`brush.apply`, `brush.addFromLibrary`
@@ -89,6 +91,21 @@ pub(super) fn panel_drop(app: &mut VectorcraftApp, ui: &Ui, resp: &egui::Respons
}
return;
}
// A solid fill color on a selected gradient mesh: the point under the pointer, else the four
// points around the patch there, take it. The selected meshes are looked at directly, so
// Object Selection by Path Only and the selection tolerance do not stop it.
if let widgets::PanelDrag::Paint { params, paint, .. } = &*d
&& !params.is_null()
&& let Some(color) = paint.color()
&& crate::panels::proxy_cmd(app, false) == "paint.setFill"
&& let Some((id, points)) = selected_mesh_drop(app, xf.to_doc(pos), xf.zoom)
{
let params = json!({"id": id.0, "points": points, "color": crate::panels::color_json(&color), "keepModel": true});
if let Err(e) = app.run("object.mesh.setPointColor", params) {
app.status(e);
}
return;
}
let hit = hit_at(app, xf.to_doc(pos), xf.zoom);
// A scatter, art or pattern brush from the Brushes panel dropped where there is no art: a copy
// of its art, centered where it is dropped, to edit and Alt-drag back onto the brush.
@@ -148,3 +165,21 @@ pub(super) fn panel_drop(app: &mut VectorcraftApp, ui: &Ui, resp: &egui::Respons
app.status(e);
}
}
/// The selected, editable gradient mesh and its points that a solid color dropped at document
/// point `p` at `zoom` recolors: the topmost object within 5 screen pixels of `p` must be that mesh
/// (fills count even with Object Selection by Path Only, and clipping applies), and the points are
/// its point within 5 screen pixels, else the four points around its patch under `p`.
fn selected_mesh_drop(app: &VectorcraftApp, p: vectorcraft_geom::Point, zoom: f64) -> Option<(vectorcraft_doc::NodeId, Vec<usize>)> {
let tol = 5.0 / zoom.max(1e-9);
let leaf = hit_with(app, p, zoom, |doc, p, opt| {
vectorcraft_doc::hit::hit_test(doc, p, vectorcraft_doc::hit::HitOptions { path_only: false, tol: opt.tol.max(tol), ..opt })
})?
.leaf;
let st = app.session.active()?;
if !st.selection.contains(leaf) || !st.doc.editable()(leaf) {
return None;
}
let Some(vectorcraft_doc::NodeKind::Mesh(m)) = st.doc.node(leaf).map(|n| &n.kind) else { return None };
Some((leaf, vectorcraft_tools::meshedit::color_points(m, p, tol)?))
}
+80
View File
@@ -8460,6 +8460,86 @@ fn swatches_and_proxy_paints_dropped_on_art_fill_the_object_hit() {
assert!(matches!(fill(&app), Paint::Gradient(g) if g.geom.is_none()));
}
#[test]
fn a_color_dropped_on_a_selected_mesh_recolors_the_point_or_patch_under_it() {
use vectorcraft_color::{Color, Paint};
use vectorcraft_doc::{GradientMesh, NodeId, NodeKind};
use widgets::PanelDrag;
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap();
let id = app.session.execute("shape.rectangle", &json!({"x": 50, "y": 50, "width": 100, "height": 100})).unwrap()["id"].as_u64().unwrap();
let id = NodeId(id);
app.session.execute("object.mesh.create", &json!({"ids": [id.0], "rows": 2, "columns": 2})).unwrap();
app.session.execute("select.none", &json!({})).unwrap();
let ctx = egui::Context::default();
frame(&mut app, &ctx, vec![]);
let xf = Xf::new(app.canvas_rect.unwrap(), app.view().unwrap());
let drop = |app: &mut VectorcraftApp, d: PanelDrag, at: Point| {
let at = xf.to_screen(at);
egui::DragAndDrop::set_payload(&ctx, d);
let up = egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed: false, modifiers: Default::default() };
frame(app, &ctx, vec![egui::Event::PointerMoved(at), up]);
};
let mesh = |app: &VectorcraftApp| -> GradientMesh {
match &app.session.active().unwrap().doc.node(id).unwrap().kind {
NodeKind::Mesh(m) => m.clone(),
_ => panic!("not a gradient mesh"),
}
};
let colors = |app: &VectorcraftApp| mesh(app).points.iter().map(|q| q.color).collect::<Vec<_>>();
let undo_len = |app: &VectorcraftApp| app.session.active().unwrap().history.undo.len();
let before = colors(&app);
let m = mesh(&app);
let (center, patch) = (m.points[4].p, Point::new((m.points[0].p.x + m.points[4].p.x) / 2.0, (m.points[0].p.y + m.points[4].p.y) / 2.0));
let (red, blue) = (Color::rgb(1.0, 0.0, 0.0), Color::rgb(0.0, 0.0, 1.0));
app.session.execute("select.set", &json!({"ids": [id.0]})).unwrap();
// On a mesh point: that point alone.
drop(&mut app, PanelDrag::paint(Paint::solid(red)), center);
let after = colors(&app);
assert_eq!(after[4], red);
assert_eq!((0..9).filter(|i| after[*i] != before[*i]).count(), 1, "only the point under the pointer");
// Inside a patch: its four points.
drop(&mut app, PanelDrag::paint(Paint::solid(blue)), patch);
let after = colors(&app);
assert_eq!([0, 1, 3, 4].map(|i| after[i]), [blue; 4]);
assert_eq!([2, 5, 6, 7, 8].map(|i| after[i]), [2, 5, 6, 7, 8].map(|i| before[i]));
assert!(app.session.active().unwrap().selection.contains(id), "the selection stays");
// The same color again changes nothing and records no undo step.
let n = undo_len(&app);
drop(&mut app, PanelDrag::paint(Paint::solid(blue)), patch);
assert_eq!(undo_len(&app), n);
// With Object Selection by Path Only, a drop inside a patch still reaches its points, and a
// drop 4 screen pixels outside a corner point still reaches that point.
app.session.execute("prefs.set", &json!({"key": "objectSelectionByPathOnly", "value": true})).unwrap();
let green = Color::rgb(0.0, 1.0, 0.0);
let far = Point::new((m.points[4].p.x + m.points[8].p.x) / 2.0, (m.points[4].p.y + m.points[8].p.y) / 2.0);
// Art below the patch, with its outline within the selection tolerance, is not taken for it.
let h = 1.0 / xf.zoom;
let below = app.session.execute("shape.rectangle", &json!({"x": far.x - h, "y": far.y - h, "width": 2.0 * h, "height": 2.0 * h})).unwrap()["id"]
.as_u64()
.unwrap();
app.session.execute("object.arrange.sendToBack", &json!({"ids": [below]})).unwrap();
app.session.execute("select.set", &json!({"ids": [id.0]})).unwrap();
drop(&mut app, PanelDrag::paint(Paint::solid(green)), far);
assert_eq!([4, 5, 7, 8].map(|i| colors(&app)[i]), [green; 4]);
assert_ne!(app.session.active().unwrap().doc.node(NodeId(below)).unwrap().appearance.fill_paint(), Paint::solid(green));
let off = 4.0 / xf.zoom;
drop(&mut app, PanelDrag::paint(Paint::solid(red)), Point::new(m.points[0].p.x - off, m.points[0].p.y));
assert_eq!(colors(&app)[0], red, "the corner point within reach");
assert_eq!(colors(&app)[1], blue);
// An unselected object on top of the mesh takes the drop itself.
app.session.execute("prefs.set", &json!({"key": "objectSelectionByPathOnly", "value": false})).unwrap();
let c = m.points[4].p;
let cover = app.session.execute("shape.rectangle", &json!({"x": c.x - 10.0, "y": c.y - 10.0, "width": 20, "height": 20})).unwrap()["id"]
.as_u64()
.unwrap();
app.session.execute("select.set", &json!({"ids": [id.0]})).unwrap();
let before = colors(&app);
drop(&mut app, PanelDrag::paint(Paint::solid(green)), c);
assert_eq!(colors(&app), before, "the mesh under it keeps its colors");
assert_eq!(app.session.active().unwrap().doc.node(NodeId(cover)).unwrap().appearance.fill_paint(), Paint::solid(green));
}
#[test]
fn dropping_the_appearance_thumbnail_on_art_copies_the_appearance() {
use vectorcraft_doc::NodeId;
+10
View File
@@ -11116,6 +11116,16 @@ picked point's color, and `fillMixed` compares the picked points' colors with ea
selected objects. The Transparency panel shows the picked points' opacity (blank when it differs), and the Control
bar shows the first picked point's opacity.
A solid color dragged from the Color or Swatches panel or a Fill proxy and dropped on a selected gradient mesh, while the
Fill proxy is active, recolors the mesh point under the pointer (within 5 screen pixels), else the four points around the
patch there, as `object.mesh.setPointColor {id, points, color, keepModel: true}`. With a gradient mesh selected and a
solid fill color set, a plain Eyedropper click does the same with the fill color: on a point of any selected mesh, or
inside a patch of the selected mesh the click hits. A gradient, a pattern, a fill of None, a stroke proxy or a mesh that
is not selected keeps the earlier behavior (the whole object takes the paint, and the Eyedropper copies the appearance),
and so does an unselected object on top of the mesh at that place.
See [Edit mesh objects](https://web.archive.org/web/20210601162814/https://helpx.adobe.com/pdf/illustrator_reference.pdf#page=312);
the 5-pixel reach is VectorCraft's own.
`object.mesh.movePoints {id, points: [index…], dx, dy}` (or `{items: [{id, points}…], dx, dy}` over several meshes)
moves points by (dx, dy), their handles with them; `object.mesh.setPointColor` takes `points` or `items` the same
way to colour or fade several points at once. An index out of range, an index that is not a whole number ≥ 0, or a
+1
View File
@@ -320,6 +320,7 @@ tools, never checked side by side) isn't captured row by row.
| Cmd/Ctrl: last-used selection tool; Cmd+Alt: Group Selection inside Direct Selection | done | Cmd+Alt lends Group Selection only while Direct Selection is active (Cmd borrow from the Pen keeps Direct Selection so Alt stays copy/subtract); Cmd alone lends Direct Selection the Selection tool: its bounding box shows while Cmd is held over objects selected whole and its handles scale and rotate them, and a Cmd drag moves the whole object while its direct-selected anchors stay selected (#3901; no box over a selection with anchors picked yet); engine pointer and canvas regressions |
| Eyedropper copies effective character dimensions between transformed text objects | done | Point/area and grouped text, highlighted ranges, reverse/remembered sampling, opt-outs and atomic invalid-transform rejection; `scaled_type_pick_*` (#6404) |
| Eyedropper styles highlighted characters only and preserves ranges across Type switches | done | Font/paint, Shift color-only, Unicode, Undo/Redo and stale-range regression tests; enabled non-neutral source or target object transparency is refused atomically; disable Transparency to sample supported attributes |
| Gradient mesh: a solid color dropped from the Color or Swatches panel or a Fill proxy on a selected mesh recolors the mesh point under the pointer, else the patch's four points; with the mesh selected, an Eyedropper click applies the fill color the same way | done | [Edit mesh objects](https://web.archive.org/web/20210601162814/https://helpx.adobe.com/pdf/illustrator_reference.pdf#page=312); `object.mesh.setPointColor`, one undo step, none when the color is unchanged. The 5-pixel reach for a point is VectorCraft's own. Tests `a_color_dropped_on_a_selected_mesh_recolors_the_point_or_patch_under_it`, `a_click_on_a_selected_mesh_gives_its_point_or_patch_the_fill_color`, `a_click_on_an_object_over_a_selected_mesh_copies_that_object` |
| Eyedropper reads visible sources while their layer/object is locked or outside isolation | done | Ordinary/Shift/Shift+Alt and gradient-stop reads preserve the source and selection; Alt reverse remains editable-only; hidden art stays excluded (`eyedropper_sources.rs`, #1251) |
| Add a visible stroke after drawing with inherited None/zero-width paint | done | Paint and Swap Fill/Stroke restore the existing 1 pt default; positive widths and painted zero widths stay as set; Undo/Redo and targeted appearance items tested (`stroke_visibility.rs`, #1253) |
| Double-click a stroked segment to create a width point in Width Point Edit | done | Finite-arithmetic follow-up: engine regressions and full CI passed; [Validation evidence](target-app-parity.md#evidence-what-ships-today-by-area). Command and pointer regressions cover opening, sampled widths, adjoining adjustment on/off through checkbox/OK, compound paths, Cancel/Delete and Undo/Redo; dialog layout reviewed. [Validation evidence](target-app-parity.md#evidence-what-ships-today-by-area) |