1pub struct Color;
2
3impl Color {
4 pub fn from_hex(hex: &str) -> u32 {
5 u32::from_str_radix(hex, 16).unwrap_or(0xFFFFFF)
6 }
7
8 pub fn from_rgb(r: u8, g: u8, b: u8) -> u32 {
9 let (r, g, b) = (r as u32, g as u32, b as u32);
10 (r << 16) | (g << 8) | b
11 }
12
13 pub fn from_hsl(h: f32, s: f32, l: f32) -> u32 {
14 let c = (1.0 - (2.0 * l - 1.0).abs()) * s;
15 let x = c * (1.0 - ((h / 60.0) % 2.0 - 1.0).abs());
16 let m = l - c / 2.0;
17
18 let (r, g, b) = if h < 60.0 {
19 (c, x, 0.0)
20 } else if h < 120.0 {
21 (x, c, 0.0)
22 } else if h < 180.0 {
23 (0.0, c, x)
24 } else if h < 240.0 {
25 (0.0, x, c)
26 } else if h < 300.0 {
27 (x, 0.0, c)
28 } else {
29 (c, 0.0, x)
30 };
31
32 ((r + m) * 255.0) as u32 * 0x10000 + ((g + m) * 255.0) as u32 * 0x100 + ((b + m) * 255.0) as u32
33 }
34}