Skip to main content

rust_css_parser/syntax/color/
oklab.rs

1//! Port of `src/syntax/color/oklab.ts`.
2
3use super::color::{color2srgbvalues, get_number};
4use super::lab::lchvalues2labvalues;
5use super::oklch::get_oklch_components;
6use super::srgb::{cmyk2srgbvalues, hex2srgbvalues, hsl2srgb, hwb2srgbvalues, lab2srgbvalues, lch2srgbvalues, lsrgb2srgbvalues, rgb2srgb, srgb2lsrgbvalues};
7use super::tokens::{number, percentage, slash};
8use super::utils::components::get_color_components;
9use super::utils::matrix::multiply_matrices;
10use super::utils::values::{at, opt};
11use crate::ast::arena::AstArena;
12use crate::ast::types::{ColorType, EnumToken};
13
14macro_rules! to_token {
15    ($name:ident, $values:ident) => {
16        pub fn $name(arena: &AstArena, token: u32) -> Option<u32> {
17            let values = $values(arena, token)?;
18            Some(oklab_token(arena, &values))
19        }
20    };
21}
22
23to_token!(hex2oklab_token, hex2oklabvalues);
24to_token!(rgb2oklab_token, rgb2oklabvalues);
25to_token!(hsl2oklab_token, hsl2oklabvalues);
26to_token!(hwb2oklab_token, hwb2oklabvalues);
27to_token!(cmyk2oklab_token, cmyk2oklabvalues);
28to_token!(lab2oklab_token, lab2oklabvalues);
29to_token!(lch2oklab_token, lch2oklabvalues);
30to_token!(oklch2oklab_token, oklch2oklabvalues);
31to_token!(color2oklab_token, color2oklabvalues);
32
33fn oklab_token(arena: &AstArena, values: &[f64]) -> u32 {
34    let mut chi = vec![number(arena, at(values, 0)), number(arena, at(values, 1)), number(arena, at(values, 2))];
35    if values.len() == 4 && values[3] != 1.0 {
36        chi.push(slash(arena));
37        chi.push(percentage(arena, values[3] * 100.0));
38    }
39    arena.create_color("oklab", ColorType::OKLAB, Some(&chi))
40}
41
42fn to_oklab(values: &[f64]) -> Vec<f64> {
43    srgb2oklab(at(values, 0), at(values, 1), at(values, 2), opt(values, 3))
44}
45
46pub fn hex2oklabvalues(arena: &AstArena, token: u32) -> Option<Vec<f64>> {
47    Some(to_oklab(&hex2srgbvalues(arena, token)))
48}
49
50pub fn rgb2oklabvalues(arena: &AstArena, token: u32) -> Option<Vec<f64>> {
51    Some(to_oklab(&rgb2srgb(arena, token)?))
52}
53
54pub fn hsl2oklabvalues(arena: &AstArena, token: u32) -> Option<Vec<f64>> {
55    Some(to_oklab(&hsl2srgb(arena, token)?))
56}
57
58pub fn hwb2oklabvalues(arena: &AstArena, token: u32) -> Option<Vec<f64>> {
59    Some(to_oklab(&hwb2srgbvalues(arena, token)?))
60}
61
62pub fn cmyk2oklabvalues(arena: &AstArena, token: u32) -> Option<Vec<f64>> {
63    Some(to_oklab(&cmyk2srgbvalues(arena, token)?))
64}
65
66pub fn lab2oklabvalues(arena: &AstArena, token: u32) -> Option<Vec<f64>> {
67    Some(to_oklab(&lab2srgbvalues(arena, token)?))
68}
69
70pub fn lch2oklabvalues(arena: &AstArena, token: u32) -> Option<Vec<f64>> {
71    Some(to_oklab(&lch2srgbvalues(arena, token)?))
72}
73
74pub fn oklch2oklabvalues(arena: &AstArena, token: u32) -> Option<Vec<f64>> {
75    let values = get_oklch_components(arena, token)?;
76    Some(lchvalues2labvalues(at(&values, 0), at(&values, 1), at(&values, 2), opt(&values, 3)))
77}
78
79fn color2oklabvalues(arena: &AstArena, token: u32) -> Option<Vec<f64>> {
80    Some(to_oklab(&color2srgbvalues(arena, token)?))
81}
82
83pub fn srgb2oklab(r: f64, g: f64, blue: f64, alpha: Option<f64>) -> Vec<f64> {
84    let lin = srgb2lsrgbvalues(r, g, blue, None);
85    let (r, g, blue) = (lin[0], lin[1], lin[2]);
86    let l_ = (0.41222147079999993 * r + 0.5363325363 * g + 0.0514459929 * blue).cbrt();
87    let m_ = (0.2119034981999999 * r + 0.6806995450999999 * g + 0.1073969566 * blue).cbrt();
88    let s_ = (0.08830246189999998 * r + 0.2817188376 * g + 0.6299787005000002 * blue).cbrt();
89    let l = 0.2104542553 * l_ + 0.793617785 * m_ - 0.0040720468 * s_;
90    let gray = r == g && g == blue;
91    let a = if gray { 0.0 } else { 1.9779984951 * l_ - 2.428592205 * m_ + 0.4505937099 * s_ };
92    let b = if gray { 0.0 } else { 0.0259040371 * l_ + 0.7827717662 * m_ - 0.808675766 * s_ };
93    match alpha {
94        Some(alpha) if alpha != 1.0 => vec![l, a, b, alpha],
95        _ => vec![l, a, b],
96    }
97}
98
99pub fn get_oklab_components(arena: &AstArena, token: u32) -> Option<Vec<f64>> {
100    let components = get_color_components(arena, token)?;
101    if components.len() < 3 {
102        return None;
103    }
104    let scaled = |i: usize, factor: f64| {
105        let t = components[i];
106        get_number(arena, t) * if arena.data_types.get(t) == EnumToken::PercentageTokenType { factor } else { 1.0 }
107    };
108    let l = get_number(arena, components[0]);
109    let a = scaled(1, 0.4);
110    let b = scaled(2, 0.4);
111    let mut rgb = vec![l, a, b];
112    if components.len() > 3 {
113        let alpha = get_number(arena, components[3]);
114        if alpha != 1.0 {
115            rgb.push(alpha);
116        }
117    }
118    Some(rgb)
119}
120
121#[allow(non_snake_case)]
122pub fn oklab_to_xyz(l: f64, a: f64, b: f64, alpha: Option<f64>) -> Vec<f64> {
123    const LMS_TO_XYZ: [[f64; 3]; 3] = [
124        [1.2268798758459243, -0.5578149944602171, 0.2813910456659647],
125        [-0.0405757452148008, 1.112286803280317, -0.0717110580655164],
126        [-0.0763729366746601, -0.4214933324022432, 1.5869240198367816],
127    ];
128    const OKLAB_TO_LMS: [[f64; 3]; 3] = [
129        [1.0, 0.3963377773761749, 0.2158037573099136],
130        [1.0, -0.1055613458156586, -0.0638541728258133],
131        [1.0, -0.0894841775298119, -1.2914855480194092],
132    ];
133    let lms = multiply_matrices(&OKLAB_TO_LMS, [l, a, b]);
134    let mut xyz = multiply_matrices(&LMS_TO_XYZ, [lms[0].powf(3.0), lms[1].powf(3.0), lms[2].powf(3.0)]);
135    if let Some(alpha) = alpha {
136        xyz.push(alpha);
137    }
138    xyz
139}
140
141#[allow(non_snake_case)]
142pub fn oklab_to_srgb(l: f64, a: f64, b: f64) -> Vec<f64> {
143    let L = (l * 0.99999999845051981432 + 0.39633779217376785678 * a + 0.21580375806075880339 * b).powf(3.0);
144    let M = (l * 1.0000000088817607767 - 0.1055613423236563494 * a - 0.063854174771705903402 * b).powf(3.0);
145    let S = (l * 1.0000000546724109177 - 0.089484182094965759684 * a - 1.2914855378640917399 * b).powf(3.0);
146    lsrgb2srgbvalues(
147        4.076741661347994 * L - 3.307711590408193 * M + 0.230969928729428 * S,
148        -1.2684380040921763 * L + 2.6097574006633715 * M - 0.3413193963102197 * S,
149        -0.004196086541837188 * L - 0.7034186144594493 * M + 1.7076147009309444 * S,
150        None,
151    )
152}