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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
pub use crate::shapes::text::{FontWeight, TextAlign};
pub const fn rbg(r: u8, g: u8, b: u8) -> Color {
Color::RGB { r, g, b }
}
pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Color {
Color::RGBA { r, g, b, a }
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Color {
RGBA { r: u8, g: u8, b: u8, a: u8 },
RGB { r: u8, g: u8, b: u8 },
U32(u32),
}
impl Color {
pub const RED: Color = rbg(255, 0, 0);
pub const GREEN: Color = rbg(0, 255, 0);
pub const BLUE: Color = rbg(0, 0, 255);
pub const WHITE: Color = rbg(255, 255, 255);
pub const BLACK: Color = rbg(0, 0, 0);
pub const YELLOW: Color = rbg(255, 255, 0);
pub const ORANGE: Color = rbg(255, 165, 0);
pub const MAGENTA: Color = rbg(255, 0, 255);
pub const CYAN: Color = rbg(0, 255, 255);
pub const GRAY: Color = rbg(128, 128, 128);
pub const TRANSPARENT: Color = rgba(0, 0, 0, 0);
pub const LIGHT_GRAY: Color = rbg(192, 192, 192);
pub const DARK_GRAY: Color = rbg(64, 64, 64);
pub const fn rgba(self) -> Color {
match self {
Color::RGBA { .. } => self,
Color::RGB { r, g, b } => Color::RGBA { r, g, b, a: 255 },
Color::U32(c) => Color::RGBA {
r: ((c >> 16) & 0xFF) as u8,
g: ((c >> 8) & 0xFF) as u8,
b: (c & 0xFF) as u8,
a: 255,
},
}
}
pub fn hsla(hue: f32, saturation: f32, lightness: f32, alpha: f32) -> Color {
let chroma = 1. - (2. * lightness - 1.).abs() * saturation;
let hue_prime = hue / 60.;
let x = chroma * (1. - (hue_prime % 2. - 1.).abs());
let a = (alpha * 255.) as u8;
let (r, g, b) = if hue_prime < 1. {
(chroma, x, 0.)
} else if hue_prime < 2. {
(x, chroma, 0.)
} else if hue_prime < 3. {
(0., chroma, x)
} else if hue_prime < 4. {
(0., x, chroma)
} else if hue_prime < 5. {
(x, 0., chroma)
} else {
(chroma, 0., x)
};
let m = lightness - chroma / 2.;
Color::RGBA {
r: ((r + m) * 255.) as u8,
g: ((g + m) * 255.) as u8,
b: ((b + m) * 255.) as u8,
a,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Fill {
Color(Color),
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Stroke {
Full {
color: Color,
width: f32,
},
Dashed {
color: Color,
width: f32,
on: f32,
off: f32,
},
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Style {
pub fill: Option<Fill>,
pub stroke: Option<Stroke>,
}