diff --git a/src/palette.rs b/src/palette.rs index d531f9d..5ff1a7c 100644 --- a/src/palette.rs +++ b/src/palette.rs @@ -31,8 +31,9 @@ impl Palette { /// todo result pub fn from_file(path: PathBuf) -> Self { match path.extension().unwrap().to_str().unwrap() { - "pal" => Self::from_jasc(path), "gpl" => Self::from_gpl(path), + "hex" => Self::from_hex(path), + "pal" => Self::from_jasc(path), "txt" => Self::from_txt(path), "toml" => Self::from( path.file_stem().as_ref().unwrap().to_str().unwrap(), @@ -132,6 +133,26 @@ impl Palette { 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 = self.colours.iter().map(|colour| + colour.to_string().replace('#', "") + ).collect(); + + format!("{}\r\n", colours.join("\r\n")) + } } /// for toml purposes @@ -225,6 +246,21 @@ mod test { 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] fn palette_from_toml() { let output = Palette::from( diff --git a/src/test-resources/basic/palettes/soup11.hex b/src/test-resources/basic/palettes/soup11.hex new file mode 100644 index 0000000..552f895 --- /dev/null +++ b/src/test-resources/basic/palettes/soup11.hex @@ -0,0 +1,11 @@ +4f1e45 +963057 +d74459 +eb7060 +ffb383 +ffffff +7fe3bb +5cbbc4 +457ea3 +384276 +322451