diff --git a/crates/color/src/lib.rs b/crates/color/src/lib.rs index ba50e9ae..fe02a975 100644 --- a/crates/color/src/lib.rs +++ b/crates/color/src/lib.rs @@ -35,7 +35,7 @@ impl Rgba { /// `RRGGBB` or `#RRGGBB` (also `RRGGBBAA`). pub fn from_hex(s: &str) -> Option { let s = s.trim().trim_start_matches('#'); - if !s.is_ascii() { + if !s.bytes().all(|b| b.is_ascii_hexdigit()) { return None; } let byte = |i: usize| s.get(i..i + 2).and_then(|h| u8::from_str_radix(h, 16).ok()); @@ -519,6 +519,14 @@ pub fn preset(name: &str) -> Option { mod tests { use super::*; + #[test] + fn from_hex_rejects_signs() { + assert_eq!(Rgba::from_hex("+12345"), None); + assert_eq!(Rgba::from_hex("#+1+2+3"), None); + assert_eq!(Rgba::from_hex("+1+2+3+4"), None); + assert_eq!(Rgba::from_hex("+12"), None); + } + #[test] fn hex_roundtrip() { assert_eq!(Rgba::from_hex("#156082"), Some(Rgba::rgb(0x15, 0x60, 0x82)));