implement From for Colour

This commit is contained in:
Max Bradbury 2020-04-12 11:31:18 +01:00
parent 7b941b5d99
commit b09b9dbece
1 changed files with 10 additions and 8 deletions

View File

@ -589,20 +589,22 @@ fn test_tile_to_string() {
assert_eq!(output, expected);
}
fn colour_from_string(colour: String) -> Colour {
let values: Vec<&str> = colour.split(',').collect();
impl From<String> for Colour {
fn from(string: String) -> Colour {
let values: Vec<&str> = string.split(',').collect();
let red: u8 = values[0].parse().unwrap_or(0);
let green: u8 = values[1].parse().unwrap_or(0);
let blue: u8 = values[2].parse().unwrap_or(0);
let red: u8 = values[0].parse().unwrap_or(0);
let green: u8 = values[1].parse().unwrap_or(0);
let blue: u8 = values[2].parse().unwrap_or(0);
Colour { red, green, blue }
Colour { red, green, blue }
}
}
#[test]
fn test_colour_from_string() {
assert_eq!(
colour_from_string("0,255,0".to_string()),
Colour::from("0,255,0".to_string()),
Colour { red: 0, green: 255, blue: 0 }
);
}
@ -635,7 +637,7 @@ fn palette_from_string(palette: String) -> Palette {
let colour_start_index = if name.is_some() {2} else {1};
let colours = lines[colour_start_index..].iter().map(|&line| {
colour_from_string(line.to_string())
Colour::from(line.to_string())
}).collect();
Palette { id, name, colours }