From c1be226956ca1deca409f25d0a11cc9a59001315 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Sun, 14 Nov 2021 18:04:06 +0000 Subject: [PATCH] get palette colours by index --- src/palette.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/palette.rs b/src/palette.rs index 73b548f..37704f5 100644 --- a/src/palette.rs +++ b/src/palette.rs @@ -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 { + 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 pub fn from_file(path: PathBuf) -> Self { match path.extension().unwrap().to_str().unwrap() { "gpl" => Self::from_gpl(path),