Skip to content

Commit b23cf50

Browse files
authoredDec 20, 2022
feat(css/compat): Support hwb color (#6687)
1 parent 417278f commit b23cf50

File tree

12 files changed

+394
-183
lines changed

12 files changed

+394
-183
lines changed
 

‎crates/swc_css_compat/src/compiler/color_hex_alpha.rs

+2-40
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,9 @@ use swc_css_ast::{
44
AbsoluteColorBase, AlphaValue, Color, ComponentValue, Delimiter, DelimiterValue, Function,
55
Ident, Number,
66
};
7+
use swc_css_utils::{hex_to_rgba, round_alpha};
78

8-
use crate::compiler::{utils::round_alpha, Compiler};
9-
10-
#[inline]
11-
fn from_hex(c: u8) -> u8 {
12-
match c {
13-
b'0'..=b'9' => c - b'0',
14-
b'a'..=b'f' => c - b'a' + 10,
15-
b'A'..=b'F' => c - b'A' + 10,
16-
_ => {
17-
unreachable!();
18-
}
19-
}
20-
}
9+
use crate::compiler::Compiler;
2110

2211
fn shorten_hex_color(value: &str) -> Option<&str> {
2312
let length = value.len();
@@ -32,33 +21,6 @@ fn shorten_hex_color(value: &str) -> Option<&str> {
3221
None
3322
}
3423

35-
fn hex_to_rgba(hex: &str) -> (u8, u8, u8, f64) {
36-
let hex = hex.as_bytes();
37-
38-
match hex.len() {
39-
8 => {
40-
let r = from_hex(hex[0]) * 16 + from_hex(hex[1]);
41-
let g = from_hex(hex[2]) * 16 + from_hex(hex[3]);
42-
let b = from_hex(hex[4]) * 16 + from_hex(hex[5]);
43-
let a = (from_hex(hex[6]) * 16 + from_hex(hex[7])) as f64 / 255.0;
44-
45-
(r, g, b, a)
46-
}
47-
4 => {
48-
let r = from_hex(hex[0]) * 17;
49-
let g = from_hex(hex[1]) * 17;
50-
let b = from_hex(hex[2]) * 17;
51-
let a = (from_hex(hex[3]) * 17) as f64 / 255.0;
52-
53-
(r, g, b, a)
54-
}
55-
56-
_ => {
57-
unreachable!()
58-
}
59-
}
60-
}
61-
6224
impl Compiler {
6325
pub(crate) fn process_color_hex_alpha(&mut self, n: &mut ComponentValue) {
6426
if let ComponentValue::Color(box Color::AbsoluteColorBase(AbsoluteColorBase::HexColor(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
use swc_atoms::js_word;
2+
use swc_css_ast::{
3+
AbsoluteColorBase, AlphaValue, Angle, ComponentValue, Delimiter, DelimiterValue, Hue, Ident,
4+
Number, Percentage,
5+
};
6+
use swc_css_utils::{angle_to_deg, hwb_to_rgb, to_rgb255};
7+
8+
use crate::compiler::Compiler;
9+
10+
impl Compiler {
11+
fn get_hue(&self, hue: Option<&ComponentValue>) -> Option<f64> {
12+
match hue {
13+
Some(ComponentValue::Hue(box hue)) => {
14+
let mut value = match hue {
15+
Hue::Number(Number { value, .. }) => *value,
16+
Hue::Angle(Angle {
17+
value: Number { value, .. },
18+
unit: Ident { value: unit, .. },
19+
..
20+
}) => angle_to_deg(*value, unit),
21+
};
22+
23+
value %= 360.0;
24+
25+
if value < 0.0 {
26+
value += 360.0;
27+
}
28+
29+
Some(value)
30+
}
31+
Some(ComponentValue::Ident(box Ident { value, .. }))
32+
if value.eq_ignore_ascii_case(&js_word!("none")) =>
33+
{
34+
Some(0.0)
35+
}
36+
_ => None,
37+
}
38+
}
39+
40+
fn get_percentage(&self, percentage: Option<&ComponentValue>) -> Option<f64> {
41+
match percentage {
42+
Some(ComponentValue::Percentage(box Percentage {
43+
value: Number { value, .. },
44+
..
45+
})) => {
46+
if *value > 100.0 {
47+
return Some(1.0);
48+
} else if *value < 0.0 {
49+
return Some(0.0);
50+
}
51+
52+
Some(*value / 100.0)
53+
}
54+
Some(ComponentValue::Ident(box Ident { value, .. }))
55+
if value.eq_ignore_ascii_case(&js_word!("none")) =>
56+
{
57+
Some(0.0)
58+
}
59+
_ => None,
60+
}
61+
}
62+
63+
fn get_alpha_value(&self, alpha_value: Option<&ComponentValue>) -> Option<f64> {
64+
match alpha_value {
65+
Some(ComponentValue::AlphaValue(box AlphaValue::Number(Number { value, .. }))) => {
66+
if *value > 1.0 {
67+
return Some(1.0);
68+
} else if *value < 0.0 {
69+
return Some(0.0);
70+
}
71+
72+
Some(*value)
73+
}
74+
Some(ComponentValue::AlphaValue(box AlphaValue::Percentage(Percentage {
75+
value: Number { value, .. },
76+
..
77+
}))) => {
78+
if *value > 100.0 {
79+
return Some(1.0);
80+
} else if *value < 0.0 {
81+
return Some(0.0);
82+
}
83+
84+
Some(*value / 100.0)
85+
}
86+
Some(ComponentValue::Ident(box Ident { value, .. }))
87+
if value.eq_ignore_ascii_case(&js_word!("none")) =>
88+
{
89+
Some(0.0)
90+
}
91+
None => Some(1.0),
92+
_ => None,
93+
}
94+
}
95+
96+
pub(crate) fn process_color_hwb(&mut self, n: &mut AbsoluteColorBase) {
97+
if let AbsoluteColorBase::Function(function) = n {
98+
if function.name.value != js_word!("hwb") {
99+
return;
100+
}
101+
102+
let h = match self.get_hue(function.value.get(0)) {
103+
Some(value) => value,
104+
_ => return,
105+
};
106+
let w = match self.get_percentage(function.value.get(1)) {
107+
Some(value) => value,
108+
_ => return,
109+
};
110+
let b = match self.get_percentage(function.value.get(2)) {
111+
Some(value) => value,
112+
_ => return,
113+
};
114+
let a = match self.get_alpha_value(function.value.get(4)) {
115+
Some(value) => value,
116+
_ => return,
117+
};
118+
119+
let rgb = to_rgb255(hwb_to_rgb([h, w, b]));
120+
121+
if a == 1.0 {
122+
*n = AbsoluteColorBase::Function(swc_css_ast::Function {
123+
name: Ident {
124+
value: js_word!("rgb"),
125+
span: Default::default(),
126+
raw: None,
127+
},
128+
value: vec![
129+
ComponentValue::Number(Box::new(Number {
130+
value: rgb[0].round(),
131+
span: Default::default(),
132+
raw: None,
133+
})),
134+
ComponentValue::Delimiter(box Delimiter {
135+
value: DelimiterValue::Comma,
136+
span: Default::default(),
137+
}),
138+
ComponentValue::Number(Box::new(Number {
139+
value: rgb[1].round(),
140+
span: Default::default(),
141+
raw: None,
142+
})),
143+
ComponentValue::Delimiter(box Delimiter {
144+
value: DelimiterValue::Comma,
145+
span: Default::default(),
146+
}),
147+
ComponentValue::Number(Box::new(Number {
148+
value: rgb[2].round(),
149+
span: Default::default(),
150+
raw: None,
151+
})),
152+
],
153+
span: Default::default(),
154+
});
155+
} else {
156+
*n = AbsoluteColorBase::Function(swc_css_ast::Function {
157+
name: Ident {
158+
value: js_word!("rgba"),
159+
span: Default::default(),
160+
raw: None,
161+
},
162+
value: vec![
163+
ComponentValue::Number(Box::new(Number {
164+
value: rgb[0].round(),
165+
span: Default::default(),
166+
raw: None,
167+
})),
168+
ComponentValue::Delimiter(box Delimiter {
169+
value: DelimiterValue::Comma,
170+
span: Default::default(),
171+
}),
172+
ComponentValue::Number(Box::new(Number {
173+
value: rgb[1].round(),
174+
span: Default::default(),
175+
raw: None,
176+
})),
177+
ComponentValue::Delimiter(box Delimiter {
178+
value: DelimiterValue::Comma,
179+
span: Default::default(),
180+
}),
181+
ComponentValue::Number(Box::new(Number {
182+
value: rgb[2].round(),
183+
span: Default::default(),
184+
raw: None,
185+
})),
186+
ComponentValue::Delimiter(box Delimiter {
187+
value: DelimiterValue::Comma,
188+
span: Default::default(),
189+
}),
190+
ComponentValue::AlphaValue(box AlphaValue::Number(Number {
191+
value: a,
192+
span: Default::default(),
193+
raw: None,
194+
})),
195+
],
196+
span: Default::default(),
197+
});
198+
}
199+
}
200+
}
201+
}

‎crates/swc_css_compat/src/compiler/legacy_rgb_and_hsl.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ use std::f64::consts::PI;
22

33
use swc_atoms::js_word;
44
use swc_css_ast::{AbsoluteColorBase, AlphaValue, Angle, ComponentValue, Hue, Number, Percentage};
5+
use swc_css_utils::{clamp_unit_f64, round_alpha};
56

6-
use crate::compiler::{
7-
utils::{clamp_unit_f32, round_alpha},
8-
Compiler,
9-
};
7+
use crate::compiler::Compiler;
108

119
impl Compiler {
1210
pub(crate) fn process_rgb_and_hsl(&mut self, n: &mut AbsoluteColorBase) {
@@ -28,7 +26,7 @@ impl Compiler {
2826
..
2927
}) => ComponentValue::Number(Box::new(Number {
3028
span,
31-
value: clamp_unit_f32(value / 100.0) as f64,
29+
value: clamp_unit_f64(value / 100.0) as f64,
3230
raw: None,
3331
})),
3432
_ => n,

‎crates/swc_css_compat/src/compiler/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::feature::Features;
1111

1212
mod color_alpha_parameter;
1313
mod color_hex_alpha;
14+
mod color_hwb;
1415
mod color_space_separated_parameters;
1516
mod custom_media;
1617
mod legacy_rgb_and_hsl;
@@ -172,5 +173,9 @@ impl VisitMut for Compiler {
172173
if process.contains(Features::COLOR_LEGACY_RGB_AND_HSL) {
173174
self.process_rgb_and_hsl(n);
174175
}
176+
177+
if process.contains(Features::COLOR_HWB) {
178+
self.process_color_hwb(n);
179+
}
175180
}
176181
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1 @@
1-
#[inline]
2-
pub(crate) fn clamp_unit_f32(val: f64) -> u8 {
3-
(val * 255.).round().max(0.).min(255.) as u8
4-
}
51

6-
#[inline]
7-
pub(crate) fn round_alpha(alpha: f64) -> f64 {
8-
let mut rounded_alpha = (alpha * 100.).round() / 100.;
9-
10-
if clamp_unit_f32(rounded_alpha) != clamp_unit_f32(alpha) {
11-
rounded_alpha = (alpha * 1000.).round() / 1000.;
12-
}
13-
14-
rounded_alpha
15-
}

‎crates/swc_css_compat/src/feature.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ bitflags! {
99
const COLOR_ALPHA_PARAMETER = 1 << 4;
1010
const COLOR_SPACE_SEPARATED_PARAMETERS = 1 << 5;
1111
const COLOR_LEGACY_RGB_AND_HSL = 1 << 6;
12-
const SELECTOR_NOT = 1 << 7;
12+
const COLOR_HWB = 1 << 7;
13+
const SELECTOR_NOT = 1 << 8;
1314
}
1415
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.test-hwb {
2+
color: hwb(194 0% 0%); /* #00c3ff */
3+
color: hwb(194 0% 0% / .5); /* #00c3ff with 50% opacity */
4+
color: hwb(0 10% 90%);
5+
color: hwb(0 20% 80%);
6+
color: hwb(0 30% 70%);
7+
color: hwb(0 40% 60%);
8+
color: hwb(0 50% 50%);
9+
color: hwb(0 60% 40%);
10+
}
11+
12+
.test-ignore {
13+
color: hwb(194, 0%, 0%, .5); /* with comma-separated values */
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.test-hwb {
2+
color: rgb(0, 195, 255);
3+
color: rgba(0, 195, 255, 0.5);
4+
color: rgb(26, 26, 26);
5+
color: rgb(51, 51, 51);
6+
color: rgb(77, 77, 77);
7+
color: rgb(102, 102, 102);
8+
color: rgb(128, 128, 128);
9+
color: rgb(153, 153, 153);
10+
}
11+
.test-ignore {
12+
color: hwb(194, 0%, 0%, .5);
13+
}

‎crates/swc_css_compat/tests/fixture.rs

+21
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,24 @@ fn test_selector_not(input: PathBuf) {
187187
})
188188
.unwrap();
189189
}
190+
191+
#[testing::fixture("tests/color-hwb/**/*.css", exclude("expect.css"))]
192+
fn test_color_hwb(input: PathBuf) {
193+
let output = input.with_extension("expect.css");
194+
195+
testing::run_test(false, |cm, _| {
196+
let fm = cm.load_file(&input).unwrap();
197+
let mut ss = parse_stylesheet(&fm);
198+
199+
ss.visit_mut_with(&mut Compiler::new(Config {
200+
process: Features::COLOR_HWB,
201+
}));
202+
203+
let s = print_stylesheet(&ss);
204+
205+
NormalizedOutput::from(s).compare_to_file(&output).unwrap();
206+
207+
Ok(())
208+
})
209+
.unwrap();
210+
}

‎crates/swc_css_minifier/src/compressor/angle.rs

+3-35
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use core::f64::consts::PI;
2-
3-
use swc_atoms::{js_word, JsWord};
1+
use swc_atoms::js_word;
42
use swc_css_ast::*;
3+
use swc_css_utils::angle_to_deg;
54

65
use super::Compressor;
76

@@ -37,12 +36,7 @@ impl Compressor {
3736
return;
3837
}
3938

40-
let from = match get_angle_type(&angle.unit.value) {
41-
Some(angel_type) => angel_type,
42-
None => return,
43-
};
44-
45-
let deg = to_deg(angle.value.value, from);
39+
let deg = angle_to_deg(angle.value.value, &angle.unit.value);
4640

4741
if deg.fract() != 0.0 {
4842
return;
@@ -64,32 +58,6 @@ impl Compressor {
6458
}
6559
}
6660

67-
pub(crate) enum AngleType {
68-
Deg,
69-
Grad,
70-
Rad,
71-
Turn,
72-
}
73-
74-
pub(crate) fn to_deg(value: f64, from: AngleType) -> f64 {
75-
match from {
76-
AngleType::Deg => value,
77-
AngleType::Grad => value * 180.0 / 200.0,
78-
AngleType::Turn => value * 360.0,
79-
AngleType::Rad => value * 180.0 / PI,
80-
}
81-
}
82-
83-
pub(crate) fn get_angle_type(unit: &JsWord) -> Option<AngleType> {
84-
match *unit {
85-
js_word!("deg") => Some(AngleType::Deg),
86-
js_word!("grad") => Some(AngleType::Grad),
87-
js_word!("rad") => Some(AngleType::Rad),
88-
js_word!("turn") => Some(AngleType::Turn),
89-
_ => None,
90-
}
91-
}
92-
9361
fn normalize_deg(mut value: f64) -> f64 {
9462
value = (value % 360.0 + 360.0) % 360.0;
9563

‎crates/swc_css_minifier/src/compressor/color.rs

+7-86
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
use swc_atoms::{js_word, JsWord};
22
use swc_common::DUMMY_SP;
33
use swc_css_ast::*;
4-
use swc_css_utils::NAMED_COLORS;
4+
use swc_css_utils::{angle_to_deg, hsl_to_rgb, hwb_to_rgb, to_rgb255, NAMED_COLORS};
55

6-
use super::{
7-
angle::{get_angle_type, to_deg},
8-
Compressor,
9-
};
6+
use super::Compressor;
107
use crate::compressor::alpha_value::compress_alpha_value;
118

129
fn compress_alpha_in_hex(value: &JsWord) -> Option<&str> {
@@ -82,62 +79,6 @@ fn get_named_color_by_hex(v: u32) -> Option<&'static str> {
8279
Some(s)
8380
}
8481

85-
fn hsl_to_rgb(hsl: [f64; 3]) -> [f64; 3] {
86-
let [h, s, l] = hsl;
87-
88-
let r;
89-
let g;
90-
let b;
91-
92-
if s == 0.0 {
93-
r = l;
94-
g = l;
95-
b = l;
96-
} else {
97-
let f = |n: f64| -> f64 {
98-
let k = (n + h / 30.0) % 12.0;
99-
let a = s * f64::min(l, 1.0 - l);
100-
101-
l - a * f64::max(-1.0, f64::min(f64::min(k - 3.0, 9.0 - k), 1.0))
102-
};
103-
104-
r = f(0.0);
105-
g = f(8.0);
106-
b = f(4.0);
107-
}
108-
109-
[r, g, b]
110-
}
111-
112-
fn hwb_to_rgb(hwb: [f64; 3]) -> [f64; 3] {
113-
let [h, w, b] = hwb;
114-
115-
if w + b >= 1.0 {
116-
let gray = w / (w + b);
117-
118-
return [gray, gray, gray];
119-
}
120-
121-
let mut rgb = hsl_to_rgb([h, 1.0, 0.5]);
122-
123-
for item in &mut rgb {
124-
*item *= 1.0 - w - b;
125-
*item += w;
126-
}
127-
128-
[rgb[0], rgb[1], rgb[2]]
129-
}
130-
131-
fn to_rgb255(abc: [f64; 3]) -> [f64; 3] {
132-
let mut abc255 = abc;
133-
134-
for item in &mut abc255 {
135-
*item *= 255.0;
136-
}
137-
138-
abc255
139-
}
140-
14182
macro_rules! make_color {
14283
($span:expr,$r:expr,$g:expr,$b:expr, $a:expr) => {{
14384
let need_alpha_value = $a != 1.0;
@@ -324,14 +265,7 @@ impl Compressor {
324265
value: Number { value, .. },
325266
unit: Ident { value: unit, .. },
326267
..
327-
}) => {
328-
let angel_type = match get_angle_type(unit) {
329-
Some(angel_type) => angel_type,
330-
_ => return None,
331-
};
332-
333-
to_deg(*value, angel_type)
334-
}
268+
}) => angle_to_deg(*value, unit),
335269
};
336270

337271
value %= 360.0;
@@ -532,32 +466,19 @@ impl Compressor {
532466
value,
533467
..
534468
})) if matches!(&*name.value, "hwb") => {
535-
let hsla: Vec<_> = value
536-
.iter()
537-
.filter(|n| {
538-
!matches!(
539-
n,
540-
ComponentValue::Delimiter(box Delimiter {
541-
value: DelimiterValue::Comma | DelimiterValue::Solidus,
542-
..
543-
})
544-
)
545-
})
546-
.collect();
547-
548-
let h = match self.get_hue(hsla.get(0)) {
469+
let h = match self.get_hue(value.get(0).as_ref()) {
549470
Some(value) => value,
550471
_ => return,
551472
};
552-
let w = match self.get_percentage(hsla.get(1)) {
473+
let w = match self.get_percentage(value.get(1).as_ref()) {
553474
Some(value) => value,
554475
_ => return,
555476
};
556-
let b = match self.get_percentage(hsla.get(2)) {
477+
let b = match self.get_percentage(value.get(2).as_ref()) {
557478
Some(value) => value,
558479
_ => return,
559480
};
560-
let a = match self.get_alpha_value(hsla.get(3)) {
481+
let a = match self.get_alpha_value(value.get(4).as_ref()) {
561482
Some(value) => value,
562483
_ => return,
563484
};

‎crates/swc_css_utils/src/lib.rs

+123-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#![deny(clippy::all)]
22

3-
use std::{borrow::Cow, char::REPLACEMENT_CHARACTER, str};
3+
use std::{borrow::Cow, char::REPLACEMENT_CHARACTER, f64::consts::PI, str};
44

55
use once_cell::sync::Lazy;
66
use serde::{Deserialize, Serialize};
7-
use swc_atoms::JsWord;
7+
use swc_atoms::{js_word, JsWord};
88
use swc_common::collections::AHashMap;
99
use swc_css_ast::*;
1010
use swc_css_visit::{VisitMut, VisitMutWith};
@@ -295,3 +295,124 @@ fn hex_escape(ascii_byte: u8, minify: bool) -> String {
295295
.to_string()
296296
}
297297
}
298+
299+
pub fn hwb_to_rgb(hwb: [f64; 3]) -> [f64; 3] {
300+
let [h, w, b] = hwb;
301+
302+
if w + b >= 1.0 {
303+
let gray = w / (w + b);
304+
305+
return [gray, gray, gray];
306+
}
307+
308+
let mut rgb = hsl_to_rgb([h, 1.0, 0.5]);
309+
310+
for item in &mut rgb {
311+
*item *= 1.0 - w - b;
312+
*item += w;
313+
}
314+
315+
[rgb[0], rgb[1], rgb[2]]
316+
}
317+
318+
pub fn hsl_to_rgb(hsl: [f64; 3]) -> [f64; 3] {
319+
let [h, s, l] = hsl;
320+
321+
let r;
322+
let g;
323+
let b;
324+
325+
if s == 0.0 {
326+
r = l;
327+
g = l;
328+
b = l;
329+
} else {
330+
let f = |n: f64| -> f64 {
331+
let k = (n + h / 30.0) % 12.0;
332+
let a = s * f64::min(l, 1.0 - l);
333+
334+
l - a * f64::max(-1.0, f64::min(f64::min(k - 3.0, 9.0 - k), 1.0))
335+
};
336+
337+
r = f(0.0);
338+
g = f(8.0);
339+
b = f(4.0);
340+
}
341+
342+
[r, g, b]
343+
}
344+
345+
pub fn to_rgb255(abc: [f64; 3]) -> [f64; 3] {
346+
let mut abc255 = abc;
347+
348+
for item in &mut abc255 {
349+
*item *= 255.0;
350+
}
351+
352+
abc255
353+
}
354+
355+
pub fn clamp_unit_f64(val: f64) -> u8 {
356+
(val * 255.).round().max(0.).min(255.) as u8
357+
}
358+
359+
pub fn round_alpha(alpha: f64) -> f64 {
360+
let mut rounded_alpha = (alpha * 100.).round() / 100.;
361+
362+
if clamp_unit_f64(rounded_alpha) != clamp_unit_f64(alpha) {
363+
rounded_alpha = (alpha * 1000.).round() / 1000.;
364+
}
365+
366+
rounded_alpha
367+
}
368+
369+
#[inline]
370+
fn from_hex(c: u8) -> u8 {
371+
match c {
372+
b'0'..=b'9' => c - b'0',
373+
b'a'..=b'f' => c - b'a' + 10,
374+
b'A'..=b'F' => c - b'A' + 10,
375+
_ => {
376+
unreachable!();
377+
}
378+
}
379+
}
380+
381+
pub fn hex_to_rgba(hex: &str) -> (u8, u8, u8, f64) {
382+
let hex = hex.as_bytes();
383+
384+
match hex.len() {
385+
8 => {
386+
let r = from_hex(hex[0]) * 16 + from_hex(hex[1]);
387+
let g = from_hex(hex[2]) * 16 + from_hex(hex[3]);
388+
let b = from_hex(hex[4]) * 16 + from_hex(hex[5]);
389+
let a = (from_hex(hex[6]) * 16 + from_hex(hex[7])) as f64 / 255.0;
390+
391+
(r, g, b, a)
392+
}
393+
4 => {
394+
let r = from_hex(hex[0]) * 17;
395+
let g = from_hex(hex[1]) * 17;
396+
let b = from_hex(hex[2]) * 17;
397+
let a = (from_hex(hex[3]) * 17) as f64 / 255.0;
398+
399+
(r, g, b, a)
400+
}
401+
402+
_ => {
403+
unreachable!()
404+
}
405+
}
406+
}
407+
408+
pub fn angle_to_deg(value: f64, from: &JsWord) -> f64 {
409+
match *from {
410+
js_word!("deg") => value,
411+
js_word!("grad") => value * 180.0 / 200.0,
412+
js_word!("turn") => value * 360.0,
413+
js_word!("rad") => value * 180.0 / PI,
414+
_ => {
415+
unreachable!("Unknown angle type: {:?}", from);
416+
}
417+
}
418+
}

1 commit comments

Comments
 (1)

github-actions[bot] commented on Dec 20, 2022

@github-actions[bot]

Benchmark

Benchmark suite Current: b23cf50 Previous: 7e6ec8d Ratio
es/full/bugs-1 302843 ns/iter (± 7280) 306880 ns/iter (± 11962) 0.99
es/full/minify/libraries/antd 1723045291 ns/iter (± 32460545) 1736122745 ns/iter (± 23055196) 0.99
es/full/minify/libraries/d3 303614755 ns/iter (± 6708953) 320277659 ns/iter (± 5863860) 0.95
es/full/minify/libraries/echarts 1298334353 ns/iter (± 14723118) 1313703528 ns/iter (± 14379375) 0.99
es/full/minify/libraries/jquery 90753124 ns/iter (± 696389) 91242207 ns/iter (± 850580) 0.99
es/full/minify/libraries/lodash 107638842 ns/iter (± 813301) 106799641 ns/iter (± 697612) 1.01
es/full/minify/libraries/moment 52715897 ns/iter (± 402240) 53161051 ns/iter (± 475067) 0.99
es/full/minify/libraries/react 18987035 ns/iter (± 118708) 19050442 ns/iter (± 104579) 1.00
es/full/minify/libraries/terser 250501635 ns/iter (± 2738023) 253151253 ns/iter (± 4460853) 0.99
es/full/minify/libraries/three 459294786 ns/iter (± 4153631) 455556129 ns/iter (± 9263250) 1.01
es/full/minify/libraries/typescript 3235582608 ns/iter (± 12152018) 3251258446 ns/iter (± 15062196) 1.00
es/full/minify/libraries/victory 710275725 ns/iter (± 9450002) 750750352 ns/iter (± 13751515) 0.95
es/full/minify/libraries/vue 132852980 ns/iter (± 1078689) 131995150 ns/iter (± 996690) 1.01
es/full/codegen/es3 27611 ns/iter (± 163) 27032 ns/iter (± 110) 1.02
es/full/codegen/es5 27639 ns/iter (± 48) 27091 ns/iter (± 44) 1.02
es/full/codegen/es2015 27574 ns/iter (± 49) 27074 ns/iter (± 50) 1.02
es/full/codegen/es2016 27602 ns/iter (± 66) 27061 ns/iter (± 36) 1.02
es/full/codegen/es2017 27587 ns/iter (± 50) 27061 ns/iter (± 41) 1.02
es/full/codegen/es2018 27599 ns/iter (± 79) 27040 ns/iter (± 48) 1.02
es/full/codegen/es2019 27608 ns/iter (± 45) 27027 ns/iter (± 69) 1.02
es/full/codegen/es2020 27581 ns/iter (± 67) 27061 ns/iter (± 66) 1.02
es/full/all/es3 163312359 ns/iter (± 3534433) 167249370 ns/iter (± 1395295) 0.98
es/full/all/es5 153121241 ns/iter (± 3391626) 159239338 ns/iter (± 3351203) 0.96
es/full/all/es2015 118042262 ns/iter (± 764743) 118837532 ns/iter (± 911566) 0.99
es/full/all/es2016 117444032 ns/iter (± 524722) 118099251 ns/iter (± 570402) 0.99
es/full/all/es2017 115980575 ns/iter (± 946575) 117552996 ns/iter (± 299977) 0.99
es/full/all/es2018 114714175 ns/iter (± 586900) 116396033 ns/iter (± 962585) 0.99
es/full/all/es2019 113764583 ns/iter (± 722106) 115200734 ns/iter (± 671037) 0.99
es/full/all/es2020 109649188 ns/iter (± 1162521) 110448849 ns/iter (± 1013588) 0.99
es/full/parser 502537 ns/iter (± 7229) 519937 ns/iter (± 8905) 0.97
es/full/base/fixer 22066 ns/iter (± 35) 21881 ns/iter (± 42) 1.01
es/full/base/resolver_and_hygiene 77715 ns/iter (± 74) 77651 ns/iter (± 36) 1.00
serialization of ast node 119 ns/iter (± 0) 118 ns/iter (± 0) 1.01
serialization of serde 124 ns/iter (± 0) 124 ns/iter (± 0) 1
css/minify/libraries/bootstrap 26827893 ns/iter (± 133781) 26928944 ns/iter (± 136213) 1.00
css/visitor/compare/clone 2085610 ns/iter (± 13202) 2081628 ns/iter (± 6845) 1.00
css/visitor/compare/visit_mut_span 2249047 ns/iter (± 4686) 2279352 ns/iter (± 9005) 0.99
css/visitor/compare/visit_mut_span_panic 2303935 ns/iter (± 7806) 2308912 ns/iter (± 4610) 1.00
css/visitor/compare/fold_span 2998789 ns/iter (± 27094) 2984699 ns/iter (± 27242) 1.00
css/visitor/compare/fold_span_panic 3118083 ns/iter (± 18227) 3117846 ns/iter (± 24334) 1.00
css/lexer/bootstrap_5_1_3 5178678 ns/iter (± 1434) 5176268 ns/iter (± 5200) 1.00
css/lexer/foundation_6_7_4 4355084 ns/iter (± 2516) 4360558 ns/iter (± 1878) 1.00
css/lexer/tailwind_3_1_1 827409 ns/iter (± 1509) 826069 ns/iter (± 512) 1.00
css/parser/bootstrap_5_1_3 20783355 ns/iter (± 69493) 20725430 ns/iter (± 102949) 1.00
css/parser/foundation_6_7_4 16534879 ns/iter (± 30123) 16504813 ns/iter (± 20909) 1.00
css/parser/tailwind_3_1_1 3202702 ns/iter (± 4041) 3186409 ns/iter (± 5001) 1.01
es/codegen/colors 333159 ns/iter (± 187221) 322813 ns/iter (± 181848) 1.03
es/codegen/large 1077868 ns/iter (± 549422) 1224204 ns/iter (± 631237) 0.88
es/codegen/with-parser/colors 46075 ns/iter (± 262) 46082 ns/iter (± 415) 1.00
es/codegen/with-parser/large 515727 ns/iter (± 1794) 515159 ns/iter (± 1543) 1.00
es/minify/libraries/antd 1505844239 ns/iter (± 26897697) 1512478031 ns/iter (± 14657650) 1.00
es/minify/libraries/d3 256456162 ns/iter (± 3365684) 261862349 ns/iter (± 4240974) 0.98
es/minify/libraries/echarts 1127607755 ns/iter (± 9941444) 1129535531 ns/iter (± 15777284) 1.00
es/minify/libraries/jquery 78529747 ns/iter (± 404657) 79159986 ns/iter (± 581416) 0.99
es/minify/libraries/lodash 94826988 ns/iter (± 884518) 95047273 ns/iter (± 954216) 1.00
es/minify/libraries/moment 46189842 ns/iter (± 259937) 45854454 ns/iter (± 314180) 1.01
es/minify/libraries/react 16740508 ns/iter (± 74006) 17020353 ns/iter (± 90805) 0.98
es/minify/libraries/terser 214891458 ns/iter (± 1272267) 214781454 ns/iter (± 2932196) 1.00
es/minify/libraries/three 384676410 ns/iter (± 8159281) 378082590 ns/iter (± 7406074) 1.02
es/minify/libraries/typescript 2733600962 ns/iter (± 24131813) 2795995484 ns/iter (± 17930626) 0.98
es/minify/libraries/victory 589260404 ns/iter (± 8779888) 619274024 ns/iter (± 20721219) 0.95
es/minify/libraries/vue 116776681 ns/iter (± 749375) 116932355 ns/iter (± 783123) 1.00
es/visitor/compare/clone 2419140 ns/iter (± 5224) 2426551 ns/iter (± 13648) 1.00
es/visitor/compare/visit_mut_span 2812898 ns/iter (± 3429) 2818497 ns/iter (± 5236) 1.00
es/visitor/compare/visit_mut_span_panic 2865075 ns/iter (± 3694) 2849323 ns/iter (± 21509) 1.01
es/visitor/compare/fold_span 3968315 ns/iter (± 12193) 3994194 ns/iter (± 9751) 0.99
es/visitor/compare/fold_span_panic 4121378 ns/iter (± 7850) 4143200 ns/iter (± 23230) 0.99
es/lexer/colors 17328 ns/iter (± 32) 17237 ns/iter (± 7) 1.01
es/lexer/angular 8213544 ns/iter (± 2665) 8173117 ns/iter (± 6867) 1.00
es/lexer/backbone 1070329 ns/iter (± 685) 1066864 ns/iter (± 567) 1.00
es/lexer/jquery 5933284 ns/iter (± 4526) 5919345 ns/iter (± 1409) 1.00
es/lexer/jquery mobile 9114890 ns/iter (± 8279) 9099157 ns/iter (± 5683) 1.00
es/lexer/mootools 4643199 ns/iter (± 2635) 4638427 ns/iter (± 1439) 1.00
es/lexer/underscore 893419 ns/iter (± 1755) 890938 ns/iter (± 239) 1.00
es/lexer/three 27607135 ns/iter (± 12496) 27659109 ns/iter (± 21675) 1.00
es/lexer/yui 5045214 ns/iter (± 3447) 5013355 ns/iter (± 2442) 1.01
es/parser/colors 30770 ns/iter (± 260) 30919 ns/iter (± 122) 1.00
es/parser/angular 15577497 ns/iter (± 100432) 15780768 ns/iter (± 222443) 0.99
es/parser/backbone 2306206 ns/iter (± 9443) 2323238 ns/iter (± 11166) 0.99
es/parser/jquery 12505208 ns/iter (± 58819) 12586492 ns/iter (± 96462) 0.99
es/parser/jquery mobile 19867086 ns/iter (± 267847) 19946941 ns/iter (± 450407) 1.00
es/parser/mootools 9589804 ns/iter (± 26451) 9631761 ns/iter (± 41050) 1.00
es/parser/underscore 1959248 ns/iter (± 11956) 1965783 ns/iter (± 10859) 1.00
es/parser/three 58831527 ns/iter (± 838893) 60391384 ns/iter (± 582911) 0.97
es/parser/yui 9607207 ns/iter (± 50324) 9603783 ns/iter (± 128671) 1.00
es/preset-env/usage/builtin_type 143908 ns/iter (± 33737) 144792 ns/iter (± 34151) 0.99
es/preset-env/usage/property 21680 ns/iter (± 70) 21443 ns/iter (± 73) 1.01
es/resolver/typescript 116727632 ns/iter (± 2629148) 122782482 ns/iter (± 2850305) 0.95
es/fixer/typescript 95921067 ns/iter (± 1149141) 97588538 ns/iter (± 1721106) 0.98
es/hygiene/typescript 187543572 ns/iter (± 1207473) 196616731 ns/iter (± 982556) 0.95
es/resolver_with_hygiene/typescript 320022745 ns/iter (± 1935664) 328600094 ns/iter (± 2114614) 0.97
es/visitor/base-perf/module_clone 76116 ns/iter (± 1021) 77152 ns/iter (± 1784) 0.99
es/visitor/base-perf/fold_empty 85562 ns/iter (± 1559) 85928 ns/iter (± 1783) 1.00
es/visitor/base-perf/fold_noop_impl_all 85879 ns/iter (± 979) 85830 ns/iter (± 1139) 1.00
es/visitor/base-perf/fold_noop_impl_vec 86588 ns/iter (± 2240) 86539 ns/iter (± 2154) 1.00
es/visitor/base-perf/boxing_boxed_clone 56 ns/iter (± 0) 58 ns/iter (± 0) 0.97
es/visitor/base-perf/boxing_unboxed_clone 60 ns/iter (± 0) 59 ns/iter (± 0) 1.02
es/visitor/base-perf/boxing_boxed 103 ns/iter (± 0) 105 ns/iter (± 0) 0.98
es/visitor/base-perf/boxing_unboxed 101 ns/iter (± 0) 102 ns/iter (± 0) 0.99
es/visitor/base-perf/visit_contains_this 3532 ns/iter (± 54) 3542 ns/iter (± 66) 1.00
es/base/parallel/resolver/typescript 5663909412 ns/iter (± 518171900) 5976636415 ns/iter (± 537032501) 0.95
es/base/parallel/hygiene/typescript 2205971525 ns/iter (± 31968284) 2258958129 ns/iter (± 28953954) 0.98
misc/visitors/time-complexity/time 5 102 ns/iter (± 0) 102 ns/iter (± 0) 1
misc/visitors/time-complexity/time 10 316 ns/iter (± 0) 325 ns/iter (± 2) 0.97
misc/visitors/time-complexity/time 15 646 ns/iter (± 0) 659 ns/iter (± 12) 0.98
misc/visitors/time-complexity/time 20 1243 ns/iter (± 14) 1241 ns/iter (± 12) 1.00
misc/visitors/time-complexity/time 40 6695 ns/iter (± 23) 6696 ns/iter (± 40) 1.00
misc/visitors/time-complexity/time 60 17142 ns/iter (± 69) 17082 ns/iter (± 27) 1.00
es/full-target/es2016 188716 ns/iter (± 788) 189005 ns/iter (± 512) 1.00
es/full-target/es2017 183349 ns/iter (± 860) 183092 ns/iter (± 628) 1.00
es/full-target/es2018 172697 ns/iter (± 761) 173274 ns/iter (± 230) 1.00
es2020_nullish_coalescing 67392 ns/iter (± 120) 67206 ns/iter (± 161) 1.00
es2020_optional_chaining 96075 ns/iter (± 409) 96888 ns/iter (± 5773) 0.99
es2022_class_properties 93696 ns/iter (± 142) 94748 ns/iter (± 134) 0.99
es2018_object_rest_spread 71659 ns/iter (± 131) 71735 ns/iter (± 81) 1.00
es2019_optional_catch_binding 61801 ns/iter (± 147) 61687 ns/iter (± 153) 1.00
es2017_async_to_generator 62113 ns/iter (± 117) 61806 ns/iter (± 119) 1.00
es2016_exponentiation 65577 ns/iter (± 241) 65584 ns/iter (± 139) 1.00
es2015_arrow 70831 ns/iter (± 167) 70690 ns/iter (± 305) 1.00
es2015_block_scoped_fn 66247 ns/iter (± 124) 66275 ns/iter (± 79) 1.00
es2015_block_scoping 149333 ns/iter (± 8940) 139164 ns/iter (± 5211) 1.07
es2015_classes 116597 ns/iter (± 385) 116252 ns/iter (± 375) 1.00
es2015_computed_props 61514 ns/iter (± 93) 61514 ns/iter (± 106) 1
es2015_destructuring 116319 ns/iter (± 237) 116025 ns/iter (± 418) 1.00
es2015_duplicate_keys 64647 ns/iter (± 79) 64529 ns/iter (± 74) 1.00
es2015_parameters 79812 ns/iter (± 237) 79865 ns/iter (± 167) 1.00
es2015_fn_name 67061 ns/iter (± 540) 66744 ns/iter (± 412) 1.00
es2015_for_of 64882 ns/iter (± 105) 64326 ns/iter (± 72) 1.01
es2015_instanceof 63795 ns/iter (± 114) 63366 ns/iter (± 103) 1.01
es2015_shorthand_property 61367 ns/iter (± 103) 61352 ns/iter (± 97) 1.00
es2015_spread 61553 ns/iter (± 109) 61374 ns/iter (± 57) 1.00
es2015_sticky_regex 62593 ns/iter (± 98) 62503 ns/iter (± 73) 1.00
es2015_typeof_symbol 62613 ns/iter (± 73) 62541 ns/iter (± 109) 1.00
es/transform/baseline/base 52372 ns/iter (± 110) 52166 ns/iter (± 98) 1.00
es/transform/baseline/common_reserved_word 62808 ns/iter (± 109) 62874 ns/iter (± 104) 1.00
es/transform/baseline/common_typescript 168695 ns/iter (± 9701) 142729 ns/iter (± 226) 1.18
es/target/es3 169559 ns/iter (± 160) 169892 ns/iter (± 320) 1.00
es/target/es2015 626662 ns/iter (± 1363) 630585 ns/iter (± 1393) 0.99
es/target/es2016 65569 ns/iter (± 153) 65549 ns/iter (± 135) 1.00
es/target/es2017 62311 ns/iter (± 120) 61954 ns/iter (± 100) 1.01
es/target/es2018 82022 ns/iter (± 85) 81723 ns/iter (± 185) 1.00
es/target/es2020 133388 ns/iter (± 480) 133852 ns/iter (± 246) 1.00
babelify-only 680562 ns/iter (± 1496) 668647 ns/iter (± 985) 1.02
parse_and_babelify_angular 43593527 ns/iter (± 912958) 42938332 ns/iter (± 520564) 1.02
parse_and_babelify_backbone 5322693 ns/iter (± 34459) 5338520 ns/iter (± 38274) 1.00
parse_and_babelify_jquery 32262250 ns/iter (± 481678) 33438636 ns/iter (± 393694) 0.96
parse_and_babelify_jquery_mobile 54604649 ns/iter (± 660305) 56860692 ns/iter (± 947843) 0.96
parse_and_babelify_mootools 33571698 ns/iter (± 530632) 34641760 ns/iter (± 468472) 0.97
parse_and_babelify_underscore 4272272 ns/iter (± 29631) 4315145 ns/iter (± 30223) 0.99
parse_and_babelify_yui 33100982 ns/iter (± 381982) 33497782 ns/iter (± 563155) 0.99
html/minify/document/css_spec 41484365 ns/iter (± 169540) 42664891 ns/iter (± 517786) 0.97
html/minify/document/github 17326646 ns/iter (± 50167) 17382943 ns/iter (± 42946) 1.00
html/minify/document/stackoverflow 15493484 ns/iter (± 24783) 15515253 ns/iter (± 38772) 1.00
html/minify/document_fragment/css_spec 39717165 ns/iter (± 119225) 40387587 ns/iter (± 303003) 0.98
html/minify/document_fragment/github 16642910 ns/iter (± 21255) 16685854 ns/iter (± 75190) 1.00
html/minify/document_fragment/stackoverflow 15018719 ns/iter (± 19253) 15173290 ns/iter (± 79434) 0.99
html/document/visitor/compare/clone 341709 ns/iter (± 1266) 340403 ns/iter (± 2755) 1.00
html/document/visitor/compare/visit_mut_span 369573 ns/iter (± 2332) 365919 ns/iter (± 1702) 1.01
html/document/visitor/compare/visit_mut_span_panic 377834 ns/iter (± 1607) 376268 ns/iter (± 1440) 1.00
html/document/visitor/compare/fold_span 408843 ns/iter (± 2537) 404620 ns/iter (± 2259) 1.01
html/document/visitor/compare/fold_span_panic 465409 ns/iter (± 3118) 461273 ns/iter (± 2510) 1.01
html/document_fragment/visitor/compare/clone 339246 ns/iter (± 2093) 337294 ns/iter (± 1480) 1.01
html/document_fragment/visitor/compare/visit_mut_span 363781 ns/iter (± 2014) 367157 ns/iter (± 2352) 0.99
html/document_fragment/visitor/compare/visit_mut_span_panic 375710 ns/iter (± 1877) 373919 ns/iter (± 2748) 1.00
html/document_fragment/visitor/compare/fold_span 405310 ns/iter (± 2832) 403060 ns/iter (± 1629) 1.01
html/document_fragment/visitor/compare/fold_span_panic 460192 ns/iter (± 1707) 462868 ns/iter (± 3212) 0.99
html/lexer/css_2021_spec 15596391 ns/iter (± 16565) 15590931 ns/iter (± 92973) 1.00
html/lexer/github_com_17_05_2022 6025020 ns/iter (± 2174) 6051707 ns/iter (± 5724) 1.00
html/lexer/stackoverflow_com_17_05_2022 5634222 ns/iter (± 4813) 5663729 ns/iter (± 1995) 0.99
html/parser/parser_document/css_2021_spec 25780436 ns/iter (± 234292) 25781087 ns/iter (± 75219) 1.00
html/parser/parser_document/github_com_17_05_2022 8818410 ns/iter (± 12172) 8780538 ns/iter (± 7586) 1.00
html/parser/parser_document/stackoverflow_com_17_05_2022 7757448 ns/iter (± 5309) 7751769 ns/iter (± 8337) 1.00
html/parser/parser_document_fragment/css_2021_spec 25535133 ns/iter (± 91383) 28111304 ns/iter (± 210206) 0.91
html/parser/parser_document_fragment/github_com_17_05_2022 8801488 ns/iter (± 9222) 9226917 ns/iter (± 10432) 0.95
html/parser/parser_document_fragment/stackoverflow_com_17_05_2022 7762645 ns/iter (± 5515) 8455241 ns/iter (± 4562) 0.92

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.