Skip to main content

rust_css_parser/syntax/color/
hwb.rs

1//! Port of `src/syntax/color/hwb.ts`.
2
3use super::color::{color2srgbvalues, get_angle, get_number};
4use super::srgb::{cmyk2srgbvalues, hslvalues, lab2srgbvalues, lch2srgbvalues, oklab2srgbvalues, oklch2srgbvalues};
5use super::tokens::{number, percentage, slash};
6use super::utils::components::get_color_components;
7use super::utils::values::{at, js_max, js_min, opt};
8use crate::ast::arena::AstArena;
9use crate::ast::types::ColorType;
10
11macro_rules! to_token {
12    ($name:ident, $values:ident) => {
13        pub fn $name(arena: &AstArena, token: u32) -> Option<u32> {
14            let values = $values(arena, token)?;
15            Some(hwb_token(arena, &values))
16        }
17    };
18}
19
20to_token!(rgb2hwb_token, rgb2hwbvalues_opt);
21to_token!(hsl2hwb_token, hsl2hwbvalues);
22to_token!(cmyk2hwb_token, cmyk2hwbvalues);
23to_token!(oklab2hwb_token, oklab2hwbvalues);
24to_token!(oklch2hwb_token, oklch2hwbvalues);
25to_token!(lab2hwb_token, lab2hwbvalues);
26to_token!(lch2hwb_token, lch2hwbvalues);
27to_token!(color2hwb_token, color2hwbvalues);
28
29pub fn hwb_token(arena: &AstArena, values: &[f64]) -> u32 {
30    let mut chi = vec![number(arena, at(values, 0)), percentage(arena, at(values, 1)), percentage(arena, at(values, 2))];
31    if values.len() == 4 {
32        chi.push(slash(arena));
33        chi.push(percentage(arena, values[3] * 100.0));
34    }
35    arena.create_color("hwb", ColorType::HWB, Some(&chi))
36}
37
38fn to_hwb(values: &[f64]) -> Vec<f64> {
39    srgb2hwb(at(values, 0), at(values, 1), at(values, 2), opt(values, 3))
40}
41
42/// typescript: `getColorComponents(...)!` (a `null` result throws)
43pub fn rgb2hwbvalues(arena: &AstArena, token: u32) -> Vec<f64> {
44    let values: Vec<f64> = get_color_components(arena, token)
45        .unwrap_or_default()
46        .iter()
47        .enumerate()
48        .map(|(index, &t)| if index == 3 { get_number(arena, t) } else { get_number(arena, t) / 255.0 })
49        .collect();
50    to_hwb(&values)
51}
52
53fn rgb2hwbvalues_opt(arena: &AstArena, token: u32) -> Option<Vec<f64>> {
54    Some(rgb2hwbvalues(arena, token))
55}
56
57pub fn cmyk2hwbvalues(arena: &AstArena, token: u32) -> Option<Vec<f64>> {
58    Some(to_hwb(&cmyk2srgbvalues(arena, token)?))
59}
60
61pub fn hsl2hwbvalues(arena: &AstArena, token: u32) -> Option<Vec<f64>> {
62    let v = hslvalues(arena, token)?;
63    Some(hslvalues2hwbvalues(v.h, v.s, v.l, v.a))
64}
65
66pub fn lab2hwbvalues(arena: &AstArena, token: u32) -> Option<Vec<f64>> {
67    Some(to_hwb(&lab2srgbvalues(arena, token)?))
68}
69
70pub fn lch2hwbvalues(arena: &AstArena, token: u32) -> Option<Vec<f64>> {
71    Some(to_hwb(&lch2srgbvalues(arena, token)?))
72}
73
74pub fn oklab2hwbvalues(arena: &AstArena, token: u32) -> Option<Vec<f64>> {
75    Some(to_hwb(&oklab2srgbvalues(arena, token)?))
76}
77
78pub fn oklch2hwbvalues(arena: &AstArena, token: u32) -> Option<Vec<f64>> {
79    Some(to_hwb(&oklch2srgbvalues(arena, token)?))
80}
81
82pub fn color2hwbvalues(arena: &AstArena, token: u32) -> Option<Vec<f64>> {
83    Some(to_hwb(&color2srgbvalues(arena, token)?))
84}
85
86/// h, w, b, a values of a hwb color
87pub struct HwbValues {
88    pub h: f64,
89    pub w: f64,
90    pub b: f64,
91    pub a: Option<f64>,
92}
93
94pub fn hwbvalues(arena: &AstArena, token: u32) -> Option<HwbValues> {
95    let components = get_color_components(arena, token)?;
96    let n = |i: usize| components.get(i).map_or(f64::NAN, |&t| get_number(arena, t));
97    let h = components.first().map_or(f64::NAN, |&t| get_angle(arena, t)) * 360.0;
98    let w = n(1) * 100.0;
99    let b = n(2) * 100.0;
100    let a = if components.len() == 4 { Some(n(3)) } else { None };
101    Some(HwbValues { h, w, b, a })
102}
103
104/// `Math.min(1, Math.max(0, v))`
105fn clamp01(v: f64) -> f64 {
106    js_min(&[1.0, js_max(&[0.0, v])])
107}
108
109pub fn srgb2hwb(r: f64, g: f64, b: f64, a: Option<f64>) -> Vec<f64> {
110    let r_clamp = clamp01(r);
111    let g_clamp = clamp01(g);
112    let b_clamp = clamp01(b);
113    let c_max = js_max(&[r_clamp, g_clamp, b_clamp]);
114    let c_min = js_min(&[r_clamp, g_clamp, b_clamp]);
115    let delta = c_max - c_min;
116    let w = c_min;
117    let bl = 1.0 - c_max;
118    let mut h = 0.0;
119    if delta != 0.0 {
120        if c_max == r_clamp {
121            h = 60.0 * (((g_clamp - b_clamp) / delta) % 6.0);
122        } else if c_max == g_clamp {
123            h = 60.0 * ((b_clamp - r_clamp) / delta + 2.0);
124        } else if c_max == b_clamp {
125            h = 60.0 * ((r_clamp - g_clamp) / delta + 4.0);
126        }
127        if h < 0.0 {
128            h += 360.0;
129        }
130    }
131    let mut result = vec![h, w * 100.0, bl * 100.0];
132    if let Some(a) = a {
133        result.push(a);
134    }
135    result
136}
137
138pub fn hslvalues2hwbvalues(h: f64, s: f64, l: f64, a: Option<f64>) -> Vec<f64> {
139    let s_norm = s / 100.0;
140    let l_norm = l / 100.0;
141    let hue = ((h % 360.0) + 360.0) % 360.0;
142    let v = l_norm + s_norm * js_min(&[l_norm, 1.0 - l_norm]);
143    let chroma = 2.0 * (v - l_norm);
144    let w = v - chroma;
145    let b = 1.0 - v;
146    let mut hwb = vec![hue, w * 100.0, b * 100.0];
147    if let Some(a) = a.filter(|&a| a != 1.0) {
148        hwb.push(a);
149    }
150    hwb
151}