use bevy::prelude::*;
use crate::{
PColor,
pico8::{PalError, Palette},
};
pub(crate) fn plugin(_app: &mut App) {}
fn default_pico8_color(index: usize) -> Option<Color> {
let [r, g, b, a] = *super::cart::PALETTE.get(index)?;
Some(Color::srgba_u8(r, g, b, a))
}
#[derive(Debug, Default, Deref, DerefMut, Reflect, Clone)]
pub struct Palettes(pub(crate) Vec<Palette>);
impl Palettes {
pub fn get_pal(&self, palette_index: usize) -> Result<&Palette, PalError> {
self.0.get(palette_index).ok_or(PalError::NoSuchPalette {
index: palette_index,
count: self.0.len(),
})
}
pub fn len_in(
&self,
palette_index: usize,
images: &Assets<Image>,
) -> Result<Option<usize>, PalError> {
let pal = self.get_pal(palette_index)?;
Ok(images.get(&pal.image).map(|img| pal.len_in(img)))
}
pub fn get_color(
&self,
c: PColor,
palette_index: usize,
images: &Assets<Image>,
) -> Result<Color, PalError> {
match c {
PColor::Palette(n) => {
let pal = self.get_pal(palette_index)?;
if let Some(image) = images.get(&pal.image) {
pal.get_color_in(n, image)
.map(|c| c.into())
.map_err(|e| match e {
PalError::NoSuchColor(c) => PalError::NoSuchPaletteColor {
color: c,
palette: palette_index,
},
x => x,
})
} else if let Some(color) = default_pico8_color(n) {
Ok(color)
} else {
Err(PalError::NoSuchPaletteColor {
color: n,
palette: palette_index,
})
}
}
PColor::Color(c) => Ok(c.into()),
}
}
}
impl From<Vec<Palette>> for Palettes {
fn from(palettes: Vec<Palette>) -> Self {
Self(palettes)
}
}