use crate::style::Color;
#[repr(u8)]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum SpriteFormat {
A4 = 0,
A8 = 1,
Rgb565 = 2,
Rgba8888 = 5,
}
pub trait PixelFormat: Copy + Clone + Default + PartialEq + Eq + Send + Sync + 'static {
const BLACK: Self;
fn from_color(c: Color) -> Self;
fn to_color(self) -> Color;
#[inline(always)]
fn blend(self, fg: Color, coverage: u8) -> Self {
let alpha = (((fg.a() as u32) * (coverage as u32) + 127) / 255) as u16;
if alpha == 0 {
return self;
}
if alpha >= 255 {
return Self::from_color(fg);
}
let bg = self.to_color();
let inv = 255 - alpha;
let r = (((fg.r() as u16) * alpha + (bg.r() as u16) * inv + 127) / 255) as u8;
let g = (((fg.g() as u16) * alpha + (bg.g() as u16) * inv + 127) / 255) as u8;
let b = (((fg.b() as u16) * alpha + (bg.b() as u16) * inv + 127) / 255) as u8;
Self::from_color(Color::rgb(r, g, b))
}
}
#[repr(transparent)]
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
pub struct Rgb565(pub u16);
impl PixelFormat for Rgb565 {
const BLACK: Self = Self(0);
#[inline(always)]
fn from_color(c: Color) -> Self {
let r = ((c.r() as u16) >> 3) & 0x1F;
let g = ((c.g() as u16) >> 2) & 0x3F;
let b = ((c.b() as u16) >> 3) & 0x1F;
Self((r << 11) | (g << 5) | b)
}
#[inline(always)]
fn to_color(self) -> Color {
let r5 = ((self.0 >> 11) & 0x1F) as u8;
let g6 = ((self.0 >> 5) & 0x3F) as u8;
let b5 = (self.0 & 0x1F) as u8;
let r = (r5 << 3) | (r5 >> 2);
let g = (g6 << 2) | (g6 >> 4);
let b = (b5 << 3) | (b5 >> 2);
Color::rgb(r, g, b)
}
}
#[repr(transparent)]
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
pub struct Xrgb8888(pub u32);
impl PixelFormat for Xrgb8888 {
const BLACK: Self = Self(0);
#[inline(always)]
fn from_color(c: Color) -> Self {
Self(0xFF00_0000 | (c.0 & 0x00FF_FFFF))
}
#[inline(always)]
fn to_color(self) -> Color {
Color(0xFF00_0000 | (self.0 & 0x00FF_FFFF))
}
}
#[repr(transparent)]
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
pub struct Rgba8888(pub u32);
impl Rgba8888 {
#[inline(always)]
pub const fn to_rgba_bytes(self) -> [u8; 4] {
self.0.to_le_bytes()
}
}
impl PixelFormat for Rgba8888 {
const BLACK: Self = Self(0xFF00_0000);
#[inline(always)]
fn from_color(c: Color) -> Self {
let r = c.r() as u32;
let g = (c.g() as u32) << 8;
let b = (c.b() as u32) << 16;
let a = (c.a() as u32) << 24;
Self(a | b | g | r)
}
#[inline(always)]
fn to_color(self) -> Color {
let r = (self.0 & 0xFF) as u8;
let g = ((self.0 >> 8) & 0xFF) as u8;
let b = ((self.0 >> 16) & 0xFF) as u8;
let a = ((self.0 >> 24) & 0xFF) as u8;
Color::rgba(r, g, b, a)
}
#[inline(always)]
fn blend(self, fg: Color, coverage: u8) -> Self {
let sa = (((fg.a() as u32) * (coverage as u32) + 127) / 255) as u16;
if sa == 0 {
return self;
}
if sa >= 255 {
return Self::from_color(Color::rgba(fg.r(), fg.g(), fg.b(), 255));
}
let bg = self.to_color();
let da = bg.a() as u16;
let inv = 255 - sa;
let out_a = sa + ((da * inv + 127) / 255);
let r = (((fg.r() as u16) * sa + (bg.r() as u16) * inv + 127) / 255) as u8;
let g = (((fg.g() as u16) * sa + (bg.g() as u16) * inv + 127) / 255) as u8;
let b = (((fg.b() as u16) * sa + (bg.b() as u16) * inv + 127) / 255) as u8;
Self::from_color(Color::rgba(r, g, b, out_a.min(255) as u8))
}
}