pub struct Block {
pub bytes: Vec<u8>,
pub format: Format,
pub width: u32,
pub height: u32,
}
pub enum Format {
RGB,
RGBA, }
impl Block {
pub fn new(bytes: Vec<u8>, width: u32, height: u32) -> Self {
Self {
bytes,
format: Format::RGB,
width,
height,
}
}
pub fn from_colors(colors: Vec<u32>, width: u32, height: u32) -> Self {
Self {
bytes: Block::flatten(colors),
format: Format::RGB,
width,
height,
}
}
pub fn colors(&self) -> Vec<u32> {
self
.bytes
.chunks(3)
.map(|c| {
let r = (c[0] as u32) << 16;
let g = (c[1] as u32) << 8;
let b = c[2] as u32;
(r | g | b) as u32
})
.collect()
}
pub fn flatten(colors: Vec<u32>) -> Vec<u8> {
let mut result = Vec::new();
for color in colors {
for i in (0..3).rev() {
result.push(((color >> (i * 8)) & 0xFF) as u8);
}
}
result
}
}