From 1af9fe50d37d1f3e9472e53fb7d3255fc4b1683a Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 25 Nov 2026 22:14:07 -0800 Subject: [PATCH] Color Wheel: objects that differ in isolation over a blending lower row have no one answer A translucent or blending row drawn over a lower row that blends reads the art behind the object only when the object does not isolate its blending. Selected objects that differ only in isolation then draw the color over different rows, so color.contrast reports that their colors differ instead of measuring both with the first object's isolation. A blending row at no opacity, or a fill row covered by an opaque solid Normal fill row, does not count. --- crates/engine/src/cmd/colorcmds.rs | 19 ++++++++- crates/engine/src/tests/colorcontrast.rs | 49 ++++++++++++++++++++++++ docs/mcp.md | 3 +- 3 files changed, 69 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/colorcmds.rs b/crates/engine/src/cmd/colorcmds.rs index 4475897d9a..5bcbb930c9 100644 --- a/crates/engine/src/cmd/colorcmds.rs +++ b/crates/engine/src/cmd/colorcmds.rs @@ -1418,7 +1418,8 @@ fn composite(mode: BlendMode, x: ([f32; 3], f32), opacity: f32, backdrop: ([f32; /// levels that change what they draw must be the same objects too, since the art below each one is /// read with that object hidden, and the object's opacity must be the same apart from its row's, /// since the row draws over the object's rows below it and the object over the art behind it, and a -/// row that blends or is translucent must lie over the same shown rows. +/// row that blends or is translucent must lie over the same shown rows, and over a lower row that +/// blends, with the same isolation. fn draws_alike(a: &Drawn, b: &Drawn, given_opacity: bool, below: bool) -> bool { let counts = |l: &&Level| l.blend != BlendMode::Normal || l.isolate || (!given_opacity && l.opacity < 1.0); let level = |x: &Level, y: &Level| { @@ -1434,10 +1435,26 @@ fn draws_alike(a: &Drawn, b: &Drawn, given_opacity: bool, below: bool) -> bool { let total = |d: &Drawn| d.opacity * d.row.map_or(1.0, |r| r.1); // A row that blends or lets art through draws over the rows below it, read at one point. let over_rows = |d: &Drawn| d.row.is_some_and(|(m, o)| m != BlendMode::Normal || o < 1.0); + // A lower row that blends reads the art behind the object unless the object isolates its + // blending, so a row drawn over it reads a backdrop that depends on the isolation. A blending + // row at no opacity does not show, nor does a fill row without effects under an opaque solid + // Normal fill row without effects, which covers the same shape (a stroke reaches outside it). + let plain_fill = |i: &vectorcraft_doc::AppearanceItem| i.is_fill() && i.effects().is_empty(); + let covers = |i: &vectorcraft_doc::AppearanceItem| { + plain_fill(i) && i.blend() == BlendMode::Normal && i.opacity() >= 1.0 && matches!(i.paint(), Paint::Solid { .. }) + }; + let over_blended = |d: &Drawn| { + over_rows(d) + && d.lower + .iter() + .enumerate() + .any(|(k, i)| i.blend() != BlendMode::Normal && i.opacity() > 0.0 && !(plain_fill(i) && d.lower[k + 1..].iter().any(covers))) + }; row(a) == row(b) && (given_opacity || !below || !(over_rows(a) || over_rows(b)) || a.lower == b.lower) && a.blend == b.blend && (row(a).is_none() || a.isolate == b.isolate) + && (given_opacity || !below || !(over_blended(a) || over_blended(b)) || a.isolate == b.isolate) && (given_opacity || (total(a) - total(b)).abs() < 1e-6) && (given_opacity || (row(a).is_none() && !below) || (a.opacity - b.opacity).abs() < 1e-6) && la.len() == lb.len() diff --git a/crates/engine/src/tests/colorcontrast.rs b/crates/engine/src/tests/colorcontrast.rs index 7c2a5c838f..609aa9189e 100644 --- a/crates/engine/src/tests/colorcontrast.rs +++ b/crates/engine/src/tests/colorcontrast.rs @@ -1036,6 +1036,55 @@ fn isolation_leaves_a_translucent_normal_row_as_it_draws() { } } +/// Two squares over locked orange, each with a 50 % white top fill over a blue Multiply fill, one +/// isolating its blending: the blue multiplies with the orange in one and draws over nothing in the +/// other, so the white draws over different rows and there is no one answer. A given background +/// stands for the rows below and answers. +#[test] +fn a_translucent_row_over_a_blending_row_in_objects_of_different_isolation_has_no_one_answer() { + let mut s = session(); + let id = |v: Value| v["id"].as_u64().unwrap(); + s.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 180, "height": 180})).unwrap(); + s.execute("paint.setFill", &json!({"color": "#ff8000"})).unwrap(); + s.execute("object.lock", &json!({})).unwrap(); + let square = |s: &mut Session, x: i32, isolate: bool| { + let n = id(s.execute("shape.rectangle", &json!({"x": x, "y": 50, "width": 50, "height": 100})).unwrap()); + s.execute("paint.setFill", &json!({"color": "#0000ff"})).unwrap(); + s.execute("appearance.addFill", &json!({})).unwrap(); + let st = s.active().unwrap(); + let items = &st.doc.node(vectorcraft_doc::NodeId(n)).unwrap().appearance.items; + let top = items.iter().rposition(|i| i.is_fill()).unwrap(); + let lower = items.iter().position(|i| i.is_fill()).unwrap(); + s.execute("appearance.setItem", &json!({"index": top, "color": "#ffffff", "opacity": 50})).unwrap(); + s.execute("appearance.setItem", &json!({"index": lower, "blend": "multiply"})).unwrap(); + s.execute("appearance.setActiveItem", &json!({"index": null})).unwrap(); + if isolate { + s.execute("transparency.set", &json!({"item": null, "isolate": true})).unwrap(); + } + n + }; + let a = square(&mut s, 30, false); + let b = square(&mut s, 120, true); + s.execute("select.set", &json!({"ids": [a]})).unwrap(); + let ra = ratio(&s.execute("color.contrast", &json!({})).unwrap()); + s.execute("select.set", &json!({"ids": [b]})).unwrap(); + let rb = ratio(&s.execute("color.contrast", &json!({})).unwrap()); + assert!((ra - rb).abs() > 0.1, "the white draws over different rows: {ra} {rb}"); + s.execute("select.set", &json!({"ids": [a, b]})).unwrap(); + let e = s.execute("color.contrast", &json!({})).unwrap_err().to_string(); + assert!(e.contains("the selected objects' colors differ"), "{e}"); + s.execute("color.contrast", &json!({"against": "#000000"})).unwrap(); + // A blending row at no opacity does not show, so the isolation no longer changes the answer. + for n in [a, b] { + let st = s.active().unwrap(); + let lower = st.doc.node(vectorcraft_doc::NodeId(n)).unwrap().appearance.items.iter().position(|i| i.is_fill()).unwrap(); + s.execute("appearance.setItem", &json!({"ids": [n], "index": lower, "opacity": 0})).unwrap(); + } + s.execute("appearance.setActiveItem", &json!({"index": null})).unwrap(); + s.execute("select.set", &json!({"ids": [a, b]})).unwrap(); + s.execute("color.contrast", &json!({})).unwrap(); +} + /// A Difference square centered on the edge between locked black and white art: the art behind it /// is not one color where it is read, so no one color stands for it; a given background answers, /// and over one color the background is read as before. diff --git a/docs/mcp.md b/docs/mcp.md index 2787bc8791..0f2326bf51 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -1236,7 +1236,8 @@ is drawn); a picked object of a dynamic symbol instance hides only itself, and t These are errors: `"below"` with nothing selected; a "?" proxy (hidden objects count there, as in the Color panel); selected objects that draw one color with different blend modes, or at different opacities without `opacity` (with `"below"`, also in different groups or layers that are translucent, have a blend mode or isolate their blending, or -with a translucent or blending row over different shown rows below it), where hidden objects do not count, and the +with a translucent or blending row over different shown rows below it, or over a shown row that blends in objects +that differ in isolating their blending), where hidden objects do not count, and the error names `opacity` when giving it settles the difference; a proxy of None, a pattern or a hidden row without `color`; and without `color`, a proxy's object drawn through an opacity mask that is not disabled, its own or that of a group, layer or instance around it, since the mask sets how much of the color shows at each point; and with `"below"`, a color drawn with a