lj_rust/src/point.rs
2023-07-24 18:53:19 +02:00

82 lines
1.7 KiB
Rust

use ether_dream::protocol::DacPoint;
use std::ops::Mul;
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct Point {
pub x: f32,
pub y: f32,
pub color: Color,
}
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct Color {
pub r: u8,
pub g: u8,
pub b: u8,
}
impl Mul<u8> for Color {
type Output = Self;
fn mul(self, rhs: u8) -> Self {
Self{
r: (255 * self.r as u16 / rhs as u16) as u8,
g: (255 * self.g as u16 / rhs as u16) as u8,
b: (255 * self.b as u16 / rhs as u16) as u8,
}
}
}
impl From<Color> for u32 {
fn from(value: Color) -> Self {
let r = value.r as u32;
let g = value.g as u32;
let b = (value.b) as u32;
(r << 16) + (g << 8) + b
}
}
impl From<(f32, f32, u32)> for Point {
fn from((x, y, color): (f32, f32, u32)) -> Point {
let r = (color >> 16) as u8;
let g = ((color >> 8) & 255) as u8;
let b = (color & 255) as u8;
Point { x, y, color: Color { r, g, b } }
}
}
impl From<Point> for helios_dac::Point {
fn from(pt: Point) -> helios_dac::Point {
let x = pt.x.clamp(0.0, 4095.0) as u16;
let y = pt.y.clamp(0.0, 4095.0) as u16;
helios_dac::Point {
coordinate: (x, y).into(),
color: helios_dac::Color::new(pt.color.r, pt.color.g, pt.color.b),
intensity: 0xFF,
}
}
}
impl From<Point> for DacPoint {
fn from(pt: Point) -> DacPoint {
let control = 0;
let (u1, u2) = (0, 0);
let i = 0;
let x = pt.x.clamp(-32000.0, 32000.0);
let y = pt.y.clamp(-32000.0, 32000.0);
let pt = DacPoint {
control,
x: x as i16,
y: y as i16,
i,
r: (pt.color.r as u16) * 255,
g: (pt.color.g as u16) * 255,
b: (pt.color.b as u16) * 255,
u1,
u2,
};
// debug!("point {:?}", pt);
pt
}
}