mirror of
https://github.com/italicsjenga/agb.git
synced 2025-01-11 09:31:34 +11:00
Add transparent colour to the image itself
This commit is contained in:
parent
12ed911f16
commit
89af366b7a
|
@ -1,3 +1,5 @@
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub struct Colour {
|
pub struct Colour {
|
||||||
pub r: u8,
|
pub r: u8,
|
||||||
|
@ -20,3 +22,22 @@ impl Colour {
|
||||||
self.a != 255
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -23,6 +23,7 @@ pub(crate) fn parse(filename: &str) -> Box<dyn Config> {
|
||||||
pub(crate) trait Config {
|
pub(crate) trait Config {
|
||||||
fn crate_prefix(&self) -> String;
|
fn crate_prefix(&self) -> String;
|
||||||
fn images(&self) -> HashMap<String, &dyn Image>;
|
fn images(&self) -> HashMap<String, &dyn Image>;
|
||||||
|
fn transparent_colour(&self) -> Option<Colour>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) trait Image {
|
pub(crate) trait Image {
|
||||||
|
@ -36,6 +37,7 @@ pub(crate) trait Image {
|
||||||
pub struct ConfigV1 {
|
pub struct ConfigV1 {
|
||||||
version: String,
|
version: String,
|
||||||
crate_prefix: Option<String>,
|
crate_prefix: Option<String>,
|
||||||
|
transparent_colour: Option<String>,
|
||||||
|
|
||||||
image: HashMap<String, ImageV1>,
|
image: HashMap<String, ImageV1>,
|
||||||
}
|
}
|
||||||
|
@ -53,6 +55,21 @@ impl Config for ConfigV1 {
|
||||||
.map(|(filename, image)| (filename.clone(), image as &dyn Image))
|
.map(|(filename, image)| (filename.clone(), image as &dyn Image))
|
||||||
.collect()
|
.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)]
|
#[derive(Deserialize)]
|
||||||
|
@ -69,19 +86,9 @@ impl Image for ImageV1 {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn transparent_colour(&self) -> Option<Colour> {
|
fn transparent_colour(&self) -> Option<Colour> {
|
||||||
if let Some(colour) = &self.transparent_colour {
|
self.transparent_colour
|
||||||
if colour.len() != 6 {
|
.as_ref()
|
||||||
panic!("Expected colour to be 6 characters, got {}", colour);
|
.map(|colour| colour.parse().unwrap())
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tilesize(&self) -> TileSize {
|
fn tilesize(&self) -> TileSize {
|
||||||
|
|
|
@ -58,7 +58,7 @@ impl Palette16 {
|
||||||
}) as u8
|
}) as u8
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn colours<'a>(&'a self) -> impl Iterator<Item = &Colour> + 'a {
|
pub fn colours(&self) -> impl Iterator<Item = &Colour> {
|
||||||
self.colours.iter()
|
self.colours.iter()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -167,7 +167,10 @@ impl Palette16Optimiser {
|
||||||
fn find_maximal_palette_for(&self, unsatisfied_palettes: &HashSet<Palette16>) -> Palette16 {
|
fn find_maximal_palette_for(&self, unsatisfied_palettes: &HashSet<Palette16>) -> Palette16 {
|
||||||
let mut palette = Palette16::new();
|
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 {
|
loop {
|
||||||
let mut colour_usage = vec![0; MAX_COLOURS];
|
let mut colour_usage = vec![0; MAX_COLOURS];
|
||||||
|
|
Loading…
Reference in a new issue