Skip to content

Commit 08152f8

Browse files
zohnannorMartinFillon
authored andcommitted
fix: color code parsing in theme.yml
allow 1- and 3-digit color codes
1 parent 33a6626 commit 08152f8

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

src/options/config.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ fn color_from_str(s: &str) -> Option<Color> {
9191
Some(Rgb(r, g, b))
9292
},
9393
// #rgb shorthand hex color
94-
['#', r, g, b] => {
94+
['#', r, g, b] => {
9595
let Ok(r) = u8::from_str_radix(&format!("{r}{r}"), 16)
9696
else { return None };
9797
let Ok(g) = u8::from_str_radix(&format!("{g}{g}"), 16)
@@ -100,12 +100,22 @@ fn color_from_str(s: &str) -> Option<Color> {
100100
else { return None };
101101
Some(Rgb(r, g, b))
102102
},
103-
// 0-255 color code
103+
// 0-255 color code (1-3 digits)
104+
[c1] => {
105+
let Ok(c) = str::parse::<u8>(&format!("{c1}"))
106+
else { return None };
107+
Some(Fixed(c))
108+
},
104109
[c1, c2] => {
105110
let Ok(c) = str::parse::<u8>(&format!("{c1}{c2}"))
106111
else { return None };
107112
Some(Fixed(c))
108113
},
114+
[c1, c2, c3] => {
115+
let Ok(c) = str::parse::<u8>(&format!("{c1}{c2}{c3}"))
116+
else { return None };
117+
Some(Fixed(c))
118+
},
109119
// unknown format
110120
_ => None,
111121
}
@@ -662,7 +672,7 @@ mod tests {
662672

663673
#[test]
664674
fn parse_color_code_from_string() {
665-
for (s, c) in &[("10", 10), ("01", 1)] {
675+
for (s, c) in &[("118", 118), ("10", 10), ("01", 1), ("1", 1), ("001", 1)] {
666676
assert_eq!(color_from_str(s), Some(Color::Fixed(*c)));
667677
}
668678
}

0 commit comments

Comments
 (0)