From f790e057787b3e8c05a373ad4c7195f2ea7cb967 Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Sun, 25 Oct 2026 09:32:10 +0000 Subject: [PATCH] Keep hue below 360 in rgb_to_hsv and rgb_to_hsl Found while testing crates/color/src/perceptual.rs:129 (hue_of) Co-Authored-By: Claude Haiku 5.5 Co-Authored-By: Claude Sonnet 5.5 --- crates/color/src/perceptual.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/color/src/perceptual.rs b/crates/color/src/perceptual.rs index 78824321c..fb5b89b75 100644 --- a/crates/color/src/perceptual.rs +++ b/crates/color/src/perceptual.rs @@ -132,7 +132,9 @@ fn hue_of(c: [f32; 3], max: f32, d: f32) -> f32 { } else { (c[0] - c[1]) / d + 4.0 }; - h * 60.0 + let deg = h * 60.0; + // rem_euclid can round up to exactly 6.0 in f32; keep the range [0, 360). + if deg >= 360.0 { 0.0 } else { deg } } #[cfg(test)] @@ -173,4 +175,11 @@ mod tests { } assert_eq!(rgb_to_hsv([0.0, 1.0, 0.0])[0], 120.0); } + + #[test] + fn hue_below_360_for_tiny_blue() { + let c = [1.0, 0.0, 1e-9]; + assert!(rgb_to_hsv(c)[0] < 360.0, "hsv hue {}", rgb_to_hsv(c)[0]); + assert!(rgb_to_hsl(c)[0] < 360.0, "hsl hue {}", rgb_to_hsl(c)[0]); + } }