Files
peachy/src/palette.rs

277 lines
8.5 KiB
Rust
Raw Normal View History

2021-05-16 14:00:13 +01:00
use std::fs::read_to_string;
use std::path::PathBuf;
use serde_derive::{Serialize, Deserialize};
use crate::colour::Colour;
2021-05-16 22:49:12 +01:00
use image::{GenericImageView, Rgba, DynamicImage, GenericImage};
2021-05-16 22:43:54 +01:00
use image::io::Reader as ImageReader;
2021-05-16 14:00:13 +01:00
/// todo enumerate original format
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct Palette {
pub name: String,
pub colours: Vec<Colour>,
}
impl Palette {
2021-05-16 17:05:08 +01:00
/// todo result
pub fn from_file(path: PathBuf) -> Self {
match path.extension().unwrap().to_str().unwrap() {
"gpl" => Self::from_gpl(path),
2021-05-16 20:56:41 +01:00
"hex" => Self::from_hex(path),
"pal" => Self::from_jasc(path),
2021-05-16 22:44:13 +01:00
"png" => Self::from_png(path),
2021-05-16 18:54:23 +01:00
"txt" => Self::from_txt(path),
2021-05-16 17:05:08 +01:00
_ => panic!("Bad palette")
}
}
2021-05-16 14:00:13 +01:00
/// JASC .pal format (Paint Shop Pro)
pub fn from_jasc(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 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 }
}
2021-05-17 13:21:12 +01:00
/// JASC .pal format (Paint Shop Pro)
2021-05-16 14:00:13 +01:00
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"))
}
/// 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"))
}
2021-05-16 18:54:23 +01:00
/// 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();
2021-05-16 18:54:54 +01:00
// todo re-insert original comments
2021-05-16 18:54:23 +01:00
format!(
";paint.net Palette File\r\n;Colors: {}\r\n{}\r\n",
self.colours.len(),
colours.join("\r\n")
)
}
2021-05-16 20:56:41 +01:00
/// simple file format. one hexadecimal colour per line
pub fn from_hex(path: PathBuf) -> Self {
let name = path.file_stem().unwrap().to_str().unwrap().into();
let colours = read_to_string(&path).unwrap().lines().map(|line|
Colour::from(line)
).collect();
Self { name, colours }
}
/// simple file format. one hexadecimal colour per line
pub fn to_hex(&self) -> String {
let colours: Vec<String> = self.colours.iter().map(|colour|
colour.to_string().replace('#', "")
).collect();
format!("{}\r\n", colours.join("\r\n"))
}
2021-05-16 22:43:54 +01:00
pub fn from_png(path: PathBuf) -> Self {
let name = path.file_stem().unwrap().to_str().unwrap().into();
let image = ImageReader::open(path).unwrap().decode().unwrap();
let mut colours: Vec<Rgba<u8>> = Vec::new();
for (_x, _y, pixel) in image.pixels() {
if !colours.contains(&pixel) {
colours.push(pixel);
}
}
let colours = colours.iter().map(|colour|
Colour { red: colour.0[0], green: colour.0[1], blue: colour.0[2] }
).collect();
// todo preserve original image?
Self { name, colours }
}
pub fn to_png(&self) -> DynamicImage {
let mut image = DynamicImage::new_rgb8(
self.colours.len() as u32, 1 as u32
);
for (x, colour) in self.colours.iter().enumerate() {
let pixel = Rgba::from([colour.red, colour.green, colour.blue, 255]);
image.put_pixel(x as u32, 0, pixel);
}
image
}
}
#[cfg(test)]
mod test {
2021-05-16 14:00:13 +01:00
use std::path::PathBuf;
use crate::{Colour, Palette};
#[test]
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 = crate::mock::palette::soup11().to_jasc();
2021-05-17 17:29:03 +01:00
let expected = include_str!("test-resources/palettes/soup11.pal");
2021-05-16 14:00:13 +01:00
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();
2021-05-17 17:29:03 +01:00
let expected = include_str!("test-resources/palettes/soup11.gpl");
assert_eq!(output, expected);
}
2021-05-16 18:54:23 +01:00
#[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();
2021-05-17 17:29:03 +01:00
let expected = include_str!("test-resources/palettes/soup11.txt");
2021-05-16 18:54:23 +01:00
assert_eq!(output, expected);
}
2021-05-16 20:56:41 +01:00
#[test]
fn palette_from_hex() {
let path = PathBuf::from("src/test-resources/basic/palettes/soup11.hex");
let output = Palette::from_hex(path);
let expected = crate::mock::palette::soup11();
assert_eq!(output, expected);
}
#[test]
fn palette_to_hex() {
let output = crate::mock::palette::soup11().to_hex();
2021-05-17 17:29:03 +01:00
let expected = include_str!("test-resources/palettes/soup11.hex");
2021-05-16 20:56:41 +01:00
assert_eq!(output, expected);
}
2021-05-16 22:43:54 +01:00
#[test]
fn palette_from_png() {
let path = PathBuf::from("src/test-resources/basic/palettes/soup11.png");
let output = Palette::from_png(path);
let expected = crate::mock::palette::soup11();
assert_eq!(output, expected);
}
#[test]
fn palette_to_png() {
use image::io::Reader as ImageReader;
let output = crate::mock::palette::soup11().to_png();
let path = PathBuf::from("src/test-resources/basic/palettes/soup11.png");
let expected = ImageReader::open(path).unwrap().decode().unwrap();
assert_eq!(output, expected);
}
}