mirror of
https://github.com/storytold/effectcraft.git
synced 2026-10-24 10:09:32 -06:00
Keep rgb_to_hsv and rgb_to_hsl hue within 0..1
Found while testing crates/color/src/lib.rs:133 (rgb_to_hsv) and :96 (rgb_to_hsl) Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com> Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> 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:
co-authored by
Claude Haiku 5.5
parent
b55cb5bc15
commit
fdfa286079
+10
-2
@@ -93,7 +93,7 @@ pub fn rgb_to_hsl(r: f32, g: f32, b: f32) -> (f32, f32, f32) {
|
||||
let d = max - min;
|
||||
let s = if l > 0.5 { d / (2.0 - max - min) } else { d / (max + min) };
|
||||
let h = if max == r {
|
||||
((g - b) / d + if g < b { 6.0 } else { 0.0 }) / 6.0
|
||||
(((g - b) / d + if g < b { 6.0 } else { 0.0 }) / 6.0).rem_euclid(1.0)
|
||||
} else if max == g {
|
||||
((b - r) / d + 2.0) / 6.0
|
||||
} else {
|
||||
@@ -130,7 +130,7 @@ pub fn rgb_to_hsv(r: f32, g: f32, b: f32) -> (f32, f32, f32) {
|
||||
let h = if d < 1e-7 {
|
||||
0.0
|
||||
} else if max == r {
|
||||
((g - b) / d).rem_euclid(6.0) / 6.0
|
||||
(((g - b) / d).rem_euclid(6.0) / 6.0).rem_euclid(1.0)
|
||||
} else if max == g {
|
||||
((b - r) / d + 2.0) / 6.0
|
||||
} else {
|
||||
@@ -262,6 +262,14 @@ mod tests {
|
||||
assert!(r > 0.99 && g < 0.01 && b < 0.01, "{r} {g} {b}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hue_stays_below_one_for_tiny_blue() {
|
||||
let (h, _, _) = rgb_to_hsv(1.0, 0.0, 1e-9);
|
||||
assert!((0.0..1.0).contains(&h), "rgb_to_hsv hue {h} not in 0..1");
|
||||
let (h, _, _) = rgb_to_hsl(1.0, 0.0, 1e-9);
|
||||
assert!((0.0..1.0).contains(&h), "rgb_to_hsl hue {h} not in 0..1");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn srgb_roundtrip() {
|
||||
for i in 0..=100 {
|
||||
|
||||
Reference in New Issue
Block a user