From 3be51d9073c05e0d5676df54a8c19779ff478d89 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Thu, 20 Aug 2026 00:03:10 +0200 Subject: [PATCH] Adds recoloring --- images/icons/bolt-lightning-solid-full.svg | 2 +- images/icons/level.svg | 4 +- images/icons/precision.svg | 4 +- images/icons/reload.svg | 6 +- images/icons/rmax.svg | 6 +- images/icons/tier.svg | 6 +- images/icons/time.svg | 4 +- setup/symbols.typ | 144 ++++++++++++++++++++- symbol_test.typ | 24 ++++ 9 files changed, 183 insertions(+), 17 deletions(-) diff --git a/images/icons/bolt-lightning-solid-full.svg b/images/icons/bolt-lightning-solid-full.svg index 3422877..9c4a21c 100644 --- a/images/icons/bolt-lightning-solid-full.svg +++ b/images/icons/bolt-lightning-solid-full.svg @@ -1 +1 @@ - \ No newline at end of file + diff --git a/images/icons/level.svg b/images/icons/level.svg index 4f949c9..defbf12 100644 --- a/images/icons/level.svg +++ b/images/icons/level.svg @@ -1,6 +1,6 @@ - - \ No newline at end of file + diff --git a/images/icons/precision.svg b/images/icons/precision.svg index 2614526..c9543e0 100644 --- a/images/icons/precision.svg +++ b/images/icons/precision.svg @@ -1,5 +1,5 @@ - + - \ No newline at end of file + diff --git a/images/icons/reload.svg b/images/icons/reload.svg index a10f0ac..11838c8 100644 --- a/images/icons/reload.svg +++ b/images/icons/reload.svg @@ -1,6 +1,6 @@ - - - \ No newline at end of file + diff --git a/images/icons/rmax.svg b/images/icons/rmax.svg index 36ca8cb..9359b11 100644 --- a/images/icons/rmax.svg +++ b/images/icons/rmax.svg @@ -1,6 +1,6 @@ - - - \ No newline at end of file + diff --git a/images/icons/tier.svg b/images/icons/tier.svg index 4f67d6b..e08f2a3 100644 --- a/images/icons/tier.svg +++ b/images/icons/tier.svg @@ -1,6 +1,6 @@ - - \ No newline at end of file + " fill="none" stroke="black" stroke-width="7" stroke-linecap="round"> + diff --git a/images/icons/time.svg b/images/icons/time.svg index ca43991..fc1ac78 100644 --- a/images/icons/time.svg +++ b/images/icons/time.svg @@ -1,6 +1,6 @@ - - \ No newline at end of file + diff --git a/setup/symbols.typ b/setup/symbols.typ index 6d95db3..b832275 100644 --- a/setup/symbols.typ +++ b/setup/symbols.typ @@ -120,11 +120,153 @@ inset: 0pt, image("/images/icons/skull-fill.svg"), ) -#let wine-box(..args) = box( +#let wine-box = box( height: 0.65em, baseline: 0.5pt, inset: 0pt, image("/images/icons/wine-fill.svg"), ) +// ============================================================================= +// SVG RECOLORING +// ============================================================================= +#let re-colorize(svg-box, color) = { + let outer = svg-box + let current = svg-box + + let source = none + let image-node = none + + // --------------------------------------------------------------------------- + // Unwrap boxes until we find an image source. + // --------------------------------------------------------------------------- + + for _ in range(2) { + let direct-source = current.at("source", default: none) + + if direct-source != none { + source = direct-source + image-node = current + break + } + + let body = current.at("body", default: none) + + if body == none { + break + } + + current = body + } + + if source == none { + panic( + "re-colorize: expected an image or boxed image, " + + "but no image source was found.", + ) + } + + let source-path = str(source) + + // Case-insensitive `.svg` extension check. + if source-path.find(regex("(?i)\\.svg$")) == none { + panic( + "re-colorize: expected an SVG image, got `" + source-path + "`.", + ) + } + + let svg = read(source-path) + + // Also verify that the contents actually look like SVG. + if svg.find(regex("(?i))")) == none { + panic( + "re-colorize: `" + + source-path + + "` does not appear to contain SVG markup.", + ) + } + + // --------------------------------------------------------------------------- + // Recolor black. + // --------------------------------------------------------------------------- + + let replacement = color.to-hex() + let black-pattern = regex( + "(?i)(#000000\\b|#000\\b|\\bblack\\b|rgb\\(\\s*0\\s*[, ]\\s*0\\s*[, ]\\s*0\\s*\\))", + ) + + let recolored = svg.replace(black-pattern, replacement) + + if recolored == svg { + panic( + "re-colorize: no black color could be replaced in `" + source-path + "`.", + ) + } + + // --------------------------------------------------------------------------- + // Preserve sizing. + // --------------------------------------------------------------------------- + + let image-width = image-node.at("width", default: auto) + let image-height = image-node.at("height", default: auto) + let image-fit = image-node.at("fit", default: "cover") + + // If the image is wrapped in a box with explicit dimensions, + // prefer those dimensions. + let outer-width = outer.at("width", default: auto) + let outer-height = outer.at("height", default: auto) + + let width = if outer-width != auto { + outer-width + } else { + image-width + } + + let height = if outer-height != auto { + outer-height + } else { + image-height + } + + // show image: it => image( + // bytes(recolored), + // width: width, + // height: height, + // fit: image-fit, + // ) + svg-box +} + +// Safe version: +// suitable for show rules and generic content. +#let try-re-colorize(body, color) = { + let current = body + let source = none + for _ in range(8) { + let direct-source = current.at("source", default: none) + if direct-source != none { + source = direct-source + break + } + let inner = current.at("body", default: none) + if inner == none { + break + } + current = inner + } + + // Not an image-like thing. + if source == none { + return body + } + + let source-path = str(source) + + // Image, but not SVG. + if source-path.find(regex("(?i)\\.svg$")) == none { + return body + } + + re-colorize(body, color) +} diff --git a/symbol_test.typ b/symbol_test.typ index d31040e..cea6647 100644 --- a/symbol_test.typ +++ b/symbol_test.typ @@ -1,2 +1,26 @@ #import "/setup/setup.typ": macros, symbols + +#show box: it => symbols.try-re-colorize(it, blue) + This is a test of symbols, in various environments: @blood:short, @jauge:short, @ar_end, @ar_tier, @ar_lvl, @ar_time, @ar_tox, @ar_wine, @ar_cst, @rl_integrite, @rl_protection, @rl_reload, @rl_rmax, @rl_am, @rl_arbreak, @rl_parmure, @rl_precision, @ca_torque + + +#symbols.cost-box +#symbols.try-re-colorize(symbols.blood-box, green) + + += Old +// #let c1 = read("c1.svg"); +// #let c2 = read("c2.svg"); +// #let c1 = c1.replace("#000000", green.hex()) +// #let c2 = c2.replace("#000000", green.hex()) +// #image.decode(c1, width: 25pt) +// #image.decode(c2, width: 25pt) + +// = New +// #let c1 = read("c1.svg"); +// #let c2 = read("c2.svg"); +// #let c1 = colorize(c1, green); +// #let c2 = colorize(c2, green); +// #image.decode(c1, width: 25pt) +// #image.decode(c2, width: 25pt)