From e881aeeaf746baed57d5a768b5142c4dcdfcf716 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Sun, 16 May 2021 18:54:23 +0100 Subject: [PATCH] paint.net palette support --- src/palette.rs | 45 ++++++++++++++++++++ src/test-resources/basic/palettes/soup11.txt | 16 +++++++ 2 files changed, 61 insertions(+) create mode 100644 src/test-resources/basic/palettes/soup11.txt diff --git a/src/palette.rs b/src/palette.rs index c4ddfb0..f8f1129 100644 --- a/src/palette.rs +++ b/src/palette.rs @@ -33,6 +33,7 @@ impl Palette { match path.extension().unwrap().to_str().unwrap() { "pal" => Self::from_jasc(path), "gpl" => Self::from_gpl(path), + "txt" => Self::from_txt(path), "toml" => Self::from( path.file_stem().as_ref().unwrap().to_str().unwrap(), &read_to_string(&path).unwrap() @@ -102,6 +103,34 @@ impl Palette { // todo re-insert original comments format!("GIMP Palette\r\n{}\r\n", colours.join("\r\n")) } + + /// Paint.net .txt format + pub fn from_txt(path: PathBuf) -> Self { + let name = path.file_stem().unwrap().to_str().unwrap().into(); + let mut colours = Vec::new(); + + for line in read_to_string(&path).unwrap().lines() { + // header and comments begin with ; + if !line.starts_with(";") { + // colour starts with FF + colours.push(Colour::from(&line[2..])); + } + } + + Self { name, colours } + } + + pub fn to_txt(&self) -> String { + let colours: Vec = self.colours.iter().map(|colour| + colour.to_string().replace('#', "FF") + ).collect(); + + format!( + ";paint.net Palette File\r\n;Colors: {}\r\n{}\r\n", + self.colours.len(), + colours.join("\r\n") + ) + } } /// for toml purposes @@ -179,6 +208,22 @@ mod test { assert_eq!(output, expected); } + #[test] + fn palette_from_txt() { + let path = PathBuf::from("src/test-resources/basic/palettes/soup11.txt"); + let output = Palette::from_txt(path); + let expected = crate::mock::palette::soup11(); + assert_eq!(output, expected); + } + + #[test] + fn palette_to_txt() { + let output = crate::mock::palette::soup11().to_txt(); + let expected = include_str!("test-resources/basic/palettes/soup11.txt"); + + assert_eq!(output, expected); + } + #[test] fn palette_from_toml() { let output = Palette::from( diff --git a/src/test-resources/basic/palettes/soup11.txt b/src/test-resources/basic/palettes/soup11.txt new file mode 100644 index 0000000..ab3065a --- /dev/null +++ b/src/test-resources/basic/palettes/soup11.txt @@ -0,0 +1,16 @@ +;paint.net Palette File +;Downloaded from Lospec.com/palette-list +;Palette Name: soup11 +;Description: reds and blues, heavy hue shifting +;Colors: 11 +FF4f1e45 +FF963057 +FFd74459 +FFeb7060 +FFffb383 +FFffffff +FF7fe3bb +FF5cbbc4 +FF457ea3 +FF384276 +FF322451