irospace/
srgb.rs

1use std::fmt::Display;
2
3#[allow(non_camel_case_types)]
4#[derive(Debug, Default, PartialEq, Clone, Copy)]
5pub struct sRgbColor {
6    r: f64,
7    g: f64,
8    b: f64,
9    a: f64,
10}
11
12impl Display for sRgbColor {
13    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14        write!(
15            f,
16            "sRgbColor R = {} G = {} B = {} A = {}",
17            self.r, self.g, self.b, self.a
18        )
19    }
20}
21
22impl sRgbColor {
23    pub fn new(r: f64, g: f64, b: f64) -> Self {
24        Self::from_srgba(r, g, b, 1f64)
25    }
26
27    pub fn from_srgba(r: f64, g: f64, b: f64, a: f64) -> Self {
28        Self { r, g, b, a }
29    }
30
31    pub fn r(&self) -> f64 {
32        self.r
33    }
34
35    pub fn g(&self) -> f64 {
36        self.g
37    }
38
39    pub fn b(&self) -> f64 {
40        self.b
41    }
42
43    pub fn a(&self) -> f64 {
44        self.a
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn srgb_new_test() {
54        let red = sRgbColor::new(1f64, 0f64, 0f64);
55
56        assert_eq!(red.r(), 1f64);
57        assert_eq!(red.g(), 0f64);
58        assert_eq!(red.b(), 0f64);
59        assert_eq!(red.a(), 1f64);
60    }
61}