tincture/
lib.rs

1#![allow(clippy::excessive_precision)]
2
3#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
4pub struct Oklab {
5    pub l: f32,
6    pub a: f32,
7    pub b: f32,
8}
9
10#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
11pub struct Oklch {
12    pub l: f32,
13    pub c: f32,
14    pub h: f32,
15}
16
17#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
18pub struct LinearSrgb {
19    pub r: f32,
20    pub g: f32,
21    pub b: f32,
22}
23
24#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
25pub struct Srgb {
26    pub r: f32,
27    pub g: f32,
28    pub b: f32,
29}
30
31pub fn linear_srgb_to_srgb(c: LinearSrgb) -> Srgb {
32    let nonlinearize = |x: f32| {
33        if x >= 0.0031308 {
34            x.powf(1.0 / 2.4) * 1.055 - 0.055
35        } else {
36            x * 12.92
37        }
38    };
39
40    Srgb { r: nonlinearize(c.r), g: nonlinearize(c.g), b: nonlinearize(c.b) }
41}
42
43pub fn srgb_to_linear_srgb(c: Srgb) -> LinearSrgb {
44    let linearize = |x: f32| {
45        if x >= 0.04045 {
46            ((x + 0.055) / 1.055).powf(2.4)
47        } else {
48            x / 12.92
49        }
50    };
51
52    LinearSrgb { r: linearize(c.r), g: linearize(c.g), b: linearize(c.b) }
53}
54
55pub fn linear_srgb_to_oklab(c: LinearSrgb) -> Oklab {
56    let l = 0.4122214708 * c.r + 0.5363325363 * c.g + 0.0514459929 * c.b;
57    let m = 0.2119034982 * c.r + 0.6806995451 * c.g + 0.1073969566 * c.b;
58    let s = 0.0883024619 * c.r + 0.2817188376 * c.g + 0.6299787005 * c.b;
59
60    let l_ = l.cbrt();
61    let m_ = m.cbrt();
62    let s_ = s.cbrt();
63
64    Oklab {
65        l: 0.2104542553 * l_ + 0.7936177850 * m_ - 0.0040720468 * s_,
66        a: 1.9779984951 * l_ - 2.4285922050 * m_ + 0.4505937099 * s_,
67        b: 0.0259040371 * l_ + 0.7827717662 * m_ - 0.8086757660 * s_,
68    }
69}
70
71pub fn oklab_to_linear_srgb(c: Oklab) -> LinearSrgb {
72    let l_ = c.l + 0.3963377774 * c.a + 0.2158037573 * c.b;
73    let m_ = c.l - 0.1055613458 * c.a - 0.0638541728 * c.b;
74    let s_ = c.l - 0.0894841775 * c.a - 1.2914855480 * c.b;
75
76    let l = l_ * l_ * l_;
77    let m = m_ * m_ * m_;
78    let s = s_ * s_ * s_;
79
80    LinearSrgb {
81        r: 4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
82        g: -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
83        b: -0.0041960863 * l - 0.7034186147 * m + 1.7076147010 * s,
84    }
85}
86
87pub fn oklab_to_oklch(c: Oklab) -> Oklch {
88    Oklch { l: c.l, c: c.a.hypot(c.b), h: c.b.atan2(c.a) }
89}
90
91pub fn oklch_to_oklab(c: Oklch) -> Oklab {
92    Oklab { l: c.l, a: c.c * c.h.cos(), b: c.c * c.h.sin() }
93}
94
95impl Srgb {
96    pub fn hex(self) -> u32 {
97        let (r, g, b) = self.components();
98        (r as u32) << 16 | (g as u32) << 8 | b as u32
99    }
100
101    pub fn components(self) -> (u8, u8, u8) {
102        let scale = |n: f32| (n * 255.0).round() as u8;
103        (scale(self.r), scale(self.g), scale(self.b))
104    }
105}
106
107#[cfg(test)]
108mod tests {
109    use super::*;
110
111    #[test]
112    fn hex() {
113        assert_eq!(Srgb { r: 0.0, g: 0.0, b: 0.0 }.hex(), 0x000000);
114        assert_eq!(Srgb { r: 1.0, g: 0.0, b: 0.0 }.hex(), 0xFF0000);
115        assert_eq!(Srgb { r: 0.0, g: 1.0, b: 0.0 }.hex(), 0x00FF00);
116        assert_eq!(Srgb { r: 0.0, g: 0.0, b: 1.0 }.hex(), 0x0000FF);
117        assert_eq!(Srgb { r: 1.0, g: 1.0, b: 1.0 }.hex(), 0xFFFFFF);
118        assert_eq!(Srgb { r: 0.5, g: 0.3, b: 0.8 }.hex(), 0x804DCC);
119    }
120}