Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

Keep hue below 360 in rgb_to_hsv and rgb_to_hsl

NetterSilverf790e057787b
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]);
+ }
}