chroma_rust/generator/
random.rs

1use crate::Color;
2
3static DIGITS: &str = "0123456789abcdef";
4
5/// Generate a random color.
6pub fn random() -> Color {
7    let mut code = "#".to_string();
8    for _ in 0..6 {
9        let index = rand::random::<usize>() % DIGITS.len();
10        code.push(DIGITS.chars().nth(index).unwrap());
11    }
12    Color::from(code.as_str())
13}
14
15#[cfg(test)]
16mod tests {
17    use super::*;
18
19    #[test]
20    fn test_random() {
21        for _ in 1..100 {
22            let color = random();
23
24            assert!(color.rgba.0.ge(&0));
25            assert!(color.rgba.0.le(&255));
26            assert!(color.rgba.1.ge(&0));
27            assert!(color.rgba.1.le(&255));
28            assert!(color.rgba.2.ge(&0));
29            assert!(color.rgba.2.le(&255));
30            assert!(color.rgba.3.eq(&1.));
31        }
32    }
33}