Skip to content

Commit ac23a0c

Browse files
authored
Merge pull request #262 from dev-five-git/optimize-rgba-func
Optimize rgba func
2 parents ab7ddfb + c39eeee commit ac23a0c

File tree

4 files changed

+56
-8
lines changed

4 files changed

+56
-8
lines changed

libs/css/src/constant.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,9 @@ pub(super) static NUM_TRIM_RE: Lazy<Regex> =
103103
Lazy::new(|| Regex::new(r"(\d(px|em|rem|vh|vw|%|dvh|dvw)?)\s+(\d)").unwrap());
104104
pub(super) static ZERO_RE: Lazy<Regex> =
105105
Lazy::new(|| Regex::new(r"(\b|,|\(|^|\s)-?0(px|em|rem|vh|vw|%|dvh|dvw)").unwrap());
106+
107+
pub(super) static F_RGBA_RE: Lazy<Regex> =
108+
Lazy::new(|| Regex::new(r"rgba\((\d+),(\d+),(\d+),(\d*\.?\d*)\)").unwrap());
109+
110+
pub(super) static F_RGB_RE: Lazy<Regex> =
111+
Lazy::new(|| Regex::new(r"rgb\((\d+),(\d+),(\d+)\)").unwrap());

libs/css/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,14 @@ mod tests {
225225
sheet_to_classname("background", 0, Some("rgba(255,0,0,.5)"), None, None),
226226
);
227227

228+
assert_eq!(
229+
sheet_to_classname("background", 0, Some("rgba(255, 0, 0, 0.5)"), None, None),
230+
sheet_to_classname("background", 0, Some("#FF000080"), None, None),
231+
);
232+
228233
{
229234
let map = GLOBAL_CLASS_MAP.lock().unwrap();
230-
assert_eq!(map.get("background-0-rgba(255,0,0,.5)--255"), Some(&2));
235+
assert_eq!(map.get("background-0-#FF000080--255"), Some(&2));
231236
}
232237
assert_eq!(
233238
sheet_to_classname("background", 0, Some("#fff"), None, None),

libs/css/src/optimize_value.rs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use crate::{
22
COLOR_HASH, F_SPACE_RE, ZERO_RE,
33
constant::{
4-
DOT_ZERO_RE, F_DOT_RE, INNER_TRIM_RE, NUM_TRIM_RE, RM_MINUS_ZERO_RE, ZERO_PERCENT_FUNCTION,
4+
DOT_ZERO_RE, F_DOT_RE, F_RGB_RE, F_RGBA_RE, INNER_TRIM_RE, NUM_TRIM_RE, RM_MINUS_ZERO_RE,
5+
ZERO_PERCENT_FUNCTION,
56
},
67
};
78

@@ -14,6 +15,29 @@ pub fn optimize_value(value: &str) -> String {
1415
if ret.contains(",") {
1516
ret = F_SPACE_RE.replace_all(&ret, ",").trim().to_string();
1617
}
18+
ret = F_RGBA_RE
19+
.replace_all(&ret, |c: &regex::Captures| {
20+
let r = c[1].parse::<i32>().unwrap();
21+
let g = c[2].parse::<i32>().unwrap();
22+
let b = c[3].parse::<i32>().unwrap();
23+
let a = c[4].parse::<f32>().unwrap();
24+
format!(
25+
"#{:02X}{:02X}{:02X}{:02X}",
26+
r,
27+
g,
28+
b,
29+
(a * 255.0).round() as i32
30+
)
31+
})
32+
.to_string();
33+
ret = F_RGB_RE
34+
.replace_all(&ret, |c: &regex::Captures| {
35+
let r = c[1].parse::<i32>().unwrap();
36+
let g = c[2].parse::<i32>().unwrap();
37+
let b = c[3].parse::<i32>().unwrap();
38+
format!("#{:02X}{:02X}{:02X}", r, g, b)
39+
})
40+
.to_string();
1741
if ret.contains("#") {
1842
ret = COLOR_HASH
1943
.replace_all(&ret, |c: &regex::Captures| optimize_color(&c[1]))
@@ -101,7 +125,14 @@ fn optimize_color(value: &str) -> String {
101125
let ch = ret.chars().collect::<Vec<char>>();
102126
if ch[0] == ch[1] && ch[2] == ch[3] && ch[4] == ch[5] && ch[6] == ch[7] {
103127
ret = format!("{}{}{}{}", ch[0], ch[2], ch[4], ch[6]);
128+
if ret.ends_with("F") {
129+
ret = ret[..ret.len() - 1].to_string();
130+
}
131+
} else if ret.ends_with("FF") {
132+
ret = ret[..ret.len() - 2].to_string();
104133
}
134+
} else if ret.len() == 4 && ret.ends_with("F") {
135+
ret = ret[..ret.len() - 1].to_string();
105136
}
106137

107138
format!("#{ret}")
@@ -145,8 +176,14 @@ mod tests {
145176
#[case("scale(0px)", "scale(0)")]
146177
#[case("scale(-0px)", "scale(0)")]
147178
#[case("scale(-0px);", "scale(0)")]
148-
#[case("rgba(255, 0, 0, 0.5)", "rgba(255,0,0,.5)")]
149-
#[case("rgba(0.0,0.0,0.0,0.5)", "rgba(0,0,0,.5)")]
179+
#[case("rgba(255,12,12,0.5)", "#FF0C0C80")]
180+
#[case("rgba(255,12,12,.5)", "#FF0C0C80")]
181+
#[case("rgba(255,12,12,1)", "#FF0C0C")]
182+
#[case("rgba(255, 0, 0, 0.5)", "#FF000080")]
183+
#[case("rgba(255, 255, 255, 0.8 )", "#FFFC")]
184+
#[case("rgb(255,12,12)", "#FF0C0C")]
185+
#[case("rgb(255, 0, 0)", "#F00")]
186+
#[case("rgb(255, 255, 255)", "#FFF")]
150187
#[case("red;", "red")]
151188
#[case("translate(0px)", "translate(0)")]
152189
#[case("translate(-0px,0px)", "translate(0,0)")]
@@ -190,9 +227,9 @@ mod tests {
190227
#[rstest]
191228
#[case("#ff0000", "#F00")]
192229
#[case("#123456", "#123456")]
193-
#[case("#ff0000ff", "#F00F")]
230+
#[case("#ff0000ff", "#F00")]
194231
#[case("#f00", "#F00")]
195-
#[case("#f00f", "#F00F")]
232+
#[case("#f00f", "#F00")]
196233
#[case("red", "red")]
197234
#[case("blue", "blue")]
198235
#[case("transparent", "transparent")]

libs/extractor/src/snapshots/extractor__tests__extract_selector-2.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ ToBTreeSet {
1818
Static(
1919
ExtractStaticStyle {
2020
property: "boxShadow",
21-
value: "0 0 15px 0 rgba(0,0,0,.25)",
21+
value: "0 0 15px 0 #00000040",
2222
level: 2,
2323
selector: Some(
2424
Selector(
@@ -31,7 +31,7 @@ ToBTreeSet {
3131
Static(
3232
ExtractStaticStyle {
3333
property: "boxShadow",
34-
value: "0 1px 3px 0 rgba(0,0,0,.25)",
34+
value: "0 1px 3px 0 #00000040",
3535
level: 0,
3636
selector: Some(
3737
Selector(

0 commit comments

Comments
 (0)