get palette colours by index

This commit is contained in:
Max Bradbury 2021-11-14 18:04:06 +00:00
parent dc8fd68e32
commit c1be226956
1 changed files with 18 additions and 1 deletions

View File

@ -13,7 +13,24 @@ pub struct Palette {
}
impl Palette {
/// todo result
/// if trying to get an out-of-bounds index, colours will wrap around
/// so if palette has 8 colours (0-7) and index is 8, will return 0
pub fn get_colour(&self, index: &u8) -> Option<&Colour> {
self.colours.get((index % self.colours.len() as u8) as usize)
}
/// colour 0 is transparent
pub fn get_colour_rgba8(&self, index: &u8) -> Vec<u8> {
match index {
0 => vec![0,0,0,0],
_ => {
let colour = self.get_colour(index).unwrap();
vec![colour.red, colour.green, colour.blue, 255]
}
}
}
/// todo Result<Palette>
pub fn from_file(path: PathBuf) -> Self {
match path.extension().unwrap().to_str().unwrap() {
"gpl" => Self::from_gpl(path),