chromashift/
xyzd65.rs

1use crate::round_dp;
2use core::fmt;
3
4/// A colour expressed as X, Y and Z values, expressed in the CIE XYZ tristimulus colour space, with an explicit D65
5/// white point.
6/// The components are:
7/// - X - a number between 0.0 and 100.0
8/// - Y - a number between 0.0 and 100.0
9/// - Z - a number between 0.0 and 100.0
10/// - Alpha - a number between 0.0 and 100.0
11#[derive(Debug, Clone, Copy, PartialEq)]
12pub struct XyzD65 {
13	pub x: f64,
14	pub y: f64,
15	pub z: f64,
16	pub alpha: f32,
17}
18
19impl XyzD65 {
20	pub fn new(x: f64, y: f64, z: f64, alpha: f32) -> Self {
21		Self { x: x.clamp(0.0, 100.0), y: y.clamp(0.0, 100.0), z: z.clamp(0.0, 100.0), alpha: alpha.clamp(0.0, 100.0) }
22	}
23}
24
25impl fmt::Display for XyzD65 {
26	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27		let Self { x, y, z, alpha } = self;
28		write!(f, "color(xyz-d65 {} {}% {}%", round_dp(*x, 4), round_dp(*y, 4), round_dp(*z, 4))?;
29		if *alpha < 100.0 {
30			write!(f, " / {}", round_dp(*alpha as f64, 2))?;
31		}
32		write!(f, ")")
33	}
34}