chromashift/
prophoto_rgb.rs1use crate::{ToAlpha, XyzD50, round_dp};
2use core::fmt;
3
4#[derive(Debug, Clone, Copy, PartialEq)]
11pub struct ProphotoRgb {
12 pub red: f64,
13 pub green: f64,
14 pub blue: f64,
15 pub alpha: f32,
16}
17
18impl ProphotoRgb {
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 ProphotoRgb {
25 fn to_alpha(&self) -> f32 {
26 self.alpha
27 }
28}
29
30impl fmt::Display for ProphotoRgb {
31 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32 let Self { red, green, blue, alpha } = self;
33 write!(f, "color(prophoto-rgb {} {} {}", 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
41fn gamma(u: f64) -> f64 {
44 let abs = u.abs();
45 if abs >= 1.0 / 512.0 { u.signum() * abs.powf(1.0 / 1.8) } else { u * 16.0 }
46}
47
48fn linear(c: f64) -> f64 {
50 let abs = c.abs();
51 if abs >= 16.0 / 512.0 { c.signum() * abs.powf(1.8) } else { c / 16.0 }
52}
53
54impl From<XyzD50> for ProphotoRgb {
55 fn from(value: XyzD50) -> Self {
56 let XyzD50 { x, y, z, alpha } = value;
57 let x = x / 100.0;
58 let y = y / 100.0;
59 let z = z / 100.0;
60 let lr = x * 1.3457868816471583 + y * (-0.25557208737979464) + z * (-0.05110186497554526);
62 let lg = x * (-0.5446307051249019) + y * 1.5082477428451468 + z * 0.02052744743642139;
63 let lb = x * 0.0 + y * 0.0 + z * 1.2119675456389452;
64 ProphotoRgb::new(gamma(lr), gamma(lg), gamma(lb), alpha)
66 }
67}
68
69impl From<ProphotoRgb> for XyzD50 {
70 fn from(value: ProphotoRgb) -> Self {
71 let ProphotoRgb { red, green, blue, alpha } = value;
72 let lr = linear(red);
74 let lg = linear(green);
75 let lb = linear(blue);
76 let x = lr * 0.7977666449006423 + lg * 0.13518129740053308 + lb * 0.0313477341283922;
78 let y = lr * 0.2880748288194013 + lg * 0.711835234241873 + lb * 0.00008993693872564;
79 let z = lr * 0.0 + lg * 0.0 + lb * 0.8251046025104602;
80 XyzD50::new(x * 100.0, y * 100.0, z * 100.0, alpha)
81 }
82}