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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use serde::{Deserialize, Serialize};
#[repr(C)]
#[derive(
Copy, Clone, Debug, Serialize, Deserialize, PartialEq, bytemuck::Pod, bytemuck::Zeroable,
)]
/// Simple RGBA color struct with some helper functions. each field should be a 0..255 float.
#[allow(missing_docs)]
pub struct Color {
pub r: f32,
pub g: f32,
pub b: f32,
pub a: f32,
}
impl Color {
/// ```
/// Color {
/// r: 0.0,
/// g: 0.0,
/// b: 0.0,
/// a: 0.0,
/// }
/// ```
pub const TRANSPARENT: Self = Self {
r: 0.0,
g: 0.0,
b: 0.0,
a: 0.0,
};
/// ```
/// Color {
/// r: 0.0,
/// g: 0.0,
/// b: 0.0,
/// a: 1.0,
/// }
/// ```
pub const BLACK: Self = Self {
r: 0.0,
g: 0.0,
b: 0.0,
a: 1.0,
};
/// ```
/// Color {
/// r: 1.0,
/// g: 1.0,
/// b: 1.0,
/// a: 1.0,
/// }
/// ```
pub const WHITE: Self = Self {
r: 1.0,
g: 1.0,
b: 1.0,
a: 1.0,
};
/// ```
/// Color {
/// r: 1.0,
/// g: 0.0,
/// b: 0.0,
/// a: 1.0,
/// }
/// ```
pub const RED: Self = Self {
r: 1.0,
g: 0.0,
b: 0.0,
a: 1.0,
};
/// ```
/// Color {
/// r: 0.0,
/// g: 1.0,
/// b: 0.0,
/// a: 1.0,
/// }
/// ```
pub const GREEN: Self = Self {
r: 0.0,
g: 1.0,
b: 0.0,
a: 1.0,
};
/// ```
/// Color {
/// r: 0.0,
/// g: 0.0,
/// b: 1.0,
/// a: 1.0,
/// }
/// ```
pub const BLUE: Self = Self {
r: 0.0,
g: 0.0,
b: 1.0,
a: 1.0,
};
#[must_use]
#[allow(missing_docs)]
pub const fn new(r: f32, g: f32, b: f32, a: f32) -> Self {
Self { r, g, b, a }
}
}
impl From<u32> for Color {
fn from(value: u32) -> Self {
let r = ((value >> 16) & 0xFF) as u8;
let g = ((value >> 8) & 0xFF) as u8;
let b = (value & 0xFF) as u8;
let a = if (value >> 24) == 0 {
255
} else {
((value >> 24) & 0xFF) as u8
};
Self {
r: f32::from(r) / 255.0,
g: f32::from(g) / 255.0,
b: f32::from(b) / 255.0,
a: f32::from(a) / 255.0,
}
}
}