swc_css_compat/compiler/
color_hex_alpha.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use swc_common::DUMMY_SP;
use swc_css_ast::{
    AbsoluteColorBase, AlphaValue, Color, ComponentValue, Delimiter, DelimiterValue, Function,
    FunctionName, Ident, Number,
};
use swc_css_utils::{hex_to_rgba, round_alpha};

use crate::compiler::Compiler;

fn shorten_hex_color(value: &str) -> Option<&str> {
    let length = value.len();
    let chars = value.as_bytes();

    if length == 8 && chars[6] == b'f' && chars[7] == b'f' {
        return Some(&value[0..6]);
    } else if length == 4 && chars[3] == b'f' {
        return Some(&value[0..3]);
    }

    None
}

impl Compiler {
    pub(crate) fn process_color_hex_alpha(&mut self, n: &mut ComponentValue) {
        if let Some(hex_color) = n
            .as_mut_color()
            .and_then(|color| color.as_mut_absolute_color_base())
            .and_then(|color| color.as_mut_hex_color())
        {
            hex_color.value = hex_color.value.to_ascii_lowercase();
            hex_color.raw = None;

            if hex_color.value.len() != 4 && hex_color.value.len() != 8 {
                return;
            }

            if let Some(shortened) = shorten_hex_color(&hex_color.value) {
                hex_color.value = shortened.into();
                hex_color.raw = None;

                return;
            }

            let rgba = hex_to_rgba(&hex_color.value);

            *n = ComponentValue::Color(Box::new(Color::AbsoluteColorBase(
                AbsoluteColorBase::Function(Function {
                    span: hex_color.span,
                    name: FunctionName::Ident(Ident {
                        span: DUMMY_SP,
                        value: "rgba".into(),
                        raw: None,
                    }),
                    value: vec![
                        ComponentValue::Number(Box::new(Number {
                            span: DUMMY_SP,
                            value: rgba.0 as f64,
                            raw: None,
                        })),
                        ComponentValue::Delimiter(Box::new(Delimiter {
                            span: DUMMY_SP,
                            value: DelimiterValue::Comma,
                        })),
                        ComponentValue::Number(Box::new(Number {
                            span: DUMMY_SP,
                            value: rgba.1 as f64,
                            raw: None,
                        })),
                        ComponentValue::Delimiter(Box::new(Delimiter {
                            span: DUMMY_SP,
                            value: DelimiterValue::Comma,
                        })),
                        ComponentValue::Number(Box::new(Number {
                            span: DUMMY_SP,
                            value: rgba.2 as f64,
                            raw: None,
                        })),
                        ComponentValue::Delimiter(Box::new(Delimiter {
                            span: DUMMY_SP,
                            value: DelimiterValue::Comma,
                        })),
                        ComponentValue::AlphaValue(Box::new(AlphaValue::Number(Number {
                            span: DUMMY_SP,
                            value: round_alpha(rgba.3),
                            raw: None,
                        }))),
                    ],
                }),
            )));
        }
    }
}