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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use std::str::FromStr;
use std::u8;
use std::iter::FromIterator;
use image::Rgba;
use error::Error;
#[derive(Debug, PartialEq, Copy, Clone)]
pub struct RgbColor(u8, u8, u8);
impl RgbColor {
pub fn new(red: u8, green: u8, blue: u8) -> RgbColor {
RgbColor(red, green, blue)
}
pub fn random() -> Self {
use rand::prelude::*;
let mut rng = thread_rng();
RgbColor(rng.gen(), rng.gen(), rng.gen())
}
pub fn find_ratio(&self, other: &RgbColor) -> f32 {
self.calculate_luminance() / other.calculate_luminance()
}
pub fn to_rgba(self, alpha: u8) -> Rgba<u8> {
Rgba {
data: [self.0, self.1, self.2, alpha]
}
}
fn calculate_luminance(&self) -> f32 {
0.299 * f32::from(self.0) +
0.587 * f32::from(self.1) +
0.114 * f32::from(self.2) +
0.05
}
}
impl FromStr for RgbColor {
type Err = Error;
fn from_str(hex: &str) -> Result<RgbColor, Error> {
if hex.is_empty() {
return Err(Error::InvalidHexFormat {
expected: String::from("Color hex code must not be empty"),
actual: String::from("Color hex was empty"),
});
}
if !hex.starts_with('#') {
return Err(Error::InvalidHexFormat {
expected: String::from("Color hex code must start with `#`"),
actual: format!("Color hex starts with `{}`", hex.chars().nth(0).unwrap())
});
}
if hex.len() != 7 {
return Err(Error::InvalidHexFormat {
expected: String::from("Hex code must be `7` characters long. Example: `#00FF00`"),
actual: format!("Hex code is `{}` characters long!", hex.len())
});
}
let raw: Vec<char> = hex.chars().collect();
Ok(RgbColor(
u8::from_str_radix(&String::from_iter(&raw[1 .. 3]), 16)?,
u8::from_str_radix(&String::from_iter(&raw[3 .. 5]), 16)?,
u8::from_str_radix(&String::from_iter(&raw[5 .. 7]), 16)?,
))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_invalid_hex_empty_str() {
let res: Result<RgbColor, Error> = "".parse();
assert!(res.is_err());
assert_eq!(
format!("{}", res.unwrap_err()),
"unexpected hex color format: expected(Color hex code must not be empty), got(Color hex was empty)"
);
}
#[test]
fn test_invalid_hex_with_missing_prefix() {
let res: Result<RgbColor, Error> = "00ff00".parse();
assert!(res.is_err());
assert_eq!(
format!("{}", res.unwrap_err()),
"unexpected hex color format: expected(Color hex code must start with `#`), got(Color hex starts with `0`)"
);
}
#[test]
fn test_invalid_hex_with_wrong_size() {
let res: Result<RgbColor, Error> = "#ff00".parse();
assert!(res.is_err());
assert_eq!(
format!("{}", res.unwrap_err()),
"unexpected hex color format: expected(Hex code must be `7` characters long. Example: `#00FF00`), got(Hex code is `5` characters long!)"
);
}
#[test]
fn test_invalid_hex_with_parse_error() {
let res: Result<RgbColor, Error> = "#0qfd00".parse();
assert!(res.is_err());
assert_eq!(format!("{}", res.unwrap_err()), "couldn't parse hex value: invalid digit found in string");
}
#[test]
fn test_valid_hex() {
let res: Result<RgbColor, Error> = "#00ff00".parse();
assert!(res.is_ok());
assert_eq!(res.unwrap(), RgbColor(0, 255, 0));
}
#[test]
fn test_contrast_ratio() {
let rgb_white = RgbColor(255, 255, 255);
let rgb_yellow = RgbColor(255, 255, 0);
assert_eq!(rgb_white.find_ratio(&rgb_yellow).floor(), 1.);
let rgb_blue = RgbColor(0, 0, 255);
assert_eq!(rgb_white.find_ratio(&rgb_blue).floor(), 8.);
}
}