pub(crate) fn to_light_mode(svg: &str) -> String {
let bytes = svg.as_bytes();
let mut out = String::with_capacity(svg.len());
let mut i = 0;
while i < bytes.len() {
if bytes[i] == b'#' {
if let Some((rgb, len)) = parse_hex(&bytes[i..]) {
out.push_str(&to_hex(flip_lightness(rgb)));
i += len;
continue;
}
} else if is_rgb_function_start(bytes, i) {
if let Some(parsed) = parse_rgb(&svg[i..]) {
let (r, g, b) = flip_lightness(parsed.rgb);
match parsed.alpha {
Some(a) => out.push_str(&format!("rgba({r}, {g}, {b}, {a})")),
None => out.push_str(&format!("rgb({r}, {g}, {b})")),
}
i += parsed.len;
continue;
}
}
out.push(bytes[i] as char);
i += 1;
}
out
}
fn starts_with_ci(haystack: &[u8], prefix: &[u8]) -> bool {
haystack.len() >= prefix.len() && haystack[..prefix.len()].eq_ignore_ascii_case(prefix)
}
fn is_rgb_function_start(bytes: &[u8], i: usize) -> bool {
let starts_token =
starts_with_ci(&bytes[i..], b"rgb(") || starts_with_ci(&bytes[i..], b"rgba(");
if !starts_token {
return false;
}
i == 0 || !matches!(bytes[i - 1], b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'_' | b'-')
}
fn parse_hex(bytes: &[u8]) -> Option<((u8, u8, u8), usize)> {
debug_assert_eq!(bytes[0], b'#');
let hex: Vec<u8> = bytes[1..]
.iter()
.take_while(|b| b.is_ascii_hexdigit())
.copied()
.collect();
match hex.len() {
3 => {
let r = dup_nibble(hex[0]);
let g = dup_nibble(hex[1]);
let b = dup_nibble(hex[2]);
Some(((r, g, b), 4))
}
6 => {
let r = byte_from_hex(hex[0], hex[1]);
let g = byte_from_hex(hex[2], hex[3]);
let b = byte_from_hex(hex[4], hex[5]);
Some(((r, g, b), 7))
}
_ => None,
}
}
struct ParsedRgb {
rgb: (u8, u8, u8),
len: usize,
alpha: Option<String>,
}
fn parse_rgb(s: &str) -> Option<ParsedRgb> {
let open = s.find('(')?;
let close = s.find(')')?;
if close < open {
return None;
}
let inner = &s[open + 1..close];
let parts: Vec<&str> = inner.split(',').map(str::trim).collect();
if parts.len() != 3 && parts.len() != 4 {
return None;
}
let r = parts[0].parse::<u8>().ok()?;
let g = parts[1].parse::<u8>().ok()?;
let b = parts[2].parse::<u8>().ok()?;
let alpha = if parts.len() == 4 {
Some(parts[3].to_string())
} else {
None
};
Some(ParsedRgb {
rgb: (r, g, b),
len: close + 1,
alpha,
})
}
fn dup_nibble(c: u8) -> u8 {
let v = hex_val(c);
v << 4 | v
}
fn byte_from_hex(hi: u8, lo: u8) -> u8 {
hex_val(hi) << 4 | hex_val(lo)
}
fn hex_val(c: u8) -> u8 {
match c {
b'0'..=b'9' => c - b'0',
b'a'..=b'f' => c - b'a' + 10,
b'A'..=b'F' => c - b'A' + 10,
_ => 0,
}
}
fn to_hex((r, g, b): (u8, u8, u8)) -> String {
format!("#{r:02x}{g:02x}{b:02x}")
}
fn flip_lightness((r, g, b): (u8, u8, u8)) -> (u8, u8, u8) {
let (h, s, l) = rgb_to_hsl(r, g, b);
hsl_to_rgb(h, s, 1.0 - l)
}
fn rgb_to_hsl(r: u8, g: u8, b: u8) -> (f64, f64, f64) {
let r = r as f64 / 255.0;
let g = g as f64 / 255.0;
let b = b as f64 / 255.0;
let max = r.max(g).max(b);
let min = r.min(g).min(b);
let l = (max + min) / 2.0;
let delta = max - min;
if delta.abs() < f64::EPSILON {
return (0.0, 0.0, l); }
let s = if l > 0.5 {
delta / (2.0 - max - min)
} else {
delta / (max + min)
};
let h = if (max - r).abs() < f64::EPSILON {
((g - b) / delta) % 6.0
} else if (max - g).abs() < f64::EPSILON {
(b - r) / delta + 2.0
} else {
(r - g) / delta + 4.0
};
let mut h = h / 6.0;
if h < 0.0 {
h += 1.0;
}
(h, s, l)
}
fn hsl_to_rgb(h: f64, s: f64, l: f64) -> (u8, u8, u8) {
if s.abs() < f64::EPSILON {
let v = (l * 255.0).round() as u8;
return (v, v, v);
}
let q = if l < 0.5 {
l * (1.0 + s)
} else {
l + s - l * s
};
let p = 2.0 * l - q;
let r = hue_to_channel(p, q, h + 1.0 / 3.0);
let g = hue_to_channel(p, q, h);
let b = hue_to_channel(p, q, h - 1.0 / 3.0);
(
(r * 255.0).round() as u8,
(g * 255.0).round() as u8,
(b * 255.0).round() as u8,
)
}
fn hue_to_channel(p: f64, q: f64, mut t: f64) -> f64 {
if t < 0.0 {
t += 1.0;
}
if t > 1.0 {
t -= 1.0;
}
if t < 1.0 / 6.0 {
p + (q - p) * 6.0 * t
} else if t < 1.0 / 2.0 {
q
} else if t < 2.0 / 3.0 {
p + (q - p) * (2.0 / 3.0 - t) * 6.0
} else {
p
}
}
#[cfg(test)]
mod tests {
use super::*;
fn luminance((r, g, b): (u8, u8, u8)) -> f64 {
0.299 * r as f64 + 0.587 * g as f64 + 0.114 * b as f64
}
#[test]
fn black_and_white_swap() {
assert_eq!(flip_lightness((0, 0, 0)), (255, 255, 255));
assert_eq!(flip_lightness((255, 255, 255)), (0, 0, 0));
}
#[test]
fn dark_background_becomes_light() {
let light = flip_lightness((13, 17, 23));
assert!(luminance(light) > 220.0, "got {light:?}");
}
#[test]
fn accent_hue_is_preserved() {
let (h_in, _, _) = rgb_to_hsl(79, 70, 229);
let flipped = flip_lightness((79, 70, 229));
let (h_out, _, _) = rgb_to_hsl(flipped.0, flipped.1, flipped.2);
assert!(
(h_in - h_out).abs() < 0.02,
"hue drifted: {h_in} -> {h_out}"
);
}
#[test]
fn rewrites_hex_shorthand_and_full() {
let out = to_light_mode(r##"<rect fill="#000" stroke="#ffffff"/>"##);
assert!(out.contains("#ffffff"), "shorthand black -> white: {out}");
assert!(out.contains("#000000"), "full white -> black: {out}");
}
#[test]
fn rewrites_rgb_function() {
let out = to_light_mode("fill:rgb(0, 0, 0)");
assert_eq!(out, "fill:rgb(255, 255, 255)");
}
#[test]
fn does_not_treat_srgb_attribute_as_rgb_function() {
let src = r##"<filter color-interpolation-filters="sRGB"><feGaussianBlur stdDeviation="1"/></filter><rect fill="rgb(13, 17, 23)"/>"##;
let out = to_light_mode(src);
assert!(
out.contains(r#"color-interpolation-filters="sRGB""#),
"{out}"
);
assert!(out.contains("<feGaussianBlur"), "{out}");
assert!(out.contains(r#"fill="rgb(232, 236, 242)""#), "{out}");
}
#[test]
fn leaves_non_colors_untouched() {
let src = r##"<g id="node1"><text>Cost is #1</text></g>"##;
assert_eq!(to_light_mode(src), src);
}
}