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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use crate::core::math::*;
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct Color {
pub r: u8,
pub g: u8,
pub b: u8,
pub a: u8,
}
impl Color {
pub const fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
Self { r, g, b, a }
}
pub const fn new_opaque(r: u8, g: u8, b: u8) -> Self {
Self { r, g, b, a: 255 }
}
pub fn from_rgb_slice(rgba: &[f32; 3]) -> Self {
Self {
r: (rgba[0] * 255.0) as u8,
g: (rgba[1] * 255.0) as u8,
b: (rgba[2] * 255.0) as u8,
..Default::default()
}
}
pub fn from_rgba_slice(rgba: &[f32; 4]) -> Self {
Self {
r: (rgba[0] * 255.0) as u8,
g: (rgba[1] * 255.0) as u8,
b: (rgba[2] * 255.0) as u8,
a: (rgba[3] * 255.0) as u8,
}
}
pub const RED: Color = Color::new_opaque(255, 0, 0);
pub const GREEN: Color = Color::new_opaque(0, 255, 0);
pub const BLUE: Color = Color::new_opaque(0, 0, 255);
pub const WHITE: Color = Color::new_opaque(255, 255, 255);
pub const BLACK: Color = Color::new_opaque(0, 0, 0);
pub fn to_vec3(&self) -> Vec3 {
vec3(
self.r as f32 / 255.0,
self.g as f32 / 255.0,
self.b as f32 / 255.0,
)
}
pub fn to_vec4(&self) -> Vec4 {
vec4(
self.r as f32 / 255.0,
self.g as f32 / 255.0,
self.b as f32 / 255.0,
self.a as f32 / 255.0,
)
}
pub fn to_rgb_slice(&self) -> [f32; 3] {
[
self.r as f32 / 255.0,
self.g as f32 / 255.0,
self.b as f32 / 255.0,
]
}
pub fn to_rgba_slice(&self) -> [f32; 4] {
[
self.r as f32 / 255.0,
self.g as f32 / 255.0,
self.b as f32 / 255.0,
self.a as f32 / 255.0,
]
}
}
impl Default for Color {
fn default() -> Self {
Color::WHITE
}
}