19 lines
598 B
Rust
19 lines
598 B
Rust
/// converts an sRGB triple in 0..=1 to hue/saturation/value, all in 0..=1.
|
|
pub fn rgb_to_hsv(r: f32, g: f32, b: f32) -> (f32, f32, f32) {
|
|
let max = r.max(g).max(b);
|
|
let min = r.min(g).min(b);
|
|
let v = max;
|
|
let d = max - min;
|
|
let s = if max <= 0.0 { 0.0 } else { d / max };
|
|
let h = if d <= 1e-6 {
|
|
0.0
|
|
} else if (max - r).abs() < f32::EPSILON {
|
|
((g - b) / d).rem_euclid(6.0) / 6.0
|
|
} else if (max - g).abs() < f32::EPSILON {
|
|
((b - r) / d + 2.0) / 6.0
|
|
} else {
|
|
((r - g) / d + 4.0) / 6.0
|
|
};
|
|
((h + 1.0).rem_euclid(1.0), s, v)
|
|
}
|