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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
use palette::{
rgb::{channels::Rgba, Rgb},
Alpha, IntoColor, IntoComponent, Lab, Srgba,
};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct Color(palette::Srgba<u8>);
#[derive(Clone, Debug, Copy)]
pub struct LightnessTone(f32);
impl LightnessTone {
#[inline]
pub fn new(tone: f32) -> Self { Self(tone.clamp(0., 1.0)) }
}
impl Color {
#[inline]
pub const fn new(red: u8, green: u8, blue: u8, alpha: u8) -> Self {
let color = Alpha {
alpha,
color: Rgb {
red,
green,
blue,
standard: std::marker::PhantomData,
},
};
Self(color)
}
#[inline]
pub const fn from_rgb(r: u8, g: u8, b: u8) -> Self { Self::new(r, g, b, 255) }
#[inline]
pub fn from_f32_rgb(red: f32, green: f32, blue: f32, alpha: f32) -> Self {
let color = Alpha {
alpha,
color: Rgb {
red,
green,
blue,
standard: std::marker::PhantomData,
},
};
Self(color.into_format())
}
#[inline]
pub fn from_u32(rgba: u32) -> Self { Self(palette::Srgba::from_u32::<Rgba>(rgba)) }
#[inline]
pub fn into_u32(self) -> u32 { self.0.into_u32::<Rgba>() }
#[inline]
pub fn with_alpha(mut self, alpha: f32) -> Self {
self.0.alpha = alpha.into_component();
self
}
#[inline]
pub fn apply_alpha(mut self, alpha: f32) -> Self {
let base: f32 = self.0.alpha.into_component();
self.0.alpha = (base * alpha).into_component();
self
}
#[inline]
pub fn with_lightness(self, l: LightnessTone) -> Self {
let color: Srgba = self.0.into_format();
let mut lab: Lab = color.into_color();
lab.l = (l.0 * 100.).clamp(0., 100.);
let rgba: Srgba = lab.into_color();
Color(rgba.into_format())
}
#[inline]
pub fn into_components(self) -> [u8; 4] {
let (r, g, b, a) = self.0.into_components();
[r, g, b, a]
}
#[inline]
pub fn into_f32_components(self) -> [f32; 4] {
let (r, g, b, a) = self.0.into_components();
[
r.into_component(),
g.into_component(),
b.into_component(),
a.into_component(),
]
}
}
impl Color {
pub const ALICEBLUE: Color = Self::from_rgb(240, 248, 255);
pub const ANTIQUEWHITE: Color = Self::from_rgb(250, 235, 215);
pub const AQUA: Color = Self::from_rgb(0, 255, 255);
pub const AQUAMARINE: Color = Self::from_rgb(127, 255, 212);
pub const AZURE: Color = Self::from_rgb(240, 255, 255);
pub const BEIGE: Color = Self::from_rgb(245, 245, 220);
pub const BISQUE: Color = Self::from_rgb(255, 228, 196);
pub const BLACK: Color = Self::from_rgb(0, 0, 0);
pub const BLANCHEDALMOND: Color = Self::from_rgb(255, 235, 205);
pub const BLUE: Color = Self::from_rgb(0, 0, 255);
pub const BLUEVIOLET: Color = Self::from_rgb(138, 43, 226);
pub const BROWN: Color = Self::from_rgb(165, 42, 42);
pub const BURLYWOOD: Color = Self::from_rgb(222, 184, 135);
pub const CADETBLUE: Color = Self::from_rgb(95, 158, 160);
pub const CHARTREUSE: Color = Self::from_rgb(127, 255, 0);
pub const CHOCOLATE: Color = Self::from_rgb(210, 105, 30);
pub const CORAL: Color = Self::from_rgb(255, 127, 80);
pub const CORNFLOWERBLUE: Color = Self::from_rgb(100, 149, 237);
pub const CORNSILK: Color = Self::from_rgb(255, 248, 220);
pub const CRIMSON: Color = Self::from_rgb(220, 20, 60);
pub const CYAN: Color = Self::from_rgb(0, 255, 255);
pub const DARKBLUE: Color = Self::from_rgb(0, 0, 139);
pub const DARKCYAN: Color = Self::from_rgb(0, 139, 139);
pub const DARKGOLDENROD: Color = Self::from_rgb(184, 134, 11);
pub const DARKGRAY: Color = Self::from_rgb(169, 169, 169);
pub const DARKGREEN: Color = Self::from_rgb(0, 100, 0);
pub const DARKGREY: Color = Self::from_rgb(169, 169, 169);
pub const DARKKHAKI: Color = Self::from_rgb(189, 183, 107);
pub const DARKMAGENTA: Color = Self::from_rgb(139, 0, 139);
pub const DARKOLIVEGREEN: Color = Self::from_rgb(85, 107, 47);
pub const DARKORANGE: Color = Self::from_rgb(255, 140, 0);
pub const DARKORCHID: Color = Self::from_rgb(153, 50, 204);
pub const DARKRED: Color = Self::from_rgb(139, 0, 0);
pub const DARKSALMON: Color = Self::from_rgb(233, 150, 122);
pub const DARKSEAGREEN: Color = Self::from_rgb(143, 188, 143);
pub const DARKSLATEBLUE: Color = Self::from_rgb(72, 61, 139);
pub const DARKSLATEGRAY: Color = Self::from_rgb(47, 79, 79);
pub const DARKSLATEGREY: Color = Self::from_rgb(47, 79, 79);
pub const DARKTURQUOISE: Color = Self::from_rgb(0, 206, 209);
pub const DARKVIOLET: Color = Self::from_rgb(148, 0, 211);
pub const DEEPPINK: Color = Self::from_rgb(255, 20, 147);
pub const DEEPSKYBLUE: Color = Self::from_rgb(0, 191, 255);
pub const DIMGRAY: Color = Self::from_rgb(105, 105, 105);
pub const DIMGREY: Color = Self::from_rgb(105, 105, 105);
pub const DODGERBLUE: Color = Self::from_rgb(30, 144, 255);
pub const FIREBRICK: Color = Self::from_rgb(178, 34, 34);
pub const FLORALWHITE: Color = Self::from_rgb(255, 250, 240);
pub const FORESTGREEN: Color = Self::from_rgb(34, 139, 34);
pub const FUCHSIA: Color = Self::from_rgb(255, 0, 255);
pub const GAINSBORO: Color = Self::from_rgb(220, 220, 220);
pub const GHOSTWHITE: Color = Self::from_rgb(248, 248, 255);
pub const GOLD: Color = Self::from_rgb(255, 215, 0);
pub const GOLDENROD: Color = Self::from_rgb(218, 165, 32);
pub const GRAY: Color = Self::from_rgb(128, 128, 128);
pub const GREEN: Color = Self::from_rgb(0, 128, 0);
pub const GREENYELLOW: Color = Self::from_rgb(173, 255, 47);
pub const GREY: Color = Self::from_rgb(128, 128, 128);
pub const HONEYDEW: Color = Self::from_rgb(240, 255, 240);
pub const HOTPINK: Color = Self::from_rgb(255, 105, 180);
pub const INDIANRED: Color = Self::from_rgb(205, 92, 92);
pub const INDIGO: Color = Self::from_rgb(75, 0, 130);
pub const IVORY: Color = Self::from_rgb(255, 255, 240);
pub const KHAKI: Color = Self::from_rgb(240, 230, 140);
pub const LAVENDER: Color = Self::from_rgb(230, 230, 250);
pub const LAVENDERBLUSH: Color = Self::from_rgb(255, 240, 245);
pub const LAWNGREEN: Color = Self::from_rgb(124, 252, 0);
pub const LEMONCHIFFON: Color = Self::from_rgb(255, 250, 205);
pub const LIGHTBLUE: Color = Self::from_rgb(173, 216, 230);
pub const LIGHTCORAL: Color = Self::from_rgb(240, 128, 128);
pub const LIGHTCYAN: Color = Self::from_rgb(224, 255, 255);
pub const LIGHTGOLDENRODYELLOW: Color = Self::from_rgb(250, 250, 210);
pub const LIGHTGRAY: Color = Self::from_rgb(211, 211, 211);
pub const LIGHTGREEN: Color = Self::from_rgb(144, 238, 144);
pub const LIGHTGREY: Color = Self::from_rgb(211, 211, 211);
pub const LIGHTPINK: Color = Self::from_rgb(255, 182, 193);
pub const LIGHTSALMON: Color = Self::from_rgb(255, 160, 122);
pub const LIGHTSEAGREEN: Color = Self::from_rgb(32, 178, 170);
pub const LIGHTSKYBLUE: Color = Self::from_rgb(135, 206, 250);
pub const LIGHTSLATEGRAY: Color = Self::from_rgb(119, 136, 153);
pub const LIGHTSLATEGREY: Color = Self::from_rgb(119, 136, 153);
pub const LIGHTSTEELBLUE: Color = Self::from_rgb(176, 196, 222);
pub const LIGHTYELLOW: Color = Self::from_rgb(255, 255, 224);
pub const LIME: Color = Self::from_rgb(0, 255, 0);
pub const LIMEGREEN: Color = Self::from_rgb(50, 205, 50);
pub const LINEN: Color = Self::from_rgb(250, 240, 230);
pub const MAGENTA: Color = Self::from_rgb(255, 0, 255);
pub const MAROON: Color = Self::from_rgb(128, 0, 0);
pub const MEDIUMAQUAMARINE: Color = Self::from_rgb(102, 205, 170);
pub const MEDIUMBLUE: Color = Self::from_rgb(0, 0, 205);
pub const MEDIUMORCHID: Color = Self::from_rgb(186, 85, 211);
pub const MEDIUMPURPLE: Color = Self::from_rgb(147, 112, 219);
pub const MEDIUMSEAGREEN: Color = Self::from_rgb(60, 179, 113);
pub const MEDIUMSLATEBLUE: Color = Self::from_rgb(123, 104, 238);
pub const MEDIUMSPRINGGREEN: Color = Self::from_rgb(0, 250, 154);
pub const MEDIUMTURQUOISE: Color = Self::from_rgb(72, 209, 204);
pub const MEDIUMVIOLETRED: Color = Self::from_rgb(199, 21, 133);
pub const MIDNIGHTBLUE: Color = Self::from_rgb(25, 25, 112);
pub const MINTCREAM: Color = Self::from_rgb(245, 255, 250);
pub const MISTYROSE: Color = Self::from_rgb(255, 228, 225);
pub const MOCCASIN: Color = Self::from_rgb(255, 228, 181);
pub const NAVAJOWHITE: Color = Self::from_rgb(255, 222, 173);
pub const NAVY: Color = Self::from_rgb(0, 0, 128);
pub const OLDLACE: Color = Self::from_rgb(253, 245, 230);
pub const OLIVE: Color = Self::from_rgb(128, 128, 0);
pub const OLIVEDRAB: Color = Self::from_rgb(107, 142, 35);
pub const ORANGE: Color = Self::from_rgb(255, 165, 0);
pub const ORANGERED: Color = Self::from_rgb(255, 69, 0);
pub const ORCHID: Color = Self::from_rgb(218, 112, 214);
pub const PALEGOLDENROD: Color = Self::from_rgb(238, 232, 170);
pub const PALEGREEN: Color = Self::from_rgb(152, 251, 152);
pub const PALETURQUOISE: Color = Self::from_rgb(175, 238, 238);
pub const PALEVIOLETRED: Color = Self::from_rgb(219, 112, 147);
pub const PAPAYAWHIP: Color = Self::from_rgb(255, 239, 213);
pub const PEACHPUFF: Color = Self::from_rgb(255, 218, 185);
pub const PERU: Color = Self::from_rgb(205, 133, 63);
pub const PINK: Color = Self::from_rgb(255, 192, 203);
pub const PLUM: Color = Self::from_rgb(221, 160, 221);
pub const POWDERBLUE: Color = Self::from_rgb(176, 224, 230);
pub const PURPLE: Color = Self::from_rgb(128, 0, 128);
pub const RED: Color = Self::from_rgb(255, 0, 0);
pub const ROSYBROWN: Color = Self::from_rgb(188, 143, 143);
pub const ROYALBLUE: Color = Self::from_rgb(65, 105, 225);
pub const SADDLEBROWN: Color = Self::from_rgb(139, 69, 19);
pub const SALMON: Color = Self::from_rgb(250, 128, 114);
pub const SANDYBROWN: Color = Self::from_rgb(244, 164, 96);
pub const SEAGREEN: Color = Self::from_rgb(46, 139, 87);
pub const SEASHELL: Color = Self::from_rgb(255, 245, 238);
pub const SIENNA: Color = Self::from_rgb(160, 82, 45);
pub const SILVER: Color = Self::from_rgb(192, 192, 192);
pub const SKYBLUE: Color = Self::from_rgb(135, 206, 235);
pub const SLATEBLUE: Color = Self::from_rgb(106, 90, 205);
pub const SLATEGRAY: Color = Self::from_rgb(112, 128, 144);
pub const SLATEGREY: Color = Self::from_rgb(112, 128, 144);
pub const SNOW: Color = Self::from_rgb(255, 250, 250);
pub const SPRINGGREEN: Color = Self::from_rgb(0, 255, 127);
pub const STEELBLUE: Color = Self::from_rgb(70, 130, 180);
pub const TAN: Color = Self::from_rgb(210, 180, 140);
pub const TEAL: Color = Self::from_rgb(0, 128, 128);
pub const THISTLE: Color = Self::from_rgb(216, 191, 216);
pub const TOMATO: Color = Self::from_rgb(255, 99, 71);
pub const TURQUOISE: Color = Self::from_rgb(64, 224, 208);
pub const VIOLET: Color = Self::from_rgb(238, 130, 238);
pub const WHEAT: Color = Self::from_rgb(245, 222, 179);
pub const WHITE: Color = Self::from_rgb(255, 255, 255);
pub const WHITESMOKE: Color = Self::from_rgb(245, 245, 245);
pub const YELLOW: Color = Self::from_rgb(255, 255, 0);
pub const YELLOWGREEN: Color = Self::from_rgb(154, 205, 50);
pub const TRANSPARENT: Color = Self::new(0, 0, 0, 0);
}
impl std::ops::Deref for Color {
type Target = palette::Srgba<u8>;
#[inline]
fn deref(&self) -> &Self::Target { &self.0 }
}
impl std::ops::DerefMut for Color {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 }
}
impl From<palette::Srgb<u8>> for Color {
#[inline]
fn from(c: palette::Srgb<u8>) -> Self { Color(c.into()) }
}