Add transparent colour to the image itself

This commit is contained in:
Gwilym Kuiper 2022-10-08 16:43:25 +01:00
parent 12ed911f16
commit 89af366b7a
3 changed files with 46 additions and 15 deletions

View file

@ -1,3 +1,5 @@
use std::str::FromStr;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Colour {
pub r: u8,
@ -20,3 +22,22 @@ impl Colour {
self.a != 255
}
}
impl FromStr for Colour {
type Err = String;
fn from_str(colour: &str) -> Result<Self, Self::Err> {
if colour.len() != 6 {
return Err(format!(
"Expected colour to be 6 characters, got {}",
colour
));
}
let r = u8::from_str_radix(&colour[0..2], 16).unwrap();
let g = u8::from_str_radix(&colour[2..4], 16).unwrap();
let b = u8::from_str_radix(&colour[4..6], 16).unwrap();
Ok(Colour::from_rgb(r, g, b, 255))
}
}

View file

@ -23,6 +23,7 @@ pub(crate) fn parse(filename: &str) -> Box<dyn Config> {
pub(crate) trait Config {
fn crate_prefix(&self) -> String;
fn images(&self) -> HashMap<String, &dyn Image>;
fn transparent_colour(&self) -> Option<Colour>;
}
pub(crate) trait Image {
@ -36,6 +37,7 @@ pub(crate) trait Image {
pub struct ConfigV1 {
version: String,
crate_prefix: Option<String>,
transparent_colour: Option<String>,
image: HashMap<String, ImageV1>,
}
@ -53,6 +55,21 @@ impl Config for ConfigV1 {
.map(|(filename, image)| (filename.clone(), image as &dyn Image))
.collect()
}
fn transparent_colour(&self) -> Option<Colour> {
if let Some(colour) = &self
.transparent_colour
.as_ref()
.map(|colour| colour.parse().unwrap())
{
return Some(*colour);
}
self.images()
.values()
.flat_map(|image| image.transparent_colour())
.next()
}
}
#[derive(Deserialize)]
@ -69,19 +86,9 @@ impl Image for ImageV1 {
}
fn transparent_colour(&self) -> Option<Colour> {
if let Some(colour) = &self.transparent_colour {
if colour.len() != 6 {
panic!("Expected colour to be 6 characters, got {}", colour);
}
let r = u8::from_str_radix(&colour[0..2], 16).unwrap();
let g = u8::from_str_radix(&colour[2..4], 16).unwrap();
let b = u8::from_str_radix(&colour[4..6], 16).unwrap();
return Some(Colour::from_rgb(r, g, b, 255));
}
None
self.transparent_colour
.as_ref()
.map(|colour| colour.parse().unwrap())
}
fn tilesize(&self) -> TileSize {

View file

@ -58,7 +58,7 @@ impl Palette16 {
}) as u8
}
pub fn colours<'a>(&'a self) -> impl Iterator<Item = &Colour> + 'a {
pub fn colours(&self) -> impl Iterator<Item = &Colour> {
self.colours.iter()
}
@ -167,7 +167,10 @@ impl Palette16Optimiser {
fn find_maximal_palette_for(&self, unsatisfied_palettes: &HashSet<Palette16>) -> Palette16 {
let mut palette = Palette16::new();
palette.add_colour(self.transparent_colour.unwrap_or_else(|| Colour::from_rgb(255, 0, 255, 0)));
palette.add_colour(
self.transparent_colour
.unwrap_or_else(|| Colour::from_rgb(255, 0, 255, 0)),
);
loop {
let mut colour_usage = vec![0; MAX_COLOURS];