//! Image and color representations. use std::ops::{Index, IndexMut}; /// RGBA8 color. #[derive(Clone, Debug, PartialEq)] pub struct Color { pub r: u8, pub g: u8, pub b: u8, pub a: u8, } /// Image with width and height. pub struct Image { w: usize, h: usize, pub cr: Vec, } impl Color { /// Converts a R5G5B5 format color to RGBA8. pub fn from_r5g5b5(rgb: u16) -> Color { let r = rgb >> 10 & 0x1f; let g = rgb >> 5 & 0x1f; let b = rgb & 0x1f; Color{r: (r << 3 | r >> 2) as u8, g: (g << 3 | g >> 2) as u8, b: (b << 3 | b >> 2) as u8, a: 255} } } impl Image { /// Creates a new Image structure. pub fn new(w: usize, h: usize) -> Image { Image{w, h, cr: Vec::with_capacity(w * h)} } pub fn w(&self) -> usize { self.w } pub fn h(&self) -> usize { self.h } } impl Index<(usize, usize)> for Image { type Output = Color; fn index(&self, (x, y): (usize, usize)) -> &Color { &self.cr[x + y * self.w] } } impl IndexMut<(usize, usize)> for Image { fn index_mut(&mut self, (x, y): (usize, usize)) -> &mut Color { &mut self.cr[x + y * self.w] } } // EOF