1#[derive(Debug, Clone, Copy, PartialEq)]
4pub struct Color {
5 pub r: f32,
6 pub g: f32,
7 pub b: f32,
8 pub a: f32,
9}
10
11impl Color {
12 pub const TRANSPARENT: Self = Self::rgba(0.0, 0.0, 0.0, 0.0);
13 pub const BLACK: Self = Self::rgb(0.0, 0.0, 0.0);
14 pub const WHITE: Self = Self::rgb(1.0, 1.0, 1.0);
15
16 pub const fn rgb(r: f32, g: f32, b: f32) -> Self {
17 Self { r, g, b, a: 1.0 }
18 }
19 pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
20 Self { r, g, b, a }
21 }
22
23 pub fn from_hex(rgb: u32) -> Self {
25 let r = ((rgb >> 16) & 0xff) as f32 / 255.0;
26 let g = ((rgb >> 8) & 0xff) as f32 / 255.0;
27 let b = (rgb & 0xff) as f32 / 255.0;
28 Self::rgb(r, g, b)
29 }
30
31 pub fn with_alpha(self, a: f32) -> Self {
33 Self { a, ..self }
34 }
35}