use super::LinearSrgb;
#[allow(unused_imports)]
use num_traits::Float;
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct GammaSrgb {
pub red: f32,
pub green: f32,
pub blue: f32,
pub gamma: f32,
}
impl GammaSrgb {
pub fn new(red: f32, green: f32, blue: f32, gamma: f32) -> Self {
Self {
red: red.clamp(0.0, 1.0),
green: green.clamp(0.0, 1.0),
blue: blue.clamp(0.0, 1.0),
gamma,
}
}
pub fn from_linear_srgb(linear_srgb: LinearSrgb, gamma: f32) -> Self {
Self {
red: gamma_encode(linear_srgb.red, gamma),
green: gamma_encode(linear_srgb.green, gamma),
blue: gamma_encode(linear_srgb.blue, gamma),
gamma,
}
}
pub fn to_linear_srgb(self) -> LinearSrgb {
LinearSrgb {
red: gamma_decode(self.red, self.gamma),
green: gamma_decode(self.green, self.gamma),
blue: gamma_decode(self.blue, self.gamma),
}
}
}
#[inline]
fn gamma_decode(c: f32, gamma: f32) -> f32 {
c.powf(gamma)
}
#[inline]
fn gamma_encode(c: f32, gamma: f32) -> f32 {
c.powf(1.0 / gamma)
}