use super::{LinearSrgb, Oklab};
use num_traits::Euclid;
#[allow(unused_imports)]
use num_traits::Float;
#[derive(Debug, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Okhsl {
pub h: f32,
pub s: f32,
pub l: f32,
}
impl Okhsl {
pub fn new(h: f32, s: f32, l: f32) -> Self {
Okhsl {
h: Euclid::rem_euclid(&h, &1.),
s: s.clamp(0., 1.),
l: l.clamp(0., 1.),
}
}
pub fn to_oklab(&self) -> Oklab {
let l = self.l;
let max_c = if l < 0.5 { 0.4 * l } else { 0.4 * (1.0 - l) };
let c = self.s * max_c;
let angle = 2.0 * core::f32::consts::PI * self.h;
let a = c * angle.cos();
let b = c * angle.sin();
Oklab { l, a, b }
}
pub fn to_linear_srgb(&self) -> LinearSrgb {
self.to_oklab().to_linear_srgb()
}
}