use crate::ser::{Ser, Sink, Source}; #[derive(Copy, Clone, Debug, Default)] pub struct Pixel { pub r: u8, pub g: u8, pub b: u8, } impl Ser for Pixel { fn write(&self, sink: &mut impl std::io::Write) -> anyhow::Result<()> { sink.put((self.r, self.g, self.b)) } fn read(source: &mut impl std::io::Read) -> anyhow::Result { let (r, g, b) = source.get()?; Ok(Self { r, g, b }) } } impl Pixel { pub const BLACK: Pixel = Pixel { r: 0, g: 0, b: 0 }; #[inline] pub fn distance(a: Pixel, b: Pixel) -> f64 { let (rd, gd, bd) = ( a.r.abs_diff(b.r) as f64, a.r.abs_diff(b.r) as f64, a.r.abs_diff(b.r) as f64, ); (rd * rd + gd * gd + bd * bd).sqrt() } }