/
color.rs
79 lines (64 loc) 路 1.87 KB
/
color.rs
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
//! Functions and types relating to color.
/// Represents an RGBA color.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Color {
/// The red component of the color.
pub r: f32,
/// The green component of the color.
pub g: f32,
/// The blue component of the color.
pub b: f32,
/// The alpha component of the color.
pub a: f32,
}
impl Color {
/// Creates a new `Color`, with the specified RGB values and the alpha set to 1.0.
pub fn rgb(r: f32, g: f32, b: f32) -> Color {
Color { r, g, b, a: 1.0 }
}
/// Creates a new `Color`, with the specified RGBA values.
pub fn rgba(r: f32, g: f32, b: f32, a: f32) -> Color {
Color { r, g, b, a }
}
/// Creates a new `Color`, with the specified RGB integer values and the alpha set to 1.0.
pub fn rgb8(r: u8, g: u8, b: u8) -> Color {
let r = f32::from(r) / 255.0;
let g = f32::from(g) / 255.0;
let b = f32::from(b) / 255.0;
Color { r, g, b, a: 1.0 }
}
/// Creates a new `Color`, with the specified RGBA integer values.
pub fn rgba8(r: u8, g: u8, b: u8, a: u8) -> Color {
let r = f32::from(r) / 255.0;
let g = f32::from(g) / 255.0;
let b = f32::from(b) / 255.0;
let a = f32::from(a) / 255.0;
Color { r, g, b, a }
}
}
/// Shortcut for Color::rgb(0.0, 0.0, 0.0).
pub const BLACK: Color = Color {
r: 0.0,
g: 0.0,
b: 0.0,
a: 1.0,
};
/// Shortcut for Color::rgb(1.0, 1.0, 1.0).
pub const WHITE: Color = Color {
r: 1.0,
g: 1.0,
b: 1.0,
a: 1.0,
};
#[cfg(test)]
mod tests {
use super::Color;
#[test]
fn rgb8_creation() {
let c1 = Color::rgb8(100, 149, 236);
let c2 = Color::rgb(0.39, 0.58, 0.92);
assert!((c1.r - c2.r).abs() < 0.01);
assert!((c1.g - c2.g).abs() < 0.01);
assert!((c1.b - c2.b).abs() < 0.01);
}
}