lib8: implement RgbColor + PixelColor for RGB8

This commit is contained in:
Victoria Fischer 2024-10-27 11:02:10 +01:00
parent 9b206f2cad
commit ea5232e048

View File

@ -1,8 +1,11 @@
use palette::convert::{IntoColor, IntoColorUnclamped, FromColorUnclamped}; use palette::convert::FromColorUnclamped;
use palette::encoding::srgb::Srgb; use palette::encoding::srgb::Srgb;
use palette::Hsv; use palette::Hsv;
use embedded_graphics::pixelcolor::RgbColor;
use embedded_graphics::pixelcolor::PixelColor;
use embedded_graphics::pixelcolor::raw::RawU8;
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug, Copy, Clone)]
pub struct RGB8 { pub struct RGB8 {
pub red: u8, pub red: u8,
pub green: u8, pub green: u8,
@ -10,7 +13,7 @@ pub struct RGB8 {
} }
impl RGB8 { impl RGB8 {
fn new(red : u8, green : u8, blue : u8) -> Self { const fn new(red : u8, green : u8, blue : u8) -> Self {
Self { Self {
red: red, red: red,
green: green, green: green,
@ -19,6 +22,31 @@ impl RGB8 {
} }
} }
impl RgbColor for RGB8 {
fn r(&self) -> u8 { self.red }
fn g(&self) -> u8 { self.green }
fn b(&self) -> u8 { self.blue }
const MAX_R: u8 = 255;
const MAX_G: u8 = 255;
const MAX_B: u8 = 255;
const BLACK: Self = Self::new(0, 0, 0);
const WHITE: Self = Self::new(255, 255, 255);
const RED: Self = Self::new(255, 0, 0);
const GREEN: Self = Self::new(0, 255, 0);
const BLUE: Self = Self::new(0, 0, 255);
const YELLOW: Self = Self::new(255, 0, 0);
const CYAN: Self = Self::new(0, 255, 0);
const MAGENTA: Self = Self::new(0, 0, 255);
}
impl PixelColor for RGB8 {
type Raw = RawU8;
}
impl FromColorUnclamped<Hsv<Srgb, u8>> for RGB8 { impl FromColorUnclamped<Hsv<Srgb, u8>> for RGB8 {
fn from_color_unclamped(hsv: Hsv<Srgb, u8>) -> RGB8 { fn from_color_unclamped(hsv: Hsv<Srgb, u8>) -> RGB8 {
if hsv.saturation == 0 { if hsv.saturation == 0 {
@ -28,7 +56,7 @@ impl FromColorUnclamped<Hsv<Srgb, u8>> for RGB8 {
let region = hsv.hue.into_inner() / 43; let region = hsv.hue.into_inner() / 43;
let remainder = (hsv.hue.into_inner() - (region * 43)) * 6; let remainder = (hsv.hue.into_inner() - (region * 43)) * 6;
let p = (hsv.value.wrapping_mul(255 - hsv.saturation).wrapping_shr(8)); let p = hsv.value.wrapping_mul(255 - hsv.saturation).wrapping_shr(8);
let q = (hsv.value.wrapping_mul(255 - ((hsv.saturation.wrapping_mul(remainder)).wrapping_shr(8)))).wrapping_shr(8); let q = (hsv.value.wrapping_mul(255 - ((hsv.saturation.wrapping_mul(remainder)).wrapping_shr(8)))).wrapping_shr(8);
let t = (hsv.value.wrapping_mul(255 - ((hsv.saturation.wrapping_mul(255 - remainder)).wrapping_shr(8)))).wrapping_shr(8); let t = (hsv.value.wrapping_mul(255 - ((hsv.saturation.wrapping_mul(255 - remainder)).wrapping_shr(8)))).wrapping_shr(8);