From ea5232e04841def51bab75967b3ee2fa8464c1b3 Mon Sep 17 00:00:00 2001 From: Victoria Fischer Date: Sun, 27 Oct 2024 11:02:10 +0100 Subject: [PATCH] lib8: implement RgbColor + PixelColor for RGB8 --- src/lib8.rs | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/lib8.rs b/src/lib8.rs index 222e99a..d27367d 100644 --- a/src/lib8.rs +++ b/src/lib8.rs @@ -1,8 +1,11 @@ -use palette::convert::{IntoColor, IntoColorUnclamped, FromColorUnclamped}; +use palette::convert::FromColorUnclamped; use palette::encoding::srgb::Srgb; 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 red: u8, pub green: u8, @@ -10,7 +13,7 @@ pub struct RGB8 { } impl RGB8 { - fn new(red : u8, green : u8, blue : u8) -> Self { + const fn new(red : u8, green : u8, blue : u8) -> Self { Self { red: red, 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> for RGB8 { fn from_color_unclamped(hsv: Hsv) -> RGB8 { if hsv.saturation == 0 { @@ -28,7 +56,7 @@ impl FromColorUnclamped> for RGB8 { let region = hsv.hue.into_inner() / 43; 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 t = (hsv.value.wrapping_mul(255 - ((hsv.saturation.wrapping_mul(255 - remainder)).wrapping_shr(8)))).wrapping_shr(8);