solstice_2d/shared/
color.rs1#[derive(Copy, Clone, Debug)]
2pub struct Color {
3 pub red: f32,
4 pub green: f32,
5 pub blue: f32,
6 pub alpha: f32,
7}
8
9impl Color {
10 pub const fn new(red: f32, green: f32, blue: f32, alpha: f32) -> Self {
11 Self {
12 red,
13 green,
14 blue,
15 alpha,
16 }
17 }
18
19 pub fn from_bytes(red: u8, green: u8, blue: u8, alpha: u8) -> Self {
20 Self {
21 red: red as f32 / u8::MAX as f32,
22 green: green as f32 / u8::MAX as f32,
23 blue: blue as f32 / u8::MAX as f32,
24 alpha: alpha as f32 / u8::MAX as f32,
25 }
26 }
27}
28
29impl Default for Color {
30 fn default() -> Self {
31 Color {
32 red: 1.,
33 green: 1.,
34 blue: 1.,
35 alpha: 1.,
36 }
37 }
38}
39
40impl From<Color> for [f32; 4] {
41 fn from(c: Color) -> Self {
42 [c.red, c.green, c.blue, c.alpha]
43 }
44}
45
46impl From<[f32; 4]> for Color {
47 fn from([red, green, blue, alpha]: [f32; 4]) -> Self {
48 Self {
49 red,
50 green,
51 blue,
52 alpha,
53 }
54 }
55}
56
57impl From<Color> for solstice::Color<solstice::ClampedF32> {
58 fn from(c: Color) -> Self {
59 Self {
60 red: c.red.into(),
61 blue: c.blue.into(),
62 green: c.green.into(),
63 alpha: c.alpha.into(),
64 }
65 }
66}
67
68impl From<Color> for mint::Vector4<f32> {
69 fn from(c: Color) -> Self {
70 Self {
71 x: c.red,
72 y: c.green,
73 z: c.blue,
74 w: c.alpha,
75 }
76 }
77}