REAL SENSORS

This commit is contained in:
2025-08-15 21:42:35 -04:00
parent 9f1802f497
commit a7dfacb83e
10 changed files with 257 additions and 123 deletions

View File

@@ -1,7 +1,7 @@
#![allow(clippy::multiple_bound_locations)]
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Zeroable)]
#[derive(Clone, Copy, bytemuck::Zeroable, Debug)]
pub struct Color<T: ColorNum> {
pub r: T,
pub g: T,
@@ -20,10 +20,9 @@ impl<T: ColorNum> Color<T> {
pub const GREEN: Self = Self::rgb(T::MIN, T::MAX, T::MIN);
pub const CYAN: Self = Self::rgb(T::MIN, T::MAX, T::MAX);
pub const BLUE: Self = Self::rgb(T::MIN, T::MIN, T::MAX);
pub const INDIGO: Self = Self::rgb(T::MIN, T::MID, T::MAX);
pub const MAGENTA: Self = Self::rgb(T::MAX, T::MIN, T::MAX);
}
impl<T: ColorNum> Color<T> {
pub const fn new(r: T, g: T, b: T, a: T) -> Self {
Self { r, g, b, a }
}
@@ -48,4 +47,54 @@ impl ColorNum for u8 {
const MAX: Self = u8::MAX;
}
impl ColorNum for f32 {
const MIN: Self = 0.0;
const MID: Self = 0.5;
const MAX: Self = 1.0;
}
unsafe impl bytemuck::Pod for Color<u8> {}
pub trait F32Conversion {
fn to(self) -> f32;
fn from(x: f32) -> Self;
}
impl<T: ColorNum + F32Conversion> Color<T> {
pub fn mul_rgb(self, x: impl F32Conversion) -> Self {
let x = x.to();
Self {
r: T::from(self.r.to() * x),
g: T::from(self.g.to() * x),
b: T::from(self.b.to() * x),
a: self.a,
}
}
pub fn add_rgb(self, x: impl F32Conversion) -> Self {
let x = x.to();
Self {
r: T::from(self.r.to() + x),
g: T::from(self.g.to() + x),
b: T::from(self.b.to() + x),
a: self.a,
}
}
}
impl F32Conversion for f32 {
fn to(self) -> f32 {
self
}
fn from(x: f32) -> Self {
x
}
}
impl F32Conversion for u8 {
fn to(self) -> f32 {
self as f32 / 255.0
}
fn from(x: f32) -> Self {
(x * 255.0).clamp(0.0, 255.0) as Self
}
}