chromashift/
a98_rgb.rs

1use crate::{LinearRgb, ToAlpha, round_dp};
2use core::fmt;
3
4/// An RGB colour space with defined chromacities.
5/// The components are:
6/// - Red - a number between 0.0 and 1.0
7/// - Blue - a number between 0.0 and 1.0
8/// - Green - a number between 0.0 and 1.0
9/// - Alpha - a number between 0.0 and 100.0
10#[derive(Debug, Clone, Copy, PartialEq)]
11pub struct A98Rgb {
12	pub red: f64,
13	pub green: f64,
14	pub blue: f64,
15	pub alpha: f32,
16}
17
18impl A98Rgb {
19	pub fn new(red: f64, green: f64, blue: f64, alpha: f32) -> Self {
20		Self {
21			red: red.clamp(0.0, 1.0),
22			green: green.clamp(0.0, 1.0),
23			blue: blue.clamp(0.0, 1.0),
24			alpha: alpha.clamp(0.0, 100.0),
25		}
26	}
27}
28
29impl ToAlpha for A98Rgb {
30	fn to_alpha(&self) -> f32 {
31		self.alpha
32	}
33}
34
35impl fmt::Display for A98Rgb {
36	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37		let Self { red, green, blue, alpha } = self;
38		write!(f, "color(a98-rgb {} {}% {}%", round_dp(*red, 2), round_dp(*green, 2), round_dp(*blue, 2))?;
39		if *alpha < 100.0 {
40			write!(f, " / {}", round_dp(*alpha as f64, 2))?;
41		}
42		write!(f, ")")
43	}
44}
45
46impl From<LinearRgb> for A98Rgb {
47	fn from(value: LinearRgb) -> Self {
48		let LinearRgb { red, green, blue, alpha } = value;
49		const INV_GAMMA: f64 = 256.0 / 563.0;
50		let gamma_red = red.signum() * red.abs().powf(INV_GAMMA);
51		let gamma_green = green.signum() * green.abs().powf(INV_GAMMA);
52		let gamma_blue = blue.signum() * blue.abs().powf(INV_GAMMA);
53		A98Rgb::new(gamma_red, gamma_green, gamma_blue, alpha)
54	}
55}
56
57impl From<A98Rgb> for LinearRgb {
58	fn from(value: A98Rgb) -> Self {
59		let A98Rgb { red, green, blue, alpha } = value;
60		const GAMMA: f64 = 563.0 / 256.0;
61		let linear_red = red.signum() * red.abs().powf(GAMMA);
62		let linear_green = green.signum() * green.abs().powf(GAMMA);
63		let linear_blue = blue.signum() * blue.abs().powf(GAMMA);
64		LinearRgb::new(linear_red, linear_green, linear_blue, alpha)
65	}
66}