From 4a29fd4112f3df24b6c9e298c3bb19dac01d0326 Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Thu, 22 Oct 2026 20:05:19 +0200 Subject: [PATCH] Wrap to_hsb hue of 360 back to 0 for near-red colours Found while testing crates/color/src/lib.rs:142 Co-Authored-By: Claude Haiku 5.5 Co-Authored-By: Claude Sonnet 5.5 --- crates/color/src/lib.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/crates/color/src/lib.rs b/crates/color/src/lib.rs index aff5c5122..d7921a948 100644 --- a/crates/color/src/lib.rs +++ b/crates/color/src/lib.rs @@ -146,6 +146,8 @@ impl Color { } else { 60.0 * ((r - g) / d + 4.0) }; + // f32 rounding in rem_euclid can yield exactly 360; wrap back to 0. + let h = if h >= 360.0 { 0.0 } else { h }; let s = if max <= 0.0 { 0.0 } else { d / max }; [h, s, max] } @@ -438,6 +440,12 @@ impl Paint { mod tests { use super::*; + #[test] + fn hue_below_360() { + let h = Color::Rgb { r: 1.0, g: 0.0, b: 1e-7 }.to_hsb()[0]; + assert!((0.0..360.0).contains(&h), "hue {h}"); + } + #[test] fn hex_roundtrip() { let c = Color::from_hex("#ff8000").unwrap();