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
use crate::{
math::vec4::Vec4,
visitor::{Visitor, VisitResult, Visit}
};
#[derive(Copy, Clone, Debug)]
#[repr(C)]
pub struct Color {
pub r: u8,
pub g: u8,
pub b: u8,
pub a: u8,
}
impl Default for Color {
fn default() -> Self {
Self::WHITE
}
}
impl Color {
pub const WHITE: Self = Self { r: 255, g: 255, b: 255, a: 255 };
pub const BLACK: Self = Self { r: 0, g: 0, b: 0, a: 255 };
pub const RED: Self = Self { r: 255, g: 0, b: 0, a: 255 };
pub const GREEN: Self = Self { r: 0, g: 255, b: 0, a: 255 };
pub const BLUE: Self = Self { r: 0, g: 0, b: 255, a: 255 };
pub fn opaque(r: u8, g: u8, b: u8) -> Color {
Color { r, g, b, a: 255 }
}
pub fn from_rgba(r: u8, g: u8, b: u8, a: u8) -> Color {
Color { r, g, b, a }
}
pub fn as_frgba(self) -> Vec4 {
Vec4 {
x: f32::from(self.r) / 255.0,
y: f32::from(self.g) / 255.0,
z: f32::from(self.b) / 255.0,
w: f32::from(self.a) / 255.0,
}
}
pub fn lerp(self, other: Self, t: f32) -> Self {
let dr = (t * (i32::from(other.r) - i32::from(self.r)) as f32) as i32;
let dg = (t * (i32::from(other.g) - i32::from(self.g)) as f32) as i32;
let db = (t * (i32::from(other.b) - i32::from(self.b)) as f32) as i32;
let da = (t * (i32::from(other.a) - i32::from(self.a)) as f32) as i32;
let red = (i32::from(self.r) + dr) as u8;
let green = (i32::from(self.g) + dg) as u8;
let blue = (i32::from(self.b) + db) as u8;
let alpha = (i32::from(self.a) + da) as u8;
Self {
r: red,
g: green,
b: blue,
a: alpha,
}
}
}
impl Visit for Color {
fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
visitor.enter_region(name)?;
self.r.visit("R", visitor)?;
self.g.visit("G", visitor)?;
self.b.visit("B", visitor)?;
self.a.visit("A", visitor)?;
visitor.leave_region()
}
}