chromashift/
linear_rgb.rs

1use crate::{ToAlpha, XyzD65, round_dp};
2use core::fmt;
3
4/// A device independent expression of RGB. No exactly 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 LinearRgb {
12	pub red: f64,
13	pub green: f64,
14	pub blue: f64,
15	pub alpha: f32,
16}
17
18impl LinearRgb {
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 LinearRgb {
30	fn to_alpha(&self) -> f32 {
31		self.alpha
32	}
33}
34
35impl fmt::Display for LinearRgb {
36	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37		let Self { red, green, blue, alpha } = self;
38		write!(f, "color(srgb-linear {} {}% {}%", 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<XyzD65> for LinearRgb {
47	fn from(value: XyzD65) -> Self {
48		let XyzD65 { x, y, z, alpha } = value;
49		let x = x / 100.0;
50		let y = y / 100.0;
51		let z = z / 100.0;
52		let red = x * (12831.0 / 3959.0) + y * (-329.0 / 214.0) + z * (-1974.0 / 3959.0);
53		let green = x * (-851781.0 / 878810.0) + y * (1648619.0 / 878810.0) + z * (36519.0 / 878810.0);
54		let blue = x * (705.0 / 12673.0) + y * (-2585.0 / 12673.0) + z * (705.0 / 667.0);
55		LinearRgb::new(red, green, blue, alpha)
56	}
57}
58
59impl From<LinearRgb> for XyzD65 {
60	fn from(value: LinearRgb) -> Self {
61		let LinearRgb { red, green, blue, alpha } = value;
62		let x = red * (506752.0 / 1228815.0) + green * (87881.0 / 245763.0) + blue * (12673.0 / 70218.0);
63		let y = red * (87098.0 / 409605.0) + green * (175762.0 / 245763.0) + blue * (12673.0 / 175545.0);
64		let z = red * (7918.0 / 409605.0) + green * (87881.0 / 737289.0) + blue * (1001167.0 / 1053270.0);
65		XyzD65::new(x * 100.0, y * 100.0, z * 100.0, alpha)
66	}
67}