Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Keep Color::to_hsb hue below 360

NetterSilver8832b0cc91d6
diff --git a/crates/color/src/lib.rs b/crates/color/src/lib.rs
index d085c5c48..c83d93f0c 100644
--- a/crates/color/src/lib.rs
+++ b/crates/color/src/lib.rs
@@ -118,6 +118,8 @@ impl Color {
} else {
60.0 * ((r - g) / d + 4.0)
};
+ // A tiny negative `rem_euclid` input can round up to exactly 6.0 (hue 360).
+ let h = if h >= 360.0 { 0.0 } else { h };
let s = if max <= 0.0 { 0.0 } else { d / max };
[h, s, max]
}
@@ -251,6 +253,12 @@ impl Paint {
mod tests {
use super::*;
+ #[test]
+ fn hsb_hue_stays_below_360() {
+ let h = Color::rgb(1.0, 0.0, 1e-8).to_hsb()[0];
+ assert!((0.0..360.0).contains(&h), "hue {h}");
+ }
+
#[test]
fn hex_roundtrip() {
let c = Color::from_hex("#ff8000").unwrap();