diff --git a/crates/brush/src/colorize.rs b/crates/brush/src/colorize.rs index 00edeb3113..4c16cb169b 100644 --- a/crates/brush/src/colorize.rs +++ b/crates/brush/src/colorize.rs @@ -12,13 +12,17 @@ fn luminance(c: &Color) -> f32 { 0.2126 * r + 0.7152 * g + 0.0722 * b } -fn map_color(c: &Color, mode: Colorization, key: &Color) -> Color { - // Black ink alone is its own tint: its amount is the tint, not how light it displays (100% K - // displays lighter than black). - let light = match *c { +/// How light `c` is for colorization, 0 black to 1 white. Black ink alone is its own tint: its +/// amount is the tint, not how light it displays (100% K displays lighter than black). +fn lightness(c: &Color) -> f32 { + match *c { Color::Cmyk { c, m, y, k } if c == 0.0 && m == 0.0 && y == 0.0 => 1.0 - k, _ => luminance(c), - }; + } +} + +fn map_color(c: &Color, mode: Colorization, key: &Color) -> Color { + let light = lightness(c); // A process or gray stroke color's tints and shades stay in its model. let (white, black) = match key { Color::Cmyk { .. } => (Color::cmyk(0.0, 0.0, 0.0, 0.0), Color::cmyk(0.0, 0.0, 0.0, 1.0)), @@ -47,45 +51,61 @@ fn map_color(c: &Color, mode: Colorization, key: &Color) -> Color { } } -fn map_paint(p: &mut Paint, mode: Colorization, key: &Color) { +/// The swatch a stroke paint links to and its tint, for colorization: Tints of a linked color are +/// tints of its swatch (a spot color's tints print on its own plate). +type Link<'a> = Option<(&'a str, f32)>; + +fn map_paint(p: &mut Paint, mode: Colorization, key: &Color, link: Link<'_>) { if let Paint::Solid { color, .. } = p { - *p = Paint::solid(map_color(color, mode, key)); + *p = match (mode, link) { + (Colorization::Tints, Some((swatch, tint))) => { + let t = (1.0 - lightness(color)).clamp(0.0, 1.0); + Paint::Solid { color: key.tinted(t), swatch: Some(swatch.to_string()), tint: tint * t } + } + _ => Paint::solid(map_color(color, mode, key)), + }; } } /// Recolour `n` (recursively) for a stroke painted `stroke`, and strip brushes from its strokes so -/// brush output never recurses. Non-solid stroke paints leave the art's colours alone. Type is +/// brush output never recurses. Non-solid stroke paints leave the art's colours alone. Under Tints, +/// a stroke color linked to a swatch gives tints of that swatch, linked to it, as a spot color's +/// tints are (the art's darkest parts the stroke's own tint, white none of it). Type is /// recolored like paths: its characters' solid fills and strokes as well as its own. pub fn colorize(n: &mut Node, mode: Colorization, stroke: &Paint) { let key = match (mode, stroke.color()) { (Colorization::None, _) | (_, None) => None, (_, Some(c)) => Some(c), }; - fn walk(n: &mut Node, mode: Colorization, key: Option) { + let link = match stroke { + Paint::Solid { swatch: Some(s), tint, .. } => Some((s.as_str(), *tint)), + _ => None, + }; + fn walk(n: &mut Node, mode: Colorization, key: Option, link: Link<'_>) { for it in &mut n.appearance.items { match it { AppearanceItem::Fill(f) => { if let Some(k) = &key { - map_paint(&mut f.paint, mode, k); + map_paint(&mut f.paint, mode, k, link); } } AppearanceItem::Stroke(s) => { s.brush = None; if let Some(k) = &key { - map_paint(&mut s.paint, mode, k); + map_paint(&mut s.paint, mode, k, link); } } } } if let (Some(k), NodeKind::Text(t)) = (&key, &mut n.kind) { for r in &mut t.runs { - map_paint(&mut r.style.fill, mode, k); - map_paint(&mut r.style.stroke, mode, k); + map_paint(&mut r.style.fill, mode, k, link); + map_paint(&mut r.style.stroke, mode, k, link); } } if let Some(ch) = n.children_mut() { for c in ch.iter_mut() { - walk(Arc::make_mut(c), mode, key); + walk(Arc::make_mut(c), mode, key, link); } } // A mask's art is drawn too: a brush on its strokes would run this brush again. Its colours @@ -94,7 +114,7 @@ pub fn colorize(n: &mut Node, mode: Colorization, stroke: &Paint) { strip_brushes(Arc::make_mut(&mut m.art)); } } - walk(n, mode, key); + walk(n, mode, key, link); } /// Strip the brushes from every stroke under `n`, its masks' art included. diff --git a/crates/brush/src/tests.rs b/crates/brush/src/tests.rs index de178c76ac..cf4ad82db1 100644 --- a/crates/brush/src/tests.rs +++ b/crates/brush/src/tests.rs @@ -840,6 +840,47 @@ fn tints_colorization_uses_stroke_colour() { assert_eq!(white.appearance.fill_paint().color().unwrap().to_rgb(), [1.0, 1.0, 1.0]); } +/// Under Tints, a stroke color linked to a swatch gives tints of that swatch, linked to it: black +/// art the stroke's own tint of it, 60% black ink 60% of that, white none; the colors shown are the +/// same as for the unlinked color, a process or an RGB swatch. Tints and Shades stay unlinked. +#[test] +fn tints_of_a_swatch_stay_linked_to_it() { + let spot = Color::cmyk(0.0, 0.5, 1.0, 0.0); + let stroke = Paint::Solid { color: spot.tinted(0.8), swatch: Some("Brand orange".into()), tint: 0.8 }; + let mapped = |art: Color| { + let mut n = rect_art(1.0, 1.0); + n.appearance.set_fill(Paint::solid(art)); + colorize(&mut n, Colorization::Tints, &stroke); + let mut plain = rect_art(1.0, 1.0); + plain.appearance.set_fill(Paint::solid(art)); + colorize(&mut plain, Colorization::Tints, &Paint::solid(spot.tinted(0.8))); + (n.appearance.fill_paint(), plain.appearance.fill_paint().color().unwrap()) + }; + for (art, want) in [(Color::cmyk(0.0, 0.0, 0.0, 1.0), 0.8), (Color::cmyk(0.0, 0.0, 0.0, 0.6), 0.48), (Color::cmyk(0.0, 0.0, 0.0, 0.0), 0.0)] { + let (paint, shown) = mapped(art); + let Paint::Solid { color, swatch: Some(s), tint } = paint else { panic!("linked: {paint:?}") }; + assert_eq!(s, "Brand orange"); + assert!((tint - want).abs() < 1e-5, "{art:?}: {tint}"); + let close = color.to_cmyk().iter().zip(shown.to_cmyk()).all(|(a, b)| (a - b).abs() < 1e-5); + assert!(close, "{color:?} {shown:?}"); + } + // An RGB swatch: its tints mix toward white, as the unlinked color's do. + let rgb = Color::rgb(1.0, 0.4, 0.0); + let mut n = rect_art(1.0, 1.0); + n.appearance.set_fill(Paint::solid(Color::cmyk(0.0, 0.0, 0.0, 0.5))); + colorize(&mut n, Colorization::Tints, &Paint::Solid { color: rgb, swatch: Some("Brand red".into()), tint: 1.0 }); + let Paint::Solid { color, swatch: Some(s), tint } = n.appearance.fill_paint() else { panic!("linked") }; + assert_eq!(s, "Brand red"); + assert!((tint - 0.5).abs() < 1e-5, "{tint}"); + let [r, g, b] = color.to_rgb(); + assert!((r - 1.0).abs() < 1e-5 && (g - 0.7).abs() < 1e-5 && (b - 0.5).abs() < 1e-5, "{r} {g} {b}"); + // Tints and Shades darken past the swatch, which a tint can't: its pieces stay unlinked. + let mut n = rect_art(1.0, 1.0); + n.appearance.set_fill(Paint::solid(Color::cmyk(0.0, 0.0, 0.0, 1.0))); + colorize(&mut n, Colorization::TintsAndShades, &stroke); + assert!(matches!(n.appearance.fill_paint(), Paint::Solid { swatch: None, .. }), "{:?}", n.appearance.fill_paint()); +} + /// Black ink alone is a tint by its amount: a process black stroke leaves it as it is, and another /// process stroke color takes that share of its inks. Tints of a process color stay process. #[test] diff --git a/crates/eps/src/import/ai/style.rs b/crates/eps/src/import/ai/style.rs index e0068a52be..2a411c63a1 100644 --- a/crates/eps/src/import/ai/style.rs +++ b/crates/eps/src/import/ai/style.rs @@ -1591,9 +1591,9 @@ const ART_SETTINGS: [(&str, Kind, Option); 14] = [ ]; /// An Art brush's filter `f` as the stroke it paints, with its settings: `None` when it isn't a -/// stroke in a color, names no Art brush, holds an entry that isn't read or a setting that isn't a -/// number of its kind, or tints its art with a swatch's color: its drawn look stays. Whether the -/// brush draws as these settings is [`super::brush::draws_as`]'s to say. +/// stroke in a color, names no Art brush, or holds an entry that isn't read or a setting that isn't +/// a number of its kind: its drawn look stays. Whether the brush draws as these settings is +/// [`super::brush::draws_as`]'s to say. fn art_stroke(f: &Filter) -> Option { if !f.paints.0 || f.gradient @@ -1612,11 +1612,9 @@ fn art_stroke(f: &Filter) -> Option { for (slot, (key, kind, missing)) in settings.iter_mut().zip(ART_SETTINGS) { *slot = entry(f, key, kind).ok()?.or(missing)?; } + // Under Tints, a stroke color linked to a swatch tints the art in that swatch (a spot color's + // tints stay on its plate: `vectorcraft_brush::colorize`). let paint = f.stroke.clone()?; - // The tinted pieces are process colors, so a swatch's ink would be lost. - if settings[0] == 1.0 && matches!(paint, Paint::Solid { swatch: Some(_), .. }) { - return None; - } Some(StyleItem::Stroke { paint, width: f.width, diff --git a/crates/eps/src/import/ai/tests.rs b/crates/eps/src/import/ai/tests.rs index 68ba060352..ddc74d9c1f 100644 --- a/crates/eps/src/import/ai/tests.rs +++ b/crates/eps/src/import/ai/tests.rs @@ -10994,6 +10994,39 @@ fn an_art_brush_with_options_comes_in_brushed_with_them() { assert!(filled(&along, 50.0, -2.0) && filled(&along, 90.0, 2.0) && !filled(&along, 10.0, 2.0), "along: the narrow block at the end"); } +/// A path whose Art brush tints its art with a stroke color linked to a swatch (Tints, a spot +/// color) comes in brushed: the brush draws the art in tints of that swatch, linked to it, so the +/// spot ink stays on its plate. +#[test] +fn an_art_brush_tinting_with_a_spot_color_comes_in_brushed_in_tints_of_it() { + let def = format!("{}{}", lopsided_art("Ribbon art"), plugin_object(ART_BRUSH, "Ribbon", RIBBON)); + let style = art_style("Ribbon", 1.0, &STRETCHED, "").replace("0 1 1 0 1 0 0 XA", "0 0 0 0 1 0.5 0 (Brand orange) 0 1 XK"); + let r = read_ok(&format!("{def}{style}{}", layer(&brushed_path()))); + let a = art(&r.doc); + assert!(matches!(a[0].kind, NodeKind::Path { .. }), "the path, not its look: {a:#?}"); + let st = a[0].appearance.stroke().cloned().unwrap(); + assert_eq!(st.brush.as_deref(), Some("Ribbon")); + let Paint::Solid { swatch: Some(swatch), .. } = &st.paint else { panic!("a swatch: {:?}", st.paint) }; + assert_eq!(swatch, "Brand orange"); + let lib = vectorcraft_brush::library(&r.doc); + let ribbon = lib.iter().find(|b| b.name == "Ribbon").unwrap(); + let line = kurbo::BezPath::from_vec(vec![kurbo::PathEl::MoveTo(Point::new(0.0, 0.0)), kurbo::PathEl::LineTo(Point::new(100.0, 0.0))]); + let mut fills = vec![]; + for n in vectorcraft_brush::stroke_pieces(&r.doc, ribbon, &line, &st) { + n.walk(&mut |c| { + if let Some(f) = c.appearance.fill() { + fills.push(f.paint.clone()); + } + }); + } + // The art is black: each piece is the swatch at the stroke's own tint. + assert!(!fills.is_empty()); + for p in &fills { + assert!(matches!(p, Paint::Solid { swatch: Some(s), tint, .. } if s == "Brand orange" && (*tint - 1.0).abs() < 1e-6), "{p:?}"); + } + assert!(!r.warnings.iter().any(|w| w.contains("drawn look")), "{:?}", r.warnings); +} + /// An Art brushed path keeps its drawn look when the brush wouldn't draw it the way the file does: /// its settings differ from the brush's, the brush's are ones VectorCraft doesn't draw as the file /// does, its art isn't read, or the brush isn't one the panel keeps. @@ -11021,7 +11054,6 @@ fn an_art_brushed_path_the_brush_cant_draw_keeps_its_drawn_look() { let cases = [ ("a switch written as another kind", good.clone(), art_style("Ribbon", 1.0, &malformed, "")), ("art in a pattern", format!("{dots}{patterned}{}", plugin_object(ART_BRUSH, "Ribbon", RIBBON)), plain_style.clone()), - ("tints of a swatch's ink", good.clone(), plain_style.replace("0 1 1 0 1 0 0 XA", "0 0 0 0 1 0.5 0 (Brand orange) 0 1 XK")), ( "art with no length", format!(