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 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
NetterSilver
2026-10-25 09:32:10 +00:00
co-authored by Claude Haiku 5.5
parent 31ec62cd03
commit f790e05778
+10 -1
View File
@@ -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]);
}
}