2020-04-12 11:35:39 +00:00
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
|
|
|
pub struct Colour {
|
2020-04-12 16:13:08 +00:00
|
|
|
pub red: u8,
|
|
|
|
pub green: u8,
|
|
|
|
pub blue: u8,
|
2020-04-12 11:35:39 +00:00
|
|
|
}
|
2020-04-12 11:22:20 +00:00
|
|
|
|
2020-04-12 11:35:39 +00:00
|
|
|
impl From<String> for Colour {
|
|
|
|
fn from(string: String) -> Colour {
|
|
|
|
let values: Vec<&str> = string.split(',').collect();
|
2020-04-12 11:22:20 +00:00
|
|
|
|
2020-04-18 15:58:30 +00:00
|
|
|
let red: u8 = values[0].parse().unwrap_or(0);
|
2020-04-12 11:35:39 +00:00
|
|
|
let green: u8 = values[1].parse().unwrap_or(0);
|
2020-04-18 15:58:30 +00:00
|
|
|
let blue: u8 = values[2].parse().unwrap_or(0);
|
2020-04-12 11:22:20 +00:00
|
|
|
|
2020-04-12 11:35:39 +00:00
|
|
|
Colour { red, green, blue }
|
2020-04-12 11:22:20 +00:00
|
|
|
}
|
2020-04-12 11:35:39 +00:00
|
|
|
}
|
2020-04-12 11:22:20 +00:00
|
|
|
|
2020-04-12 11:35:39 +00:00
|
|
|
impl ToString for Colour {
|
|
|
|
#[inline]
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
format!("{},{},{}", self.red, self.green, self.blue)
|
2020-04-12 11:22:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use crate::colour::Colour;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_colour_from_string() {
|
|
|
|
assert_eq!(
|
|
|
|
Colour::from("0,255,0".to_string()),
|
|
|
|
Colour { red: 0, green: 255, blue: 0 }
|
|
|
|
);
|
|
|
|
}
|
2020-04-12 11:22:20 +00:00
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
#[test]
|
|
|
|
fn test_colour_to_string() {
|
|
|
|
assert_eq!(Colour { red: 22, green: 33, blue: 44, }.to_string(), "22,33,44".to_string());
|
|
|
|
}
|
2020-04-12 11:22:20 +00:00
|
|
|
}
|