GPL palette support; dedupe mock palette

This commit is contained in:
Max Bradbury 2021-05-16 15:53:26 +01:00
parent 422c9e0d80
commit 4fb8ddf7ea
4 changed files with 88 additions and 17 deletions

View File

@ -12,6 +12,9 @@
* replace toml with .hex format? * replace toml with .hex format?
* what else is available for palettes? * what else is available for palettes?
* ~JASC .pal (yes)~
* Photoshop ASE (nah - seems rather proprietary)
* ~Gimp GPL (yes!)~
### parser ### parser

View File

@ -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 }
]
}
}
} }

View File

@ -56,6 +56,39 @@ impl Palette {
format!("JASC-PAL\r\n0100\r\n{}\r\n{}\r\n", self.colours.len(), colours.join("\r\n")) 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<String> = 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 /// for toml purposes
@ -111,27 +144,28 @@ mod test {
#[test] #[test]
fn palette_to_jasc() { fn palette_to_jasc() {
let output = Palette { let output = crate::mock::palette::soup11().to_jasc();
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 expected = include_str!("test-resources/basic/palettes/soup11.pal"); let expected = include_str!("test-resources/basic/palettes/soup11.pal");
assert_eq!(output, expected); 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] #[test]
fn palette_from_toml() { fn palette_from_toml() {
let output = Palette::from( let output = Palette::from(

View File

@ -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