45 lines
1.1 KiB
Rust
45 lines
1.1 KiB
Rust
|
|
use serde_derive::{Serialize, Deserialize};
|
||
|
|
|
||
|
|
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
|
||
|
|
pub struct Colour {
|
||
|
|
pub red: u8,
|
||
|
|
pub green: u8,
|
||
|
|
pub blue: u8,
|
||
|
|
pub alpha: 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),
|
||
|
|
alpha: *colours.get(3).unwrap_or(&255),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn to_vec(&self) -> Vec<u8> {
|
||
|
|
vec![self.red, self.green, self.blue, self.alpha]
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[cfg(test)]
|
||
|
|
mod test {
|
||
|
|
use crate::Colour;
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn test_colour_from_intermediate() {
|
||
|
|
let output = Colour::from(vec![64, 128, 192, 255]);
|
||
|
|
let expected = Colour { red: 64, green: 128, blue: 192, alpha: 255 };
|
||
|
|
assert_eq!(output, expected);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn test_colour_to_intermediate() {
|
||
|
|
let output = Colour { red: 64, green: 128, blue: 192, alpha: 255 }.to_vec();
|
||
|
|
let expected = vec![64, 128, 192, 255];
|
||
|
|
assert_eq!(output, expected);
|
||
|
|
}
|
||
|
|
}
|