paint.net palette support

This commit is contained in:
Max Bradbury 2021-05-16 18:54:23 +01:00
parent 2eda76729a
commit e881aeeaf7
2 changed files with 61 additions and 0 deletions

View File

@ -33,6 +33,7 @@ impl Palette {
match path.extension().unwrap().to_str().unwrap() { match path.extension().unwrap().to_str().unwrap() {
"pal" => Self::from_jasc(path), "pal" => Self::from_jasc(path),
"gpl" => Self::from_gpl(path), "gpl" => Self::from_gpl(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(),
&read_to_string(&path).unwrap() &read_to_string(&path).unwrap()
@ -102,6 +103,34 @@ impl Palette {
// todo re-insert original comments // todo re-insert original comments
format!("GIMP Palette\r\n{}\r\n", colours.join("\r\n")) 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<String> = 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 /// for toml purposes
@ -179,6 +208,22 @@ mod test {
assert_eq!(output, expected); 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] #[test]
fn palette_from_toml() { fn palette_from_toml() {
let output = Palette::from( let output = Palette::from(

View File

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