From c2fb35b24485113302ecb09b50e0a049f4832550 Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Tue, 27 Oct 2026 11:40:52 +0100 Subject: [PATCH] Fix ColorKey Display overflow on huge negative Lab a/b Found while testing crates/color/src/recolor.rs:72 (Display for ColorKey) Co-Authored-By: Claude Haiku 5.5 Co-Authored-By: Claude Sonnet 5.5 --- crates/color/src/recolor.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/crates/color/src/recolor.rs b/crates/color/src/recolor.rs index 818a5fbe4..e88f767a5 100644 --- a/crates/color/src/recolor.rs +++ b/crates/color/src/recolor.rs @@ -69,7 +69,7 @@ impl fmt::Display for ColorKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { // Components are hundredths of the written units: exact with two decimals. let num = |q: i32| { - let s = format!("{}{}.{:02}", if q < 0 { "-" } else { "" }, q.abs() / 100, q.abs() % 100); + let s = format!("{}{}.{:02}", if q < 0 { "-" } else { "" }, q.unsigned_abs() / 100, q.unsigned_abs() % 100); s.trim_end_matches('0').trim_end_matches('.').to_string() }; let [a, b, c, k] = self.v.map(num); @@ -461,6 +461,14 @@ impl Rng { mod tests { use super::*; + #[test] + fn key_display_survives_saturated_lab() { + for text in ["lab 50 -1e20 0", "lab 50 0 -1e20", "lab 50 1e20 0"] { + let k = ColorKey::parse(text).unwrap(); + assert_eq!(ColorKey::parse(&k.to_string()), Some(k), "{text}"); + } + } + fn close(a: Color, b: Color) -> bool { let v = |c: Color| match c { Color::Cmyk { c, m, y, k } => vec![c, m, y, k],