2020-04-12 11:35:39 +00:00
|
|
|
use crate::colour::Colour;
|
2020-04-13 13:43:23 +00:00
|
|
|
use crate::{from_base36, ToBase36};
|
2020-04-12 11:35:39 +00:00
|
|
|
|
2020-05-31 15:12:23 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
2020-04-12 11:35:39 +00:00
|
|
|
pub struct Palette {
|
2020-04-13 12:30:08 +00:00
|
|
|
pub id: u64,
|
2020-04-12 16:13:08 +00:00
|
|
|
pub name: Option<String>,
|
|
|
|
pub colours: Vec<Colour>,
|
2020-04-12 11:35:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<String> for Palette {
|
2020-04-28 17:00:31 +00:00
|
|
|
#[inline]
|
2020-04-12 11:35:39 +00:00
|
|
|
fn from(string: String) -> Palette {
|
|
|
|
let lines: Vec<&str> = string.lines().collect();
|
|
|
|
|
2020-04-13 12:30:08 +00:00
|
|
|
let id = from_base36(&lines[0].replace("PAL ", ""));
|
2020-04-12 11:35:39 +00:00
|
|
|
|
|
|
|
let name = match lines[1].starts_with("NAME") {
|
|
|
|
true => Some(lines[1].replace("NAME ", "").to_string()),
|
|
|
|
false => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let colour_start_index = if name.is_some() { 2 } else { 1 };
|
|
|
|
|
2020-04-18 15:58:30 +00:00
|
|
|
let colours = lines[colour_start_index..]
|
|
|
|
.iter()
|
|
|
|
.map(|&line| Colour::from(line.to_string()))
|
|
|
|
.collect();
|
2020-04-12 11:35:39 +00:00
|
|
|
|
|
|
|
Palette { id, name, colours }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToString for Palette {
|
|
|
|
#[inline]
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
let name = if self.name.as_ref().is_some() {
|
|
|
|
format!("NAME {}\n", self.name.as_ref().unwrap())
|
|
|
|
} else {
|
|
|
|
"".to_string()
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut colours = String::new();
|
|
|
|
for colour in &self.colours {
|
|
|
|
colours.push_str(&format!("{}\n", colour.to_string()));
|
|
|
|
}
|
|
|
|
colours.pop();
|
|
|
|
|
2020-04-13 13:43:23 +00:00
|
|
|
format!("PAL {}\n{}{}", self.id.to_base36(), name, colours)
|
2020-04-12 11:35:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use crate::colour::Colour;
|
|
|
|
use crate::palette::Palette;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_palette_from_string() {
|
|
|
|
let output = Palette::from("PAL 1\nNAME lamplight\n45,45,59\n66,60,39\n140,94,1".to_string());
|
|
|
|
|
|
|
|
let expected = Palette {
|
|
|
|
id: 1,
|
|
|
|
name: Some("lamplight".to_string()),
|
|
|
|
colours: vec![
|
|
|
|
Colour {
|
|
|
|
red: 45,
|
|
|
|
green: 45,
|
|
|
|
blue: 59,
|
|
|
|
},
|
|
|
|
Colour {
|
|
|
|
red: 66,
|
|
|
|
green: 60,
|
|
|
|
blue: 39,
|
|
|
|
},
|
|
|
|
Colour {
|
|
|
|
red: 140,
|
|
|
|
green: 94,
|
|
|
|
blue: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
2020-04-12 11:35:39 +00:00
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
assert_eq!(output, expected);
|
|
|
|
}
|
2020-04-12 11:35:39 +00:00
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
#[test]
|
|
|
|
fn test_palette_from_string_no_name() {
|
|
|
|
let output = Palette::from("PAL 9\n45,45,59\n66,60,39\n140,94,1".to_string());
|
|
|
|
|
|
|
|
let expected = Palette {
|
|
|
|
id: 9,
|
|
|
|
name: None,
|
|
|
|
colours: vec![
|
|
|
|
Colour {
|
|
|
|
red: 45,
|
|
|
|
green: 45,
|
|
|
|
blue: 59,
|
|
|
|
},
|
|
|
|
Colour {
|
|
|
|
red: 66,
|
|
|
|
green: 60,
|
|
|
|
blue: 39,
|
|
|
|
},
|
|
|
|
Colour {
|
|
|
|
red: 140,
|
|
|
|
green: 94,
|
|
|
|
blue: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(output, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_palette_to_string() {
|
|
|
|
let output = Palette {
|
|
|
|
id: 16,
|
|
|
|
name: Some("moss".to_string()),
|
|
|
|
colours: vec![
|
|
|
|
Colour {
|
|
|
|
red: 1,
|
|
|
|
green: 2,
|
|
|
|
blue: 3,
|
|
|
|
},
|
|
|
|
Colour {
|
|
|
|
red: 255,
|
|
|
|
green: 254,
|
|
|
|
blue: 253,
|
|
|
|
},
|
|
|
|
Colour {
|
|
|
|
red: 126,
|
|
|
|
green: 127,
|
|
|
|
blue: 128,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
.to_string();
|
|
|
|
let expected = "PAL g\nNAME moss\n1,2,3\n255,254,253\n126,127,128".to_string();
|
|
|
|
assert_eq!(output, expected);
|
2020-04-18 15:58:30 +00:00
|
|
|
}
|
2020-04-12 11:35:39 +00:00
|
|
|
}
|