chromashift/
rec2020.rs

1use crate::{ToAlpha, XyzD65, round_dp};
2use core::fmt;
3
4/// A colour in the Rec. 2020 colour space.
5/// The components are:
6/// - Red - a number between 0.0 and 1.0
7/// - Green - a number between 0.0 and 1.0
8/// - Blue - 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 Rec2020 {
12	pub red: f64,
13	pub green: f64,
14	pub blue: f64,
15	pub alpha: f32,
16}
17
18impl Rec2020 {
19	pub fn new(red: f64, green: f64, blue: f64, alpha: f32) -> Self {
20		Self { red, green, blue, alpha: alpha.clamp(0.0, 100.0) }
21	}
22}
23
24impl ToAlpha for Rec2020 {
25	fn to_alpha(&self) -> f32 {
26		self.alpha
27	}
28}
29
30impl fmt::Display for Rec2020 {
31	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32		let Self { red, green, blue, alpha } = self;
33		write!(f, "color(rec2020 {} {} {}", round_dp(*red, 2), round_dp(*green, 2), round_dp(*blue, 2))?;
34		if *alpha < 100.0 {
35			write!(f, " / {}", round_dp(*alpha as f64, 2))?;
36		}
37		write!(f, ")")
38	}
39}
40
41const ALPHA: f64 = 1.09929682680944;
42const BETA: f64 = 0.018053968510807;
43
44/// BT.2020 transfer function: linear to gamma-encoded
45fn gamma(u: f64) -> f64 {
46	let abs = u.abs();
47	if abs >= BETA { u.signum() * (ALPHA * abs.powf(0.45) - (ALPHA - 1.0)) } else { u * 4.5 }
48}
49
50/// BT.2020 transfer function: gamma-encoded to linear
51fn linear(c: f64) -> f64 {
52	let abs = c.abs();
53	if abs >= BETA * 4.5 { c.signum() * ((abs + (ALPHA - 1.0)) / ALPHA).powf(1.0 / 0.45) } else { c / 4.5 }
54}
55
56impl From<XyzD65> for Rec2020 {
57	fn from(value: XyzD65) -> Self {
58		let XyzD65 { x, y, z, alpha } = value;
59		let x = x / 100.0;
60		let y = y / 100.0;
61		let z = z / 100.0;
62		// XYZ D65 -> Linear Rec. 2020
63		let lr = x * 1.7166511879712674 + y * (-0.35567078377639233) + z * (-0.25336628137365974);
64		let lg = x * (-0.666684351832489) + y * 1.616481236634939 + z * 0.01576854581391113;
65		let lb = x * 0.017639857445310783 + y * (-0.042770613257808524) + z * 0.9421031212354738;
66		// Apply BT.2020 gamma
67		Rec2020::new(gamma(lr), gamma(lg), gamma(lb), alpha)
68	}
69}
70
71impl From<Rec2020> for XyzD65 {
72	fn from(value: Rec2020) -> Self {
73		let Rec2020 { red, green, blue, alpha } = value;
74		// Linearize with BT.2020 gamma
75		let lr = linear(red);
76		let lg = linear(green);
77		let lb = linear(blue);
78		// Linear Rec. 2020 -> XYZ D65
79		let x = lr * 0.6369580483012914 + lg * 0.14461690358620832 + lb * 0.16888097516417205;
80		let y = lr * 0.2627002120112671 + lg * 0.6779980715188708 + lb * 0.05930171646986196;
81		let z = lr * 0.0 + lg * 0.028072693049087428 + lb * 1.0609850577107909;
82		XyzD65::new(x * 100.0, y * 100.0, z * 100.0, alpha)
83	}
84}