From 4fb8ddf7ea924a2020121f82d0a7eb68866eefcf Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Sun, 16 May 2021 15:53:26 +0100 Subject: [PATCH] GPL palette support; dedupe mock palette --- TODO.md | 3 + src/mock.rs | 19 ++++++ src/palette.rs | 68 +++++++++++++++----- src/test-resources/basic/palettes/soup11.gpl | 15 +++++ 4 files changed, 88 insertions(+), 17 deletions(-) create mode 100644 src/test-resources/basic/palettes/soup11.gpl diff --git a/TODO.md b/TODO.md index ee5398a..0de584f 100644 --- a/TODO.md +++ b/TODO.md @@ -12,6 +12,9 @@ * replace toml with .hex format? * what else is available for palettes? + * ~JASC .pal (yes)~ + * Photoshop ASE (nah - seems rather proprietary) + * ~Gimp GPL (yes!)~ ### parser diff --git a/src/mock.rs b/src/mock.rs index 368e928..710746f 100644 --- a/src/mock.rs +++ b/src/mock.rs @@ -91,4 +91,23 @@ pub(crate) mod palette { ], } } + + pub(crate) fn soup11() -> Palette { + Palette { + name: "soup11".into(), + colours: vec![ + Colour { red: 79, green: 30, blue: 69 }, + Colour { red: 150, green: 48, blue: 87 }, + Colour { red: 215, green: 68, blue: 89 }, + Colour { red: 235, green: 112, blue: 96 }, + Colour { red: 255, green: 179, blue: 131 }, + Colour { red: 255, green: 255, blue: 255 }, + Colour { red: 127, green: 227, blue: 187 }, + Colour { red: 92, green: 187, blue: 196 }, + Colour { red: 69, green: 126, blue: 163 }, + Colour { red: 56, green: 66, blue: 118 }, + Colour { red: 50, green: 36, blue: 81 } + ] + } + } } diff --git a/src/palette.rs b/src/palette.rs index 0d39604..2cf3488 100644 --- a/src/palette.rs +++ b/src/palette.rs @@ -56,6 +56,39 @@ impl Palette { format!("JASC-PAL\r\n0100\r\n{}\r\n{}\r\n", self.colours.len(), colours.join("\r\n")) } + + /// GIMP .gpl format + pub fn from_gpl(path: PathBuf) -> Self { + let name = path.file_stem().unwrap().to_str().unwrap().into(); + + let mut colours = Vec::new(); + + for (index, line) in read_to_string(&path).unwrap().lines().enumerate() { + // ignore header and comments + if index > 0 && !line.starts_with("#") { + let parts = line.split_whitespace(); + colours.push(Colour::from(parts.last().unwrap())); + } + } + + Self { name, colours } + } + + /// GIMP .gpl format + pub fn to_gpl(&self) -> String { + let colours: Vec = self.colours.iter().map(|colour| { + format!( + "{}\t{}\t{}\t{}", + colour.red, + colour.green, + colour.blue, + colour.to_string().replace('#', "") + ) + }).collect(); + + // todo re-insert original comments + format!("GIMP Palette\r\n{}\r\n", colours.join("\r\n")) + } } /// for toml purposes @@ -111,27 +144,28 @@ mod test { #[test] fn palette_to_jasc() { - let output = Palette { - name: "soup11".into(), - colours: vec![ - Colour { red: 79, green: 30, blue: 69 }, - Colour { red: 150, green: 48, blue: 87 }, - Colour { red: 215, green: 68, blue: 89 }, - Colour { red: 235, green: 112, blue: 96 }, - Colour { red: 255, green: 179, blue: 131 }, - Colour { red: 255, green: 255, blue: 255 }, - Colour { red: 127, green: 227, blue: 187 }, - Colour { red: 92, green: 187, blue: 196 }, - Colour { red: 69, green: 126, blue: 163 }, - Colour { red: 56, green: 66, blue: 118 }, - Colour { red: 50, green: 36, blue: 81 } - ] - }.to_jasc(); - + let output = crate::mock::palette::soup11().to_jasc(); let expected = include_str!("test-resources/basic/palettes/soup11.pal"); assert_eq!(output, expected); } + #[test] + fn palette_from_gpl() { + let path = PathBuf::from("src/test-resources/basic/palettes/soup11.gpl"); + let output = Palette::from_gpl(path); + let expected = crate::mock::palette::soup11(); + + assert_eq!(output, expected); + } + + #[test] + fn palette_to_gpl() { + let output = crate::mock::palette::soup11().to_gpl(); + let expected = include_str!("test-resources/basic/palettes/soup11.gpl"); + + assert_eq!(output, expected); + } + #[test] fn palette_from_toml() { let output = Palette::from( diff --git a/src/test-resources/basic/palettes/soup11.gpl b/src/test-resources/basic/palettes/soup11.gpl new file mode 100644 index 0000000..cf0c246 --- /dev/null +++ b/src/test-resources/basic/palettes/soup11.gpl @@ -0,0 +1,15 @@ +GIMP Palette +#Palette Name: soup11 +#Description: reds and blues, heavy hue shifting +#Colors: 11 +79 30 69 4f1e45 +150 48 87 963057 +215 68 89 d74459 +235 112 96 eb7060 +255 179 131 ffb383 +255 255 255 ffffff +127 227 187 7fe3bb +92 187 196 5cbbc4 +69 126 163 457ea3 +56 66 118 384276 +50 36 81 322451