2021-05-15 16:59:51 +01:00
|
|
|
use serde_derive::{Serialize, Deserialize};
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
|
|
|
|
|
pub struct Colour {
|
|
|
|
|
pub red: u8,
|
|
|
|
|
pub green: u8,
|
|
|
|
|
pub blue: u8,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Colour {
|
|
|
|
|
pub fn from(colours: Vec<u8>) -> Colour {
|
|
|
|
|
const ZERO: u8 = 0;
|
|
|
|
|
Colour {
|
|
|
|
|
red: *colours.get(0).unwrap_or(&ZERO),
|
|
|
|
|
green: *colours.get(1).unwrap_or(&ZERO),
|
|
|
|
|
blue: *colours.get(2).unwrap_or(&ZERO),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn to_vec(&self) -> Vec<u8> {
|
2021-05-15 22:38:43 +01:00
|
|
|
vec![self.red, self.green, self.blue]
|
2021-05-15 16:59:51 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod test {
|
|
|
|
|
use crate::Colour;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_colour_from_intermediate() {
|
2021-05-15 22:38:43 +01:00
|
|
|
let output = Colour::from(vec![64, 128, 192]);
|
|
|
|
|
let expected = Colour { red: 64, green: 128, blue: 192 };
|
2021-05-15 16:59:51 +01:00
|
|
|
assert_eq!(output, expected);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_colour_to_intermediate() {
|
2021-05-15 22:38:43 +01:00
|
|
|
let output = Colour { red: 64, green: 128, blue: 192 }.to_vec();
|
|
|
|
|
let expected = vec![64, 128, 192];
|
2021-05-15 16:59:51 +01:00
|
|
|
assert_eq!(output, expected);
|
|
|
|
|
}
|
|
|
|
|
}
|