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
mod alpha;
mod qbasic;
pub use self::{
alpha::{AlphaColor, TRANSPARENT_BLACK},
qbasic::color,
};
use {
crate::math::{vec3, Vec3},
gfx_hal::command::{ClearColor, ClearValue},
};
pub const BLACK: Color = Color::rgb(0, 0, 0);
pub const BLUE: Color = Color::rgb(0, 0, 255);
pub const CORNFLOWER_BLUE: Color = Color::rgb(100, 149, 237);
pub const MAGENTA: Color = Color::rgb(255, 0, 255);
pub const GREEN: Color = Color::rgb(0, 255, 0);
pub const RED: Color = Color::rgb(255, 0, 0);
pub const WHITE: Color = Color::rgb(255, 255, 255);
#[derive(Clone, Copy, Debug)]
pub struct Color {
pub b: u8,
pub g: u8,
pub r: u8,
}
impl Color {
pub const fn rgb(r: u8, g: u8, b: u8) -> Self {
Self { b, g, r }
}
pub fn to_hex(self) -> String {
format!("#{:x}{:x}{:x}", self.r, self.g, self.b)
}
pub fn to_rgb(self) -> Vec3 {
const SCALE: f32 = 1.0 / u8::MAX as f32;
vec3(
self.r as f32 * SCALE,
self.g as f32 * SCALE,
self.b as f32 * SCALE,
)
}
}
impl From<(u8, u8, u8)> for Color {
fn from((r, g, b): (u8, u8, u8)) -> Self {
Self { b, g, r }
}
}
impl From<AlphaColor> for Color {
fn from(alpha_color: AlphaColor) -> Self {
Self {
b: alpha_color.b,
g: alpha_color.g,
r: alpha_color.r,
}
}
}
impl From<ClearValue> for Color {
fn from(clear_value: ClearValue) -> Self {
unsafe {
Self {
b: (clear_value.color.float32[0] * 255.0) as _,
g: (clear_value.color.float32[1] * 255.0) as _,
r: (clear_value.color.float32[2] * 255.0) as _,
}
}
}
}
impl From<Color> for ClearValue {
fn from(color: Color) -> Self {
let alpha_color: AlphaColor = color.into();
let color = alpha_color.to_rgba();
Self {
color: ClearColor {
float32: [color.x, color.y, color.z, color.w],
},
}
}
}