hex file support

This commit is contained in:
Max Bradbury 2021-05-16 20:56:41 +01:00
parent effc207575
commit 27587d2032
2 changed files with 48 additions and 1 deletions

View File

@ -31,8 +31,9 @@ impl Palette {
/// todo result /// todo result
pub fn from_file(path: PathBuf) -> Self { pub fn from_file(path: PathBuf) -> Self {
match path.extension().unwrap().to_str().unwrap() { match path.extension().unwrap().to_str().unwrap() {
"pal" => Self::from_jasc(path),
"gpl" => Self::from_gpl(path), "gpl" => Self::from_gpl(path),
"hex" => Self::from_hex(path),
"pal" => Self::from_jasc(path),
"txt" => Self::from_txt(path), "txt" => Self::from_txt(path),
"toml" => Self::from( "toml" => Self::from(
path.file_stem().as_ref().unwrap().to_str().unwrap(), path.file_stem().as_ref().unwrap().to_str().unwrap(),
@ -132,6 +133,26 @@ impl Palette {
colours.join("\r\n") colours.join("\r\n")
) )
} }
/// simple file format. one hexadecimal colour per line
pub fn from_hex(path: PathBuf) -> Self {
let name = path.file_stem().unwrap().to_str().unwrap().into();
let colours = read_to_string(&path).unwrap().lines().map(|line|
Colour::from(line)
).collect();
Self { name, colours }
}
/// simple file format. one hexadecimal colour per line
pub fn to_hex(&self) -> String {
let colours: Vec<String> = self.colours.iter().map(|colour|
colour.to_string().replace('#', "")
).collect();
format!("{}\r\n", colours.join("\r\n"))
}
} }
/// for toml purposes /// for toml purposes
@ -225,6 +246,21 @@ mod test {
assert_eq!(output, expected); assert_eq!(output, expected);
} }
#[test]
fn palette_from_hex() {
let path = PathBuf::from("src/test-resources/basic/palettes/soup11.hex");
let output = Palette::from_hex(path);
let expected = crate::mock::palette::soup11();
assert_eq!(output, expected);
}
#[test]
fn palette_to_hex() {
let output = crate::mock::palette::soup11().to_hex();
let expected = include_str!("test-resources/basic/palettes/soup11.hex");
assert_eq!(output, expected);
}
#[test] #[test]
fn palette_from_toml() { fn palette_from_toml() {
let output = Palette::from( let output = Palette::from(

View File

@ -0,0 +1,11 @@
4f1e45
963057
d74459
eb7060
ffb383
ffffff
7fe3bb
5cbbc4
457ea3
384276
322451