diff --git a/crates/engine/src/cmd/colorcmds.rs b/crates/engine/src/cmd/colorcmds.rs index b1817784b6..74c10c7dab 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 background; the modes inside an object, group or layer that isolates its blending do not apply; 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), 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; 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 ), @@ -1012,9 +1012,9 @@ fn contrast(s: &mut Session, p: &Value) -> Result { None => None, }; let below = p.get("against").is_none_or(|v| v.is_null() || v.as_str() == Some("below")); - let (color, drawn, mode, around) = match p.get("color") { + let (color, drawn) = match p.get("color") { None | Some(Value::Null) => proxy_color(s, opacity.is_some(), below).map_err(|e| bad(C, e))?, - Some(v) => (color_value(v).ok_or_else(|| bad(C, "invalid `color`"))?, 1.0, vec![], vec![]), + Some(v) => (color_value(v).ok_or_else(|| bad(C, "invalid `color`"))?, Drawn::default()), }; let rich_black = s.prefs.black_on_screen == "rich"; let cms = vectorcraft_color::cms::active(); @@ -1028,7 +1028,7 @@ fn contrast(s: &mut Session, p: &Value) -> Result { None => { // The sample in working RGB, rounded to 8 bits: the background the answer writes is the // one the ratio uses, and giving it back answers the same ratio. - let working = to_8bit(cms.srgb_to_rgb(color_below(s, rich_black, None).map_err(|e| bad(C, e))?)); + let working = to_8bit(cms.srgb_to_rgb(color_below(s, rich_black, None, &[], None).map_err(|e| bad(C, e))?.0)); (cms.rgb_to_srgb(working.map(|v| f32::from(v) / 255.0)), working) } Some(v) => { @@ -1037,27 +1037,68 @@ fn contrast(s: &mut Session, p: &Value) -> Result { (shown, to_8bit(cms.srgb_to_rgb(shown))) } }; - // Each blend mode, innermost first, replaces the color with its blend with the background - // where it covers it. - let blended = mode.iter().fold(shown(&color), |c, m| vectorcraft_color::blend::blend_rgb(*m, against, c)); - let front = match opacity { - Some(o) => wcag::over(blended, o, against), - None => { - // A translucent group or layer draws the art inside it, the art behind the object - // included, over what is outside it: the color is drawn opaque through each such level - // from the outermost in, over the color read with that level hidden, then at the - // object's own opacity over the background. - let mut x = blended; - for (a, level) in around.iter().rev().filter(|(a, _)| *a < 1.0) { - let outside = match level.filter(|_| below) { - Some(id) => color_below(s, rich_black, Some(id)).map_err(|e| bad(C, e))?, - None => against, - }; - x = wcag::over(x, *a, outside); - } - wcag::over(x, drawn, against) + // Innermost first: the row (inside the object), the object, then its groups and layer. A given + // opacity takes the place of the object's and of its groups' and layer's. + let row = drawn.row.map(|(blend, o)| Step { + blend, + opacity: if opacity.is_some() { 1.0 } else { o }, + isolate: false, + hide: Some(Part::Rows), + within: Part::Rows, + }); + let object = Step { blend: drawn.blend, opacity: opacity.unwrap_or(drawn.opacity), isolate: drawn.isolate, hide: None, within: Part::Object }; + let groups = drawn.around.iter().map(|l| Step { + blend: l.blend, + opacity: if opacity.is_some() { 1.0 } else { l.opacity }, + isolate: l.isolate, + hide: Some(l.part.clone()), + within: l.part.clone(), + }); + let steps = row.into_iter().chain([object]).chain(groups).collect::>(); + // With the background sampled and no opacity given, each level blends with and draws over the + // art below it inside the levels around it, as the canvas composites groups; otherwise the + // background stands for that art. Inside an object, group or layer that isolates its blending, + // the art below is only what that level holds (nothing, for a given background). + let sampled = below && opacity.is_none(); + // A level with a blend mode that does not isolate draws what is inside it over the art below it, + // then takes that art back out by the alpha of what it holds drawn alone, and blends the rest: + // `alone` is that alpha for the nearest such level around, from the levels inside it so far. + let blends = |a: &Step| a.blend != BlendMode::Normal && !a.isolate; + let mut x = (shown(&color), 1.0f32); + let mut alone = 1.0f32; + for (k, step) in steps.iter().enumerate() { + if step.blend == BlendMode::Normal && step.opacity >= 1.0 && x.1 >= 1.0 && alone >= 1.0 { + continue; } - }; + let above = &steps[k + 1..]; + let scope = above.iter().find(|a| a.isolate); + let full = above.iter().filter(|a| a.blend != BlendMode::Normal || a.opacity < 1.0).map(|a| &a.within).collect::>(); + let read = |scope: Option<&Part>| color_below(s, rich_black, step.hide.as_ref(), &full, scope).map_err(|e| bad(C, e)); + let backdrop = match scope { + Some(_) if !sampled => ([0.0; 3], 0.0), + None if !sampled => (against, 1.0), + // The background itself, as the answer writes it. + None if step.hide.is_none() && full.is_empty() => (against, 1.0), + _ => read(scope.map(|a| &a.within))?, + }; + // The alpha of the art below the level inside the nearest blending level around it (none + // for a given background). + let inner = match above.iter().take_while(|a| !a.isolate).find(|a| blends(a)) { + Some(group) if sampled => read(Some(&group.within))?.1, + _ => 0.0, + }; + let source = if !blends(step) { + x + } else if alone <= 0.0 { + (x.0, 0.0) + } else { + (std::array::from_fn(|i| ((x.0[i] * x.1 - (1.0 - alone) * backdrop.0[i] * backdrop.1) / alone).clamp(0.0, 1.0)), alone) + }; + let drawn = alone * step.opacity.clamp(0.0, 1.0); + x = composite(step.blend, source, step.opacity, backdrop); + alone = drawn + inner * (1.0 - drawn); + } + let front = if x.1 >= 1.0 { x.0 } else { wcag::over(x.0, x.1, against) }; let ratio = wcag::contrast_ratio(front, against); let levels = |c: Category| { let (aa, aaa) = c.passes(ratio); @@ -1076,22 +1117,104 @@ fn contrast(s: &mut Session, p: &Value) -> Result { })) } -/// The opacity of each object around the proxy's object that draws it, innermost first, with the -/// object to hide to read what is outside it (`None` for a group of a symbol's art). -type Around = Vec; +/// An object around the proxy's object: one in the document, or a group of the picked object's +/// dynamic symbol instance's art, by its path in the art (the art's root for an empty path). For +/// [`color_below`], also the objects the proxy's color comes from, or their rows from the one the +/// proxy shows up. +#[derive(Clone, PartialEq)] +enum Part { + Doc(NodeId), + Art(Vec), + Object, + Rows, +} -/// One object of [`Around`]: its opacity and the object to hide. -type Level = (f32, Option); +/// An object around the proxy's object that draws it: its opacity, blend mode, whether it isolates +/// the blending inside it, and which object it is. +#[derive(Clone, PartialEq)] +struct Level { + opacity: f32, + blend: BlendMode, + isolate: bool, + part: Part, +} -/// The active proxy's color (its selected gradient stop or freeform point, else the first), the -/// opacity it is drawn with inside its object: the object's opacity times that of its fill or -/// stroke row (none for type's character paint or a colorize color) and of a stop or point, the -/// blend modes it is drawn with, innermost first ([`drawn_modes`]), and the opacities of the groups -/// and layer around the object ([`Around`]). 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 groups or layers differ, since each -/// such level is hidden to read what is outside it. -fn proxy_color(s: &Session, given_opacity: bool, below: bool) -> std::result::Result<(Color, f32, Vec, Around), String> { +/// How the proxy's color is drawn: the object's opacity, its fill or stroke row's blend mode and +/// opacity (none for type's character paint or a colorize color), each times that of a stop or +/// point when it holds the paint, the object's blend mode and whether it isolates its blending, and +/// the groups and layer around the object, innermost first. +#[derive(Clone, PartialEq)] +struct Drawn { + opacity: f32, + row: Option<(BlendMode, f32)>, + blend: BlendMode, + isolate: bool, + around: Vec, +} + +impl Default for Drawn { + fn default() -> Self { + Self { opacity: 1.0, row: None, blend: BlendMode::Normal, isolate: false, around: vec![] } + } +} + +/// One level [`contrast`] draws the color through: the row, the object, or a [`Level`]. `hide` is +/// what [`color_below`] hides to read the art below it (`None`: the selection), and `within` the +/// object whose art is read inside it. +struct Step { + blend: BlendMode, + opacity: f32, + isolate: bool, + hide: Option, + within: Part, +} + +/// `x` (a display sRGB color and its alpha) drawn at `opacity` with blend mode `mode` over `backdrop` +/// (likewise), as the PDF compositing formulas give it: the mode applies where the backdrop is +/// drawn, and over nothing the color stays as it is. +fn composite(mode: BlendMode, x: ([f32; 3], f32), opacity: f32, backdrop: ([f32; 3], f32)) -> ([f32; 3], f32) { + let (b, ab) = backdrop; + let a = x.1 * if opacity.is_finite() { opacity.clamp(0.0, 1.0) } else { 1.0 }; + let mixed = vectorcraft_color::blend::blend_rgb(mode, b, x.0); + let out = a + ab * (1.0 - a); + if out <= 0.0 { + return (b, 0.0); + } + (std::array::from_fn(|i| (a * ((1.0 - ab) * x.0[i] + ab * mixed[i]) + ab * (1.0 - a) * b[i]) / out), out) +} + +/// Whether `a` and `b` draw a color alike: the same blend modes and isolation, and unless +/// `given_opacity` (which takes the place of their opacities) the same opacities. With `below`, the +/// levels that change what they draw must be the same objects too, since the art below each one is +/// read with that object hidden. +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| { + x.blend == y.blend + && x.isolate == y.isolate + && (given_opacity || (x.opacity - y.opacity).abs() < 1e-6) + && (!below || given_opacity || x.part == y.part) + }; + let (la, lb) = (a.around.iter().filter(counts).collect::>(), b.around.iter().filter(counts).collect::>()); + // A row in Normal mode draws as type's character paint does, and an object's isolation only + // changes a row's blending. + let row = |d: &Drawn| d.row.map(|r| r.0).filter(|m| *m != BlendMode::Normal); + let total = |d: &Drawn| d.opacity * d.row.map_or(1.0, |r| r.1); + row(a) == row(b) + && a.blend == b.blend + && (row(a).is_none() || a.isolate == b.isolate) + && (given_opacity || (total(a) - total(b)).abs() < 1e-6) + && (given_opacity || row(a).is_none() || (a.opacity - b.opacity).abs() < 1e-6) + && la.len() == lb.len() + && 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 +/// 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 +/// such level is read with it hidden. +fn proxy_color(s: &Session, given_opacity: bool, below: bool) -> std::result::Result<(Color, Drawn), String> { let stroke = !s.fill_active; let (fill_mixed, stroke_mixed) = s.proxy_mixed(); if if stroke { stroke_mixed } else { fill_mixed } { @@ -1099,7 +1222,7 @@ fn proxy_color(s: &Session, given_opacity: bool, below: bool) -> std::result::Re } let (fill, stroke_paint) = s.proxy_paints(); let source = || s.proxy_source(stroke).map(|(n, item)| drawn_settings(s, &n, item, stroke)).transpose(); - let (layer, mode, around); + 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()) { let item = s.appearance_item(); @@ -1113,36 +1236,32 @@ fn proxy_color(s: &Session, given_opacity: bool, below: bool) -> std::result::Re drawn.push(drawn_settings(s, n, item, stroke)?); } } - (layer, mode, around) = match drawn.first() { + drawing = match drawn.first() { Some(first) => first.clone(), - None => source()?.unwrap_or((1.0, vec![], vec![])), + None => source()?.unwrap_or_default(), }; - // Only the groups and layers that draw at less than full opacity change the answer. - let translucent = |a: &Around| a.iter().filter(|x| x.0 < 1.0).copied().collect::>(); - let levels = translucent(&around); - let same = |(o, m, a): &(f32, Vec, Around)| { - let a = translucent(a); - let level = |x: &Level, y: &Level| (x.0 - y.0).abs() < 1e-6 && (!below || x.1 == y.1); - let opacities = given_opacity || ((o - layer).abs() < 1e-6 && a.len() == levels.len() && a.iter().zip(&levels).all(|(x, y)| level(x, y))); - *m == mode && opacities - }; - if !drawn.iter().all(same) { + if !drawn.iter().all(|d| draws_alike(d, &drawing, given_opacity, below)) { return Err("the selected objects' colors differ: give `color`".into()); } } else { - (layer, mode, around) = source()?.unwrap_or((1.0, vec![], vec![])); + 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), layer, mode, around)), + 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, layer * pt.opacity, mode, around)).ok_or_else(no_color) + f.points.get(i).map(|pt| (pt.color, at(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, layer * st.opacity, mode, around)).ok_or_else(no_color) + g.gradient.stops.get(i).map(|st| (st.color, at(st.opacity))).ok_or_else(no_color) } Paint::None | Paint::Pattern { .. } => Err(no_color()), } @@ -1160,10 +1279,9 @@ fn shown_sources(n: &Node, item: Option, stroke: bool) -> Vec<(&Node, Opt nodes.into_iter().map(|c| (c, None)).collect() } -/// How object `n` draws its fill or stroke (`item`, else the proxy's row): its opacity times that of -/// the row (none for type's character paint or a colorize color), the blend modes, innermost first -/// ([`drawn_modes`]), and the groups and layer around it ([`Around`]). An error for a hidden row. -fn drawn_settings(s: &Session, n: &Node, item: Option, stroke: bool) -> std::result::Result<(f32, Vec, Around), String> { +/// How object `n` draws its fill or stroke (`item`, else the proxy's row), as [`Drawn`] gives it. An +/// error for a hidden row. +fn drawn_settings(s: &Session, n: &Node, item: Option, stroke: bool) -> std::result::Result { let item = n.appearance.item_of_kind(item, !stroke); // Without a targeted row, type shows its characters' paint and a colorized image its // colorize color, which no fill or stroke row's opacity applies to. @@ -1176,14 +1294,10 @@ fn drawn_settings(s: &Session, n: &Node, item: Option, stroke: bool) -> s if own.is_some_and(|o| !o.2) { return Err("the active fill or stroke is hidden: give `color`".to_string()); } - // The groups and layer the object is in draw it at their opacity too (`outer` lists the - // outermost first). - let outer = enclosing(s, n.id); - let opacity = n.opacity * own.map_or(1.0, |o| o.0); - let around: Around = outer.iter().rev().map(|a| (a.0, a.3)).collect(); - // Innermost first: the row (which the object holds), the object, then its groups and layer. - let levels = own.map(|o| (o.1, false)).into_iter().chain([(n.blend, n.isolate)]).chain(outer.iter().rev().map(|a| (a.1, a.2))); - Ok((opacity, drawn_modes(levels.collect()), around)) + // The groups and layer the object is in draw it too (`enclosing` lists the outermost first). + let mut around = enclosing(s, n.id); + around.reverse(); + Ok(Drawn { opacity: n.opacity, row: own.map(|o| (o.1, o.0)), blend: n.blend, isolate: n.isolate, around }) } /// `color` as the canvas draws the picked object of a dynamic symbol instance that a fill stains @@ -1201,28 +1315,18 @@ fn stained(s: &Session, color: Color) -> Color { } } -/// The blend modes that apply to a color drawn through `levels` (each a blend mode and whether that -/// level isolates the blending inside it, innermost first), innermost first. Inside a level that -/// does not isolate, blending reaches the art below, so each mode blends the result of the ones -/// inside it with the background; inside one that does, the art is blended with nothing, so the -/// modes inside the outermost isolating level do not apply. -fn drawn_modes(levels: Vec<(BlendMode, bool)>) -> Vec { - let first = levels.iter().rposition(|l| l.1).unwrap_or(0); - levels.into_iter().skip(first).map(|l| l.0).filter(|m| *m != BlendMode::Normal).collect() -} - -/// The opacity, blend mode and isolation of each object that draws the proxy's object `id`, with -/// its id in the document (`None` for a group of a symbol's art), outermost first: its groups and +/// The objects that draw the proxy's object `id` ([`Level`]), outermost first: its groups and /// layer, and for a picked object of a dynamic symbol instance, the instance, its groups and layer, /// and the groups of the symbol's art around the object. -fn enclosing(s: &Session, id: NodeId) -> Vec<(f32, BlendMode, bool, Option)> { +fn enclosing(s: &Session, id: NodeId) -> Vec { let Some(st) = s.active() else { return vec![] }; let doc = &st.doc; + let level = |n: &Node, part: Part| Level { opacity: n.opacity, blend: n.blend, isolate: n.isolate, part }; // `ancestry` lists the object itself last. let in_doc = |id: NodeId, own: bool| { let ids = doc.ancestry(id).unwrap_or_default(); let nodes = ids.into_iter().filter(|a| own || *a != id).filter_map(|a| doc.node(a)); - nodes.map(|a| (a.opacity, a.blend, a.isolate, Some(a.id))).collect::>() + nodes.map(|a| level(a, Part::Doc(a.id))).collect::>() }; let Some(sp) = st.selection.symbol_part.as_ref() else { return in_doc(id, false) }; let art = doc.node(sp.instance).and_then(|inst| { @@ -1236,11 +1340,11 @@ fn enclosing(s: &Session, id: NodeId) -> Vec<(f32, BlendMode, bool, Option Vec<(f32, BlendMode, bool, Option