use crate::spaces::{Rgb, Xyz65};
use crate::traits::ColorSpace;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Prismatic {
pub l: f64,
pub r: f64,
pub g: f64,
pub b: f64,
pub alpha: Option<f64>,
}
impl ColorSpace for Prismatic {
const MODE: &'static str = "prismatic";
const CHANNELS: &'static [&'static str] = &["l", "r", "g", "b"];
fn alpha(&self) -> Option<f64> {
self.alpha
}
fn with_alpha(self, alpha: Option<f64>) -> Self {
Self { alpha, ..self }
}
fn to_xyz65(&self) -> Xyz65 {
Rgb::from(*self).to_xyz65()
}
fn from_xyz65(xyz: Xyz65) -> Self {
Rgb::from_xyz65(xyz).into()
}
}
impl From<Rgb> for Prismatic {
fn from(c: Rgb) -> Self {
let s = c.r + c.g + c.b;
let l = c.r.max(c.g).max(c.b);
if s > 0.0 {
Self {
l,
r: c.r / s,
g: c.g / s,
b: c.b / s,
alpha: c.alpha,
}
} else {
Self {
l,
r: 0.0,
g: 0.0,
b: 0.0,
alpha: c.alpha,
}
}
}
}
impl From<Prismatic> for Rgb {
fn from(c: Prismatic) -> Self {
let m = c.r.max(c.g).max(c.b);
if m > 0.0 {
let scale = c.l / m;
Rgb {
r: c.r * scale,
g: c.g * scale,
b: c.b * scale,
alpha: c.alpha,
}
} else {
Rgb {
r: 0.0,
g: 0.0,
b: 0.0,
alpha: c.alpha,
}
}
}
}