feat: variable format impplemented
This commit is contained in:
@@ -26,16 +26,18 @@ pub struct Color(RGB);
|
|||||||
|
|
||||||
impl Color {
|
impl Color {
|
||||||
pub fn try_parse(input: String) -> Result<Color, ()> {
|
pub fn try_parse(input: String) -> Result<Color, ()> {
|
||||||
let input = input.replace(" ", "").to_uppercase();
|
let input = input.replace(" ", "").to_lowercase();
|
||||||
|
|
||||||
|
// TODO: clean all of this
|
||||||
|
// - Move down to custom trait that returns a Result
|
||||||
let hex_regex = Regex::new(r".*(#[a-fA-F0-9]{3,6}).*").unwrap();
|
let hex_regex = Regex::new(r".*(#[a-fA-F0-9]{3,6}).*").unwrap();
|
||||||
let rgb_regex =
|
let rgb_regex =
|
||||||
Regex::new(r"(rgb\([ ]*[0-9]+[ ]*,[ ]*[0-9]+[ ]*,[ ]*[0-9 ]+[ ]*\)).*").unwrap();
|
Regex::new(r".*(rgb\([ ]*[0-9]+[ ]*,[ ]*[0-9]+[ ]*,[ ]*[0-9 ]+[ ]*\)).*").unwrap();
|
||||||
let hsl_regex =
|
let hsl_regex =
|
||||||
Regex::new(r"(hsl\([ ]*[0-9]+[ ]*,[0-9]+[ ]*[%]*[ ]*,[0-9 ]+[ ]*[%]*[ ]*\)).*")
|
Regex::new(r".*(hsl\([ ]*[0-9]+[ ]*,[0-9]+[ ]*[%]*[ ]*,[0-9 ]+[ ]*[%]*[ ]*\)).*")
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let hsv_regex =
|
let hsv_regex =
|
||||||
Regex::new(r"(hsv\([ ]*[0-9]+[ ]*,[0-9]+[ ]*[%]*[ ]*,[0-9 ]+[ ]*[%]*[ ]*\)).*")
|
Regex::new(r".*(hsv\([ ]*[0-9]+[ ]*,[0-9]+[ ]*[%]*[ ]*,[0-9 ]+[ ]*[%]*[ ]*\)).*")
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let hex_result = hex_regex.captures(&input);
|
let hex_result = hex_regex.captures(&input);
|
||||||
|
|||||||
@@ -251,4 +251,27 @@ pub mod tests {
|
|||||||
let color = "hsv(157,29, 37 )";
|
let color = "hsv(157,29, 37 )";
|
||||||
assert_eq!(HSV::from(color.to_string()), HSV::new(157, 29, 37));
|
assert_eq!(HSV::from(color.to_string()), HSV::new(157, 29, 37));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_color_string_parse() {
|
||||||
|
let input = "test1hsv(255 , 83, 3)test2";
|
||||||
|
let result = Color::try_parse(input.to_string());
|
||||||
|
assert_eq!(result, Ok(Color::from(HSV::from(input.to_string()))));
|
||||||
|
|
||||||
|
let input = "test1hsj(255 , 83, 3)test2";
|
||||||
|
let result = Color::try_parse(input.to_string());
|
||||||
|
assert_eq!(result, Err(()));
|
||||||
|
|
||||||
|
let input = "test1hsl(25a , 83, 3)test2";
|
||||||
|
let result = Color::try_parse(input.to_string());
|
||||||
|
assert_eq!(result, Err(()));
|
||||||
|
|
||||||
|
let input = "#afj";
|
||||||
|
let result = Color::try_parse(input.to_string());
|
||||||
|
assert_eq!(result, Err(()));
|
||||||
|
|
||||||
|
let input = "p#test2#010203j";
|
||||||
|
let result = Color::try_parse(input.to_string());
|
||||||
|
assert_eq!(result, Ok(Color::from(RGB::new(1, 2, 3))));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user