drawing_stuff/
color.rs

1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2pub struct RGBA {
3    pub r: u8,
4    pub g: u8,
5    pub b: u8,
6    pub a: u8,
7}
8
9impl RGBA {
10    pub fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
11        Self { r, g, b, a }
12    }
13
14    pub fn to_rgb(self) -> (RGB, u8) {
15        (
16            RGB {
17                r: self.r,
18                g: self.g,
19                b: self.b,
20            },
21            self.a,
22        )
23    }
24}
25
26#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27pub struct RGB {
28    pub r: u8,
29    pub g: u8,
30    pub b: u8,
31}
32
33impl RGB {
34    /// Adds an RGBA value onto a RGB value returning the result.
35    /// This simply performs a linear interpolation between the two.
36    pub fn add_rgba(self, other: RGBA) -> Self {
37        let (other, alpha) = other.to_rgb();
38        self.lerp(&other, alpha as f64 / 255.0)
39    }
40
41    /// Performs a linear interpolation between two RGB values returning the result.
42    pub fn lerp(&self, other: &Self, a: f64) -> Self {
43        RGB {
44            r: ((1.0 - a) * self.r as f64 + a * other.r as f64) as u8,
45            g: ((1.0 - a) * self.g as f64 + a * other.g as f64) as u8,
46            b: ((1.0 - a) * self.b as f64 + a * other.b as f64) as u8,
47        }
48    }
49}
50
51//== constants =====
52
53pub const TRANSPARANT: RGBA = RGBA {
54    r: 0,
55    g: 0,
56    b: 0,
57    a: 0,
58};
59
60pub const BLACK: RGBA = RGBA {
61    r: 0,
62    g: 0,
63    b: 0,
64    a: 255,
65};
66
67pub const WHITE: RGBA = RGBA {
68    r: 255,
69    g: 255,
70    b: 255,
71    a: 255,
72};
73
74pub const RED: RGBA = RGBA {
75    r: 255,
76    g: 0,
77    b: 0,
78    a: 255,
79};
80
81pub const GREEN: RGBA = RGBA {
82    r: 0,
83    g: 255,
84    b: 0,
85    a: 255,
86};
87
88pub const BLUE: RGBA = RGBA {
89    r: 0,
90    g: 0,
91    b: 255,
92    a: 255,
93};