use crate::{LinearRgb, ToAlpha, round_dp};
use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct A98Rgb {
pub red: f64,
pub green: f64,
pub blue: f64,
pub alpha: f32,
}
impl A98Rgb {
pub fn new(red: f64, green: f64, blue: f64, alpha: f32) -> Self {
Self { red, green, blue, alpha: alpha.clamp(0.0, 100.0) }
}
}
impl ToAlpha for A98Rgb {
fn to_alpha(&self) -> f32 {
self.alpha
}
}
impl fmt::Display for A98Rgb {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let Self { red, green, blue, alpha } = self;
write!(f, "color(a98-rgb {} {}% {}%", round_dp(*red, 2), round_dp(*green, 2), round_dp(*blue, 2))?;
if *alpha < 100.0 {
write!(f, " / {}", round_dp(*alpha as f64, 2))?;
}
write!(f, ")")
}
}
impl From<LinearRgb> for A98Rgb {
fn from(value: LinearRgb) -> Self {
let LinearRgb { red, green, blue, alpha } = value;
const INV_GAMMA: f64 = 256.0 / 563.0;
let gamma_red = red.signum() * red.abs().powf(INV_GAMMA);
let gamma_green = green.signum() * green.abs().powf(INV_GAMMA);
let gamma_blue = blue.signum() * blue.abs().powf(INV_GAMMA);
A98Rgb::new(gamma_red, gamma_green, gamma_blue, alpha)
}
}
impl From<A98Rgb> for LinearRgb {
fn from(value: A98Rgb) -> Self {
let A98Rgb { red, green, blue, alpha } = value;
const GAMMA: f64 = 563.0 / 256.0;
let linear_red = red.signum() * red.abs().powf(GAMMA);
let linear_green = green.signum() * green.abs().powf(GAMMA);
let linear_blue = blue.signum() * blue.abs().powf(GAMMA);
LinearRgb::new(linear_red, linear_green, linear_blue, alpha)
}
}