diff --git a/agb-image-converter/src/colour.rs b/agb-image-converter/src/colour.rs index 917b3b99..b401b9bd 100644 --- a/agb-image-converter/src/colour.rs +++ b/agb-image-converter/src/colour.rs @@ -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 { + 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)) + } +} diff --git a/agb-image-converter/src/config.rs b/agb-image-converter/src/config.rs index eaa2cd20..075a9f7d 100644 --- a/agb-image-converter/src/config.rs +++ b/agb-image-converter/src/config.rs @@ -23,6 +23,7 @@ pub(crate) fn parse(filename: &str) -> Box { pub(crate) trait Config { fn crate_prefix(&self) -> String; fn images(&self) -> HashMap; + fn transparent_colour(&self) -> Option; } pub(crate) trait Image { @@ -36,6 +37,7 @@ pub(crate) trait Image { pub struct ConfigV1 { version: String, crate_prefix: Option, + transparent_colour: Option, image: HashMap, } @@ -53,6 +55,21 @@ impl Config for ConfigV1 { .map(|(filename, image)| (filename.clone(), image as &dyn Image)) .collect() } + + fn transparent_colour(&self) -> Option { + 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 { - 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 { diff --git a/agb-image-converter/src/palette16.rs b/agb-image-converter/src/palette16.rs index afb50c68..2878bad6 100644 --- a/agb-image-converter/src/palette16.rs +++ b/agb-image-converter/src/palette16.rs @@ -58,7 +58,7 @@ impl Palette16 { }) as u8 } - pub fn colours<'a>(&'a self) -> impl Iterator + 'a { + pub fn colours(&self) -> impl Iterator { self.colours.iter() } @@ -167,7 +167,10 @@ impl Palette16Optimiser { fn find_maximal_palette_for(&self, unsatisfied_palettes: &HashSet) -> 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];