aboutsummaryrefslogtreecommitdiff
path: root/evc/src/pixel.rs
blob: 442c1b5e8d3e5f57df770a9e1434197a4b338bb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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) -> std::io::Result<()> {
        sink.put((self.r, self.g, self.b))
    }

    fn read(source: &mut impl std::io::Read) -> std::io::Result<Self> {
        let (r, g, b) = source.get()?;
        Ok(Self { r, g, b })
    }
}

impl Pixel {
    #[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()
    }
}