Add named text color parsing (#23)

This commit is contained in:
TheEternalShine 2022-09-06 02:24:33 +03:00 committed by GitHub
parent df3e5b12bb
commit b9b0728fd3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -532,7 +532,25 @@ fn color_from_str(s: &str) -> Option<Color> {
g: to_num(*g0)? << 4 | to_num(*g1)?, g: to_num(*g0)? << 4 | to_num(*g1)?,
b: to_num(*b0)? << 4 | to_num(*b1)?, b: to_num(*b0)? << 4 | to_num(*b1)?,
}), }),
_ => match s {
"aqua" => Some(Color::AQUA),
"black" => Some(Color::BLACK),
"blue" => Some(Color::BLUE),
"dark_aqua" => Some(Color::DARK_AQUA),
"dark_blue" => Some(Color::DARK_BLUE),
"dark_gray" => Some(Color::DARK_GRAY),
"dark_green" => Some(Color::DARK_GREEN),
"dark_purple" => Some(Color::DARK_PURPLE),
"dark_red" => Some(Color::DARK_RED),
"gold" => Some(Color::GOLD),
"gray" => Some(Color::GRAY),
"green" => Some(Color::GREEN),
"light_purple" => Some(Color::LIGHT_PURPLE),
"red" => Some(Color::RED),
"white" => Some(Color::WHITE),
"yellow" => Some(Color::YELLOW),
_ => None, _ => None,
},
} }
} }
@ -571,6 +589,8 @@ mod tests {
assert_eq!(color_from_str("#00000000"), None); assert_eq!(color_from_str("#00000000"), None);
assert_eq!(color_from_str("#000000"), Some(Color::BLACK)); assert_eq!(color_from_str("#000000"), Some(Color::BLACK));
assert_eq!(color_from_str("#"), None); assert_eq!(color_from_str("#"), None);
assert_eq!(color_from_str("red"), Some(Color::RED));
assert_eq!(color_from_str("blue"), Some(Color::BLUE));
} }
#[test] #[test]