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
#[derive(Clone)]
pub enum Color {
Rgba32(u32),
}
impl Color {
pub const fn rgba32(rgba: u32) -> Color {
Color::Rgba32(rgba)
}
pub const fn rgb24(rgb: u32) -> Color {
Color::rgba32((rgb << 8) | 0xff)
}
pub fn rgba<F: Into<f64>>(r: F, g: F, b: F, a: F) -> Color {
let r = (r.into().max(0.0).min(1.0) * 255.0).round() as u32;
let g = (g.into().max(0.0).min(1.0) * 255.0).round() as u32;
let b = (b.into().max(0.0).min(1.0) * 255.0).round() as u32;
let a = (a.into().max(0.0).min(1.0) * 255.0).round() as u32;
Color::rgba32((r << 24) | (g << 16) | (b << 8) | a)
}
pub fn rgb<F: Into<f64>>(r: F, g: F, b: F) -> Color {
let r = (r.into().max(0.0).min(1.0) * 255.0).round() as u32;
let g = (g.into().max(0.0).min(1.0) * 255.0).round() as u32;
let b = (b.into().max(0.0).min(1.0) * 255.0).round() as u32;
Color::rgba32((r << 24) | (g << 16) | (b << 8) | 0xff)
}
pub fn as_rgba32(&self) -> u32 {
match *self {
Color::Rgba32(rgba) => rgba,
}
}
pub const WHITE: Color = Color::rgba32(0xff_ff_ff_ff);
pub const BLACK: Color = Color::rgba32(0x00_00_00_ff);
}