lib8: drop custom RGB8 struct for rgb crate

This commit is contained in:
2024-10-30 20:48:00 +01:00
parent b7995423d7
commit 1668db2c85
6 changed files with 54 additions and 75 deletions

View File

@@ -1,65 +1,41 @@
use palette::convert::FromColorUnclamped;
use palette::encoding::srgb::Srgb;
use palette::Hsv;
use crate::power::AsMilliwatts;
use rgb::RGB8;
#[derive(PartialEq, Debug, Copy, Clone)]
pub struct RGB8 {
pub red: u8,
pub green: u8,
pub blue: u8
pub trait IntoRgb8 {
fn into_rgb8(self) -> RGB8;
}
impl RGB8 {
pub const fn new(red : u8, green : u8, blue : u8) -> Self {
Self {
red: red,
green: green,
blue: blue
}
impl IntoRgb8 for RGB8 {
fn into_rgb8(self) -> RGB8 {
self
}
}
impl FromColorUnclamped<Hsv<Srgb, u8>> for RGB8 {
fn from_color_unclamped(hsv: Hsv<Srgb, u8>) -> RGB8 {
if hsv.saturation == 0 {
return RGB8::new(hsv.value, hsv.value, hsv.value);
impl IntoRgb8 for palette::Hsv<Srgb, u8> {
fn into_rgb8(self) -> RGB8 {
if self.saturation == 0 {
return RGB8::new(self.value, self.value, self.value);
}
let region = hsv.hue.into_inner() / 43;
let remainder = (hsv.hue.into_inner() - (region * 43)) * 6;
let region = self.hue.into_inner() / 43;
let remainder = (self.hue.into_inner() - (region * 43)) * 6;
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);
let p = self.value.wrapping_mul(255 - self.saturation).wrapping_shr(8);
let q = (self.value.wrapping_mul(255 - ((self.saturation.wrapping_mul(remainder)).wrapping_shr(8)))).wrapping_shr(8);
let t = (self.value.wrapping_mul(255 - ((self.saturation.wrapping_mul(255 - remainder)).wrapping_shr(8)))).wrapping_shr(8);
match region {
0 => RGB8::new(hsv.value, t, p),
1 => RGB8::new(q, hsv.value, p),
2 => RGB8::new(p, hsv.value, t),
3 => RGB8::new(p, q, hsv.value),
4 => RGB8::new(t, p, hsv.value),
_ => RGB8::new(hsv.value, p, q)
0 => RGB8::new(self.value, t, p),
1 => RGB8::new(q, self.value, p),
2 => RGB8::new(p, self.value, t),
3 => RGB8::new(p, q, self.value),
4 => RGB8::new(t, p, self.value),
_ => RGB8::new(self.value, p, q)
}
}
}
impl AsMilliwatts for RGB8 {
fn as_milliwatts(&self) -> u32 {
const RED_MW : u32 = 16 * 5; //< 16mA @ 5v = 80mW
const GREEN_MW : u32 = 11 * 5; //< 11mA @ 5v = 55mW
const BLUE_MW : u32 = 15 * 5; //< 15mA @ 5v = 75mW
const DARK_MW : u32 = 1 * 5; //< 1mA @ 5v = 5mW
let red = (self.red as u32 * RED_MW).wrapping_shr(8);
let green = (self.green as u32 * GREEN_MW).wrapping_shr(8);
let blue = (self.blue as u32 * BLUE_MW).wrapping_shr(8);
return red + green + blue + DARK_MW;
}
}
#[cfg(feature="embedded-graphics")]
mod embedded_graphics {
use embedded_graphics::pixelcolor::RgbColor;
@@ -90,20 +66,4 @@ mod embedded_graphics {
impl PixelColor for RGB8 {
type Raw = RawU8;
}
impl<T: RgbColor> AsMilliwatts for T {
fn as_milliwatts(&self) -> u32 {
const RED_MW : u32 = 16 * 5; //< 16mA @ 5v = 80mW
const GREEN_MW : u32 = 11 * 5; //< 11mA @ 5v = 55mW
const BLUE_MW : u32 = 15 * 5; //< 15mA @ 5v = 75mW
const DARK_MW : u32 = 1 * 5; //< 1mA @ 5v = 5mW
let red = (self.r() as u32 * RED_MW).wrapping_shr(8);
let green = (self.g() as u32 * GREEN_MW).wrapping_shr(8);
let blue = (self.b() as u32 * BLUE_MW).wrapping_shr(8);
return red + green + blue + DARK_MW;
}
}
}