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
use crate::Color;
static DIGITS: &str = "0123456789abcdef";
pub fn random() -> Color {
let mut code = "#".to_string();
for _ in 0..6 {
let index = rand::random::<usize>() % DIGITS.len();
code.push(DIGITS.chars().nth(index).unwrap());
}
Color::from(code.as_str())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_random() {
for _ in 1..100 {
let color = random();
assert!(color.rgba.0.ge(&0));
assert!(color.rgba.0.le(&255));
assert!(color.rgba.1.ge(&0));
assert!(color.rgba.1.le(&255));
assert!(color.rgba.2.ge(&0));
assert!(color.rgba.2.le(&255));
assert!(color.rgba.3.eq(&1.));
}
}
}