feat: reject a set of invalid inputs for Color::from_hex
This commit is contained in:
parent
530748db96
commit
c2e24a44ac
1 changed files with 3 additions and 0 deletions
|
@ -277,6 +277,9 @@ impl Color {
|
|||
/// assert_eq!(color1, color2);
|
||||
/// ```
|
||||
pub fn from_hex(hex: &str) -> Option<Self> {
|
||||
if !(hex.starts_with('#') && hex.len() == 7) {
|
||||
return None;
|
||||
}
|
||||
match [1..=2, 3..=4, 5..=6].map(|i| hex.get(i).and_then(|c| u8::from_str_radix(c, 16).ok()))
|
||||
{
|
||||
[Some(r), Some(g), Some(b)] => Some(Self::Rgb(r, g, b)),
|
||||
|
|
Loading…
Add table
Reference in a new issue