JASC palette support

This commit is contained in:
Max Bradbury 2021-05-16 14:00:13 +01:00
parent e929400970
commit 422c9e0d80
2 changed files with 96 additions and 10 deletions

View File

@ -1,8 +1,9 @@
use std::fs::read_to_string;
use std::path::PathBuf;
use serde_derive::{Serialize, Deserialize}; use serde_derive::{Serialize, Deserialize};
use crate::colour::Colour; use crate::colour::Colour;
use std::fs;
/// todo enumerate original format
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)] #[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct Palette { pub struct Palette {
pub name: String, pub name: String,
@ -27,11 +28,33 @@ impl Palette {
} }
} }
pub fn from_file(path: String) -> Self { /// JASC .pal format (Paint Shop Pro)
// todo get name without extension pub fn from_jasc(path: PathBuf) -> Self {
let name = "blah"; let name = path.file_stem().unwrap().to_str().unwrap().into();
let toml = fs::read_to_string(path).unwrap(); let mut colours = Vec::new();
Self::from(name, &toml)
for (index, line) in read_to_string(&path).unwrap().lines().enumerate() {
// ignore the first 3 lines
if index > 2 {
let mut values: Vec<u8> = Vec::new();
for value in line.split_whitespace() {
values.push(value.parse().unwrap());
}
colours.push(Colour::from(values));
}
}
Palette { name, colours }
}
pub fn to_jasc(&self) -> String {
let colours: Vec<String> = self.colours.iter().map(|colour|
format!("{} {} {}", colour.red, colour.green, colour.blue)
).collect();
format!("JASC-PAL\r\n0100\r\n{}\r\n{}\r\n", self.colours.len(), colours.join("\r\n"))
} }
} }
@ -58,10 +81,59 @@ impl IntermediatePalettes {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::Palette; use std::path::PathBuf;
use crate::{Colour, Palette};
#[test] #[test]
fn test_palette_from_toml() { fn palette_from_jasc() {
let path = PathBuf::from("src/test-resources/basic/palettes/soup11.pal");
let output = Palette::from_jasc(path);
let expected = 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 }
]
};
assert_eq!(output, expected);
}
#[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 expected = include_str!("test-resources/basic/palettes/soup11.pal");
assert_eq!(output, expected);
}
#[test]
fn palette_from_toml() {
let output = Palette::from( let output = Palette::from(
"blueprint", "blueprint",
include_str!("test-resources/basic/palettes/blueprint.toml") include_str!("test-resources/basic/palettes/blueprint.toml")
@ -71,7 +143,7 @@ mod test {
} }
#[test] #[test]
fn test_palette_to_toml() { fn palette_to_toml() {
let intermediate = crate::mock::palette::intermediate(); let intermediate = crate::mock::palette::intermediate();
let output = toml::to_string(&intermediate).unwrap(); let output = toml::to_string(&intermediate).unwrap();
let expected = include_str!("test-resources/basic/palettes/blueprint.toml"); let expected = include_str!("test-resources/basic/palettes/blueprint.toml");

View File

@ -0,0 +1,14 @@
JASC-PAL
0100
11
79 30 69
150 48 87
215 68 89
235 112 96
255 179 131
255 255 255
127 227 187
92 187 196
69 126 163
56 66 118
50 36 81