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
use crate::{ FromRgb, ToRgb, approx };
#[derive(Copy, Clone, Debug, Default)]
pub struct Rgb {
pub r: f64,
pub g: f64,
pub b: f64,
}
impl Rgb {
#[inline]
pub fn new(r: f64, g: f64, b: f64) -> Self {
Self { r, g, b }
}
pub fn from_hex(hex: u32) -> Self {
Self {
r: (((hex >> 16) & 0xff) as f64) / 255.0,
g: (((hex >> 8) & 0xff) as f64) / 255.0,
b: ((hex & 0xff) as f64) / 255.0,
}
}
}
impl PartialEq for Rgb {
fn eq(&self, other: &Self) -> bool {
approx(self.r, other.r) &&
approx(self.g, other.g) &&
approx(self.b, other.b)
}
}
impl FromRgb for Rgb {
fn from_rgb(rgb: &Rgb) -> Self {
*rgb
}
}
impl ToRgb for Rgb {
fn to_rgb(&self) -> Rgb {
*self
}
}