use core::fmt::Debug;
use embedded_graphics_core::pixelcolor::{Rgb565, RgbColor};
#[derive(Debug, Clone, Copy)]
pub struct FogConfig {
pub color: Rgb565,
pub near: u32,
pub far: u32,
}
impl FogConfig {
pub fn new(color: Rgb565, near: f32, far: f32) -> Self {
Self {
color,
near: (near * 65536.0) as u32,
far: (far * 65536.0) as u32,
}
}
#[inline]
pub fn apply(&self, base_color: Rgb565, depth: u32) -> Rgb565 {
let fog_factor = if depth <= self.near {
0u32
} else if depth >= self.far {
65536u32
} else {
let numerator = (depth - self.near) as u64;
let denominator = (self.far - self.near) as u64;
((numerator * 65536) / denominator) as u32
};
let base_r = base_color.r() as u32;
let base_g = base_color.g() as u32;
let base_b = base_color.b() as u32;
let fog_r = self.color.r() as u32;
let fog_g = self.color.g() as u32;
let fog_b = self.color.b() as u32;
let r = ((base_r * (65536 - fog_factor) + fog_r * fog_factor) / 65536) as u8;
let g = ((base_g * (65536 - fog_factor) + fog_g * fog_factor) / 65536) as u8;
let b = ((base_b * (65536 - fog_factor) + fog_b * fog_factor) / 65536) as u8;
Rgb565::new(r, g, b)
}
}
#[derive(Debug, Clone, Copy)]
pub struct DitherConfig {
pub intensity: u8,
}
impl DitherConfig {
const BAYER_MATRIX: [[u8; 4]; 4] =
[[0, 8, 2, 10], [12, 4, 14, 6], [3, 11, 1, 9], [15, 7, 13, 5]];
pub fn new(intensity: u8) -> Self {
Self { intensity }
}
#[inline]
pub fn apply(&self, color: Rgb565, x: i32, y: i32) -> Rgb565 {
if self.intensity == 0 {
return color;
}
let matrix_x = (x & 3) as usize;
let matrix_y = (y & 3) as usize;
let threshold = Self::BAYER_MATRIX[matrix_y][matrix_x];
let scaled_threshold = ((threshold as u16 * self.intensity as u16) / 15) as u8;
let r = color.r();
let g = color.g();
let b = color.b();
let r = if r > scaled_threshold {
r.saturating_sub(scaled_threshold / 2)
} else {
r.saturating_add(scaled_threshold / 2)
};
let g = if g > scaled_threshold {
g.saturating_sub(scaled_threshold / 2)
} else {
g.saturating_add(scaled_threshold / 2)
};
let b = if b > scaled_threshold {
b.saturating_sub(scaled_threshold / 2)
} else {
b.saturating_add(scaled_threshold / 2)
};
Rgb565::new(r, g, b)
}
}