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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use std::fmt;
use crate::Node;
#[derive(Copy, Clone)]
#[repr(packed)]
pub struct Color {
pub data: u32,
}
impl Color {
pub const fn rgb(r: u8, g: u8, b: u8) -> Self {
Color {
data: 0xFF00_0000 | ((r as u32) << 16) | ((g as u32) << 8) | (b as u32),
}
}
pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
Color {
data: ((a as u32) << 24) | ((r as u32) << 16) | ((g as u32) << 8) | (b as u32),
}
}
pub fn hsl(h: f32, s: f32, l: f32) -> Self {
Self::hsla(h, s, l, 1.0)
}
pub fn hsla(h: f32, s: f32, l: f32, a: f32) -> Self {
let a = (a * 255.0) as u8;
let mut h = h % 1.0;
if h < 0.0 {
h += 1.0;
}
let s = s.max(0.0).min(1.0);
let l = l.max(0.0).min(1.0);
let m2 = if l <= 0.5 { l * (1.0 + s) } else { l + s - l * s };
let m1 = 2.0 * l - m2;
let r = (hue(h + 1.0 / 3.0, m1, m2).max(0.0).min(1.0) * 255.0) as u8;
let g = (hue(h, m1, m2).max(0.0).min(1.0) * 255.0) as u8;
let b = (hue(h - 1.0 / 3.0, m1, m2).max(0.0).min(1.0) * 255.0) as u8;
Color {
data: ((a as u32) << 24) | ((r as u32) << 16) | ((g as u32) << 8) | (b as u32),
}
}
pub fn r(self) -> u8 {
((self.data & 0x00FF_0000) >> 16) as u8
}
pub fn g(self) -> u8 {
((self.data & 0x0000_FF00) >> 8) as u8
}
pub fn b(self) -> u8 {
(self.data & 0x0000_00FF) as u8
}
pub fn a(self) -> u8 {
((self.data & 0xFF00_0000) >> 24) as u8
}
pub fn interpolate(start_color: Color, end_color: Color, scale: f64) -> Color {
let r = Color::interp(start_color.r(), end_color.r(), scale);
let g = Color::interp(start_color.g(), end_color.g(), scale);
let b = Color::interp(start_color.b(), end_color.b(), scale);
let a = Color::interp(start_color.a(), end_color.a(), scale);
Color::rgba(r, g, b, a)
}
fn interp(start_color: u8, end_color: u8, scale: f64) -> u8 {
(end_color as f64 - start_color as f64).mul_add(scale, start_color as f64) as u8
}
}
impl ToString for Color {
fn to_string(&self) -> String {
if self.a() == 0 {
return String::from("transparent");
}
let data = self.data;
let mut color = format!("#{:x}", data);
color.remove(1);
color.remove(1);
color
}
}
impl From<&str> for Color {
fn from(s: &str) -> Color {
let clean_hex = s.trim_start_matches('#');
match clean_hex.len() {
6 | 8 => {
let mut x = match u32::from_str_radix(&clean_hex, 16) {
Ok(x) => x,
Err(_) => 0,
};
if clean_hex.len() == 6 {
x |= 0xFF_000_000;
}
Color { data: x }
}
_ => Color { data: 0 },
}
}
}
impl From<String> for Color {
fn from(s: String) -> Color {
Color::from(s.as_str())
}
}
impl From<Color> for femtovg::Color {
fn from(src: Color) -> femtovg::Color {
femtovg::Color::rgba(src.r(), src.g(), src.b(), src.a())
}
}
impl PartialEq for Color {
fn eq(&self, other: &Color) -> bool {
self.r() == other.r() && self.g() == other.g() && self.b() == other.b()
}
}
impl std::fmt::Debug for Color {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"rgba({}, {}, {} {})",
self.r(),
self.g(),
self.b(),
self.a()
)
}
}
impl Default for Color {
fn default() -> Self {
Color::rgba(0, 0, 0, 0)
}
}
impl Color {
pub fn black() -> Self {
Self { data: 0xFF000000 }
}
pub fn white() -> Self {
Self { data: 0xFFFFFFFF }
}
pub fn red() -> Self {
Self { data: 0xFFFF0000 }
}
pub fn green() -> Self {
Self { data: 0xFF00FF00 }
}
pub fn blue() -> Self {
Self { data: 0xFF0000FF }
}
pub fn yellow() -> Self {
Self { data: 0xFFFFFF00 }
}
}
fn hue(mut h: f32, m1: f32, m2: f32) -> f32 {
if h < 0.0 {
h += 1.0;
}
if h > 1.0 {
h -= 1.0;
}
if h < 1.0 / 6.0 {
return m1 + (m2 - m1) * h * 6.0;
}
if h < 3.0 / 6.0 {
return m2;
}
if h < 4.0 / 6.0 {
return m1 + (m2 - m1) * (2.0 / 3.0 - h) * 6.0;
}
m1
}