Skip to main content

rust_css_parser/syntax/color/
hsl.rs

1//! Port of `src/syntax/color/hsl.ts`.
2
3use super::color::{color2srgbvalues, get_number};
4use super::hwb::hwbvalues;
5use super::rgb::{cmyk2rgbvalues, lab2rgbvalues, lch2rgbvalues};
6use super::srgb::{hex2srgbvalues, oklab2srgbvalues, oklch2srgbvalues};
7use super::tokens::{number, percentage, slash};
8use super::utils::components::get_color_components;
9use super::utils::values::{at, js_max, js_min, opt};
10use crate::ast::arena::AstArena;
11use crate::ast::types::ColorType;
12
13macro_rules! to_token {
14    ($name:ident, $values:ident) => {
15        pub fn $name(arena: &AstArena, token: u32) -> Option<u32> {
16            let values = $values(arena, token)?;
17            Some(hsl_token(arena, values))
18        }
19    };
20}
21
22pub fn hex2hsl_token(arena: &AstArena, token: u32) -> Option<u32> {
23    let values = hex2srgbvalues(arena, token);
24    Some(hsl_token(arena, srgb2hsl(&values)))
25}
26
27to_token!(rgb2hsl_token, rgb2hslvalues);
28to_token!(hwb2hsl_token, hwb2hslvalues);
29to_token!(cmyk2hsl_token, cmyk2hslvalues);
30to_token!(oklab2hsl_token, oklab2hslvalues);
31to_token!(oklch2hsl_token, oklch2hslvalues);
32to_token!(lab2hsl_token, lab2hslvalues);
33to_token!(lch2hsl_token, lch2hslvalues);
34
35pub fn color2hsl_token(arena: &AstArena, token: u32) -> Option<u32> {
36    let values = color2srgbvalues(arena, token)?;
37    Some(hsl_token(arena, srgb2hsl(&values)))
38}
39
40fn srgb2hsl(values: &[f64]) -> Vec<f64> {
41    srgb2hslvalues(at(values, 0), at(values, 1), at(values, 2), opt(values, 3))
42}
43
44fn hsl_token(arena: &AstArena, mut values: Vec<f64>) -> u32 {
45    if values.get(1) == Some(&0.0) {
46        values[0] = 0.0;
47    }
48    if matches!(values.get(2), Some(&v) if v == 1.0 || v == 0.0) {
49        values[0] = 0.0;
50        values[1] = 0.0;
51    }
52    let mut chi = vec![number(arena, at(&values, 0)), percentage(arena, at(&values, 1)), percentage(arena, at(&values, 2))];
53    if values.len() == 4 && values[3] != 1.0 {
54        chi.push(slash(arena));
55        chi.push(percentage(arena, values[3] * 100.0));
56    }
57    arena.create_color("hsl", ColorType::HSL, Some(&chi))
58}
59
60pub fn rgb2hslvalues(arena: &AstArena, token: u32) -> Option<Vec<f64>> {
61    let chi = get_color_components(arena, token)?;
62    if chi.len() < 3 {
63        return None;
64    }
65    let r = get_number(arena, chi[0]);
66    let g = get_number(arena, chi[1]);
67    let b = get_number(arena, chi[2]);
68    let a = if chi.len() == 4 { Some(get_number(arena, chi[3])) } else { None };
69    let a = a.filter(|&a| a != 1.0);
70    Some(rgbvalues2hslvalues(r, g, b, a))
71}
72
73pub fn cmyk2hslvalues(arena: &AstArena, token: u32) -> Option<Vec<f64>> {
74    let values = cmyk2rgbvalues(arena, token)?;
75    Some(rgbvalues2hslvalues(at(&values, 0), at(&values, 1), at(&values, 2), opt(&values, 3)))
76}
77
78pub fn hwb2hslvalues(arena: &AstArena, token: u32) -> Option<Vec<f64>> {
79    let v = hwbvalues(arena, token)?;
80    let w_norm = v.w / 100.0;
81    let b_norm = v.b / 100.0;
82    let hue = ((v.h % 360.0) + 360.0) % 360.0;
83    if w_norm + b_norm >= 1.0 {
84        let gray_lightness = w_norm / (w_norm + b_norm);
85        return Some(vec![hue, 0.0, gray_lightness * 100.0]);
86    }
87    let lightness = (1.0 - b_norm + w_norm) / 2.0;
88    let mut saturation = 0.0;
89    if lightness > 0.0 && lightness < 1.0 {
90        let chroma = 1.0 - w_norm - b_norm;
91        saturation = chroma / (1.0 - (2.0 * lightness - 1.0).abs());
92    }
93    let mut hsl = vec![hue, saturation * 100.0, lightness * 100.0];
94    if let Some(a) = v.a.filter(|&a| a != 1.0) {
95        hsl.push(a);
96    }
97    Some(hsl)
98}
99
100pub fn lab2hslvalues(arena: &AstArena, token: u32) -> Option<Vec<f64>> {
101    let values = lab2rgbvalues(arena, token)?;
102    Some(rgbvalues2hslvalues(at(&values, 0), at(&values, 1), at(&values, 2), opt(&values, 3)))
103}
104
105pub fn lch2hslvalues(arena: &AstArena, token: u32) -> Option<Vec<f64>> {
106    let values = lch2rgbvalues(arena, token)?;
107    Some(rgbvalues2hslvalues(at(&values, 0), at(&values, 1), at(&values, 2), opt(&values, 3)))
108}
109
110pub fn oklab2hslvalues(arena: &AstArena, token: u32) -> Option<Vec<f64>> {
111    Some(srgb2hsl(&oklab2srgbvalues(arena, token)?))
112}
113
114pub fn oklch2hslvalues(arena: &AstArena, token: u32) -> Option<Vec<f64>> {
115    Some(srgb2hsl(&oklch2srgbvalues(arena, token)?))
116}
117
118pub fn rgbvalues2hslvalues(r: f64, g: f64, b: f64, a: Option<f64>) -> Vec<f64> {
119    srgb2hslvalues(r / 255.0, g / 255.0, b / 255.0, a)
120}
121
122pub fn srgb2hslvalues(r: f64, g: f64, b: f64, a: Option<f64>) -> Vec<f64> {
123    let max = js_max(&[r, g, b]);
124    let min = js_min(&[r, g, b]);
125    let delta = max - min;
126    let l = (max + min) / 2.0;
127    let mut h = 0.0;
128    let mut s = 0.0;
129    if delta != 0.0 {
130        s = delta / (1.0 - (2.0 * l - 1.0).abs());
131        if max == r {
132            h = 60.0 * (((g - b) / delta) % 6.0);
133        } else if max == g {
134            h = 60.0 * ((b - r) / delta + 2.0);
135        } else if max == b {
136            h = 60.0 * ((r - g) / delta + 4.0);
137        }
138    }
139    h = ((h % 360.0) + 360.0) % 360.0;
140    let mut hsl = vec![h, s * 100.0, l * 100.0];
141    if let Some(a) = a.filter(|&a| a < 1.0) {
142        hsl.push(a);
143    }
144    hsl
145}