Merge pull request #8974 from dotCooCoo/fix/sample-color-stop-of-given-objects

Eyedropper: sampling into a stop of given objects reads their gradient
This commit is contained in:
BFlat
2026-11-22 10:51:39 +07:00
committed by GitHub
3 changed files with 78 additions and 5 deletions
+28 -4
View File
@@ -30,7 +30,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Sample Color",
[],
None,
"{color (#rrggbbaa and #rgba are refused), stroke?: bool (default: whichever proxy is active), ids?, stop?: index, clearStroke?: bool} sample a color into the active fill/stroke (and the selection; `ids: []` changes only the paint default, and so does a sample without ids while the Live Paint Bucket is the active tool, `stop` included); `clearStroke: true` requires `stroke: false`, `ids: []` and no `stop`, and clears the default stroke after sampling an image into the fill; with `stop`, recolor that stop of the gradient behind the proxy instead (paint.editGradient); in a CMYK document an RGB color is stored as CMYK through the color settings, as paint.setFill stores it",
"{color (#rrggbbaa and #rgba are refused), stroke?: bool (default: whichever proxy is active), ids?, stop?: index, clearStroke?: bool} sample a color into the active fill/stroke (and the selection; `ids: []` changes only the paint default, and so does a sample without ids while the Live Paint Bucket is the active tool, `stop` included); `clearStroke: true` requires `stroke: false`, `ids: []` and no `stop`, and clears the default stroke after sampling an image into the fill; with `stop`, recolor that stop of the gradient behind the proxy instead (paint.editGradient); with `stop` and objects in `ids`, whatever tool is active, the stops come from the topmost fill (or stroke) of the first object that paint.editGradient edits for those ids (a compound path for its member); in a CMYK document an RGB color is stored as CMYK through the color settings, as paint.setFill stores it",
has_doc,
sample_color
),
@@ -529,9 +529,15 @@ fn sample_color(s: &mut Session, p: &Value) -> Result<Value> {
Some(v) => {
let i = v.as_u64().map(index).ok_or_else(|| bad(C, "`stop` must be a stop index"))?;
let color = super::paint::new_color(s, p, color_value(c).ok_or_else(|| bad(C, format!("bad color {c}")))?);
let (fill, stroke_paint) = s.proxy_paints();
let Paint::Gradient(g) = (if stroke { stroke_paint } else { fill }) else {
return Err(bad(C, "the paint behind the proxy is not a gradient"));
let (paint, source) = match given_stop_paint(s, p, stroke)? {
Some(paint) => (paint, "the paint of the first object in `ids`"),
None => {
let (fill, stroke_paint) = s.proxy_paints();
(if stroke { stroke_paint } else { fill }, "the paint behind the proxy")
}
};
let Paint::Gradient(g) = paint else {
return Err(bad(C, format!("{source} is not a gradient")));
};
let mut stops = g.gradient.stops;
let n = stops.len();
@@ -550,6 +556,24 @@ fn sample_color(s: &mut Session, p: &Value) -> Result<Value> {
Ok(result)
}
/// The paint whose gradient `paint.sampleColor` recolors a stop of when `p` gives `stop` and
/// objects in `ids`: the fill (or stroke, `stroke`) of the first object `paint.editGradient` edits
/// for those ids (a compound path for its member, a group's painted objects, not the art of a graph
/// design in a series). None without `stop` or without objects in `ids`: the gradient behind the
/// proxy is used.
fn given_stop_paint(s: &Session, p: &Value, stroke: bool) -> Result<Option<Paint>> {
const C: &str = "paint.sampleColor";
if p.get("stop").is_none_or(Value::is_null) || p.get("ids").and_then(Value::as_array).is_none_or(Vec::is_empty) {
return Ok(None);
}
let q = json!({ "ids": p.get("ids"), "stroke": stroke });
let item = super::appearance::item_target(s, &q, C)?.of_kind(s, !stroke);
let ids = super::appearance::drop_design_art_rows(s, item.targets_for(s, &q, !stroke)?, item, !stroke, C)?;
let d = &s.doc()?.doc;
let Some(n) = ids.first().and_then(|id| d.node(*id)) else { return Ok(None) };
Ok(Some(super::paint::proxy_paint(n, stroke, None)))
}
// ---------- artboards ----------
fn artboard_move(s: &mut Session, p: &Value) -> Result<Value> {
+47
View File
@@ -51,6 +51,53 @@ fn sample_color_with_a_stop_recolours_that_stop_in_one_undo_step() {
);
}
/// With `stop` and objects in `ids`, the stop is recolored in the first object's gradient, not in
/// the gradient behind the proxy: the selection's, or the Live Paint Bucket's while it is active.
#[test]
fn sample_color_with_a_stop_and_ids_recolours_the_first_given_objects_gradient() {
let mut s = session();
let target = rect(&mut s, 10.0, 10.0, 100.0, 100.0);
let three = json!([{"offset": 0, "color": "#ffffff"}, {"offset": 0.5, "color": "#888888"}, {"offset": 1, "color": "#000000"}]);
s.execute("paint.setFill", &json!({"gradient": {"stops": three}})).unwrap();
let selected = rect(&mut s, 200.0, 10.0, 100.0, 100.0);
s.execute("paint.setFill", &json!({"gradient": {}})).unwrap();
s.execute("paint.sampleColor", &json!({"color": "#ff0000", "stop": 2, "ids": [target.0]})).unwrap();
assert_eq!(stop_hexes(&s, target, false), ["#ffffff", "#888888", "#ff0000"], "the given object's three stops");
assert_eq!(stop_hexes(&s, selected, false), ["#ffffff", "#000000"], "the selection");
// The bucket's solid fill, and then a two-stop gradient of its own.
s.select_tool("livePaintBucket", ViewInfo::default()).unwrap();
s.execute("paint.setFill", &json!({"color": "#00ff00"})).unwrap();
s.execute("paint.sampleColor", &json!({"color": "#0000ff", "stop": 0, "ids": [target.0]})).unwrap();
assert_eq!(stop_hexes(&s, target, false), ["#0000ff", "#888888", "#ff0000"], "under the bucket's solid fill");
s.execute("paint.setFill", &json!({"gradient": {}})).unwrap();
s.execute("paint.sampleColor", &json!({"color": "#00ffff", "stop": 1, "ids": [target.0]})).unwrap();
assert_eq!(stop_hexes(&s, target, false), ["#0000ff", "#00ffff", "#ff0000"], "under the bucket's gradient");
// A given object without a gradient has no stop to recolor.
s.execute("paint.setFill", &json!({"color": "#00ff00", "ids": [selected.0]})).unwrap();
let err = s.execute("paint.sampleColor", &json!({"color": "#0000ff", "stop": 0, "ids": [selected.0]})).unwrap_err();
assert!(err.to_string().contains("not a gradient"), "{err}");
// A member of a compound path: the compound path's gradient, which the edit changes.
s.select_tool("selection", ViewInfo::default()).unwrap();
let (a, b) = (rect(&mut s, 10.0, 300.0, 50.0, 50.0), rect(&mut s, 100.0, 300.0, 50.0, 50.0));
s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap();
s.execute("object.compoundPath.make", &json!({})).unwrap();
let compound = s.doc().unwrap().selection.objects[0];
s.execute("paint.setFill", &json!({"gradient": {"stops": three}, "ids": [compound.0]})).unwrap();
s.execute("select.set", &json!({"ids": [selected.0]})).unwrap();
s.execute("paint.sampleColor", &json!({"color": "#ff0000", "stop": 2, "ids": [a.0]})).unwrap();
assert_eq!(stop_hexes(&s, compound, false), ["#ffffff", "#888888", "#ff0000"], "the compound path's three stops");
// A group: the stops of its first painted object, which every painted object of the group takes.
let (c, d) = (rect(&mut s, 10.0, 400.0, 50.0, 50.0), rect(&mut s, 100.0, 400.0, 50.0, 50.0));
s.execute("paint.setFill", &json!({"gradient": {"stops": three}, "ids": [c.0]})).unwrap();
s.execute("paint.setFill", &json!({"color": "#00ff00", "ids": [d.0]})).unwrap();
s.execute("select.set", &json!({"ids": [c.0, d.0]})).unwrap();
let group = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap());
s.execute("select.set", &json!({"ids": [selected.0]})).unwrap();
s.execute("paint.sampleColor", &json!({"color": "#0000ff", "stop": 0, "ids": [group.0]})).unwrap();
assert_eq!(stop_hexes(&s, c, false), ["#0000ff", "#888888", "#000000"], "the group's first object");
assert_eq!(stop_hexes(&s, d, false), ["#0000ff", "#888888", "#000000"], "the group's other object");
}
fn fill_geom(s: &Session, id: NodeId) -> Option<GradientGeom> {
match s.doc().unwrap().doc.node(id).unwrap().appearance.fill_paint() {
Paint::Gradient(g) => g.geom,
+3 -1
View File
@@ -6321,7 +6321,9 @@ Alt+Shift-click samples only the color at the pointer into the active paint defa
sets them; a tree names flags under `appearance` (`transparency`, `fill` `{color, transparency, overprint}`, `stroke`
`{color, transparency, overprint, weight, cap, join, miter, dash}`), `character` and `paragraph`, and a bool for a
branch sets all of it. `paint.sampleColor {color}` puts a sampled color into the active proxy (in a CMYK document an
RGB color is stored as CMYK, as `paint.setFill` stores it). Clicking an image samples its pixels: a CMYK image (a
RGB color is stored as CMYK, as `paint.setFill` stores it). `paint.sampleColor {color, stop}` recolors that stop of
the gradient behind the proxy; with objects in `ids` it recolors that stop in the topmost fill (or stroke) gradient of
the first object `paint.editGradient` edits for them, whatever tool is active. Clicking an image samples its pixels: a CMYK image (a
four-component JPEG, or an 8- or 16-bit chunky CMYK TIFF) gives its ink amounts as they are, and an RGB image's sRGB
pixel is taken into the working RGB space.