1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/// checking if a color can be parsed by chroma-rust
///
/// you can use `chroma_rust::valid` to try if a color argument can be correctly parsed as color by `chroma_rust`.
///
/// ```rust
/// chroma_rust::valid("red");
/// chroma_rust::valid("bread");
/// chroma_rust::valid("#F0000D");
/// chroma_rust::valid("#FOOOOD");
/// ```
pub fn valid(str: &str) -> bool {
    let valid = match str {
        str if str.starts_with("#") => {
            if str.len() == 4 || str.len() == 7 {
                str.chars().skip(1).all(|c| c.is_ascii_hexdigit())
            } else {
                false
            }
        }
        str if str.starts_with("rgba") => true,
        str if str.starts_with("rgb") => true,
        str if str.starts_with("lab") => true,
        str if str.starts_with("hsl") => true,
        _ => match crate::W3CX11.get(str) {
            Some(_) => true,
            None => false,
        },
    };
    valid
}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn test_valid_hex() {
        assert!(valid("#abcdef"));
    }

    #[test]
    fn test_valid_hex_short() {
        assert!(valid("#abc"));
    }

    #[test]
    fn test_valid_hex_invalid() {
        assert!(!valid("#FOOOOD"));
    }

    #[test]
    fn test_valid_hex_invalid_length() {
        assert!(!valid("#abcde"));
    }

    #[test]
    fn test_valid_rgb() {
        assert!(valid("rgb(255, 255, 255)"));
    }

    #[test]
    fn test_valid_rgba() {
        assert!(valid("rgba(255, 255, 255, 0.6)"));
    }

    #[test]
    fn test_valid_lab() {
        assert!(valid("lab(100, 0, 0)"));
    }

    #[test]
    fn test_valid_name() {
        assert!(valid("mediumspringgreen"));
    }

    #[test]
    fn test_invalid() {
        assert!(!valid("invalid"));
    }
}