1#[derive(Clone, Copy, Debug, PartialEq)]
9pub struct Color {
10 pub r: f32,
12 pub g: f32,
14 pub b: f32,
16 pub a: f32,
18}
19
20impl Color {
21 pub const TRANSPARENT: Color = Color::rgba(0.0, 0.0, 0.0, 0.0);
23 pub const WHITE: Color = Color::rgb(1.0, 1.0, 1.0);
25 pub const BLACK: Color = Color::rgb(0.0, 0.0, 0.0);
27 pub const BLUE: Color = Color::hex(0x0A84FF);
29 pub const GREEN: Color = Color::hex(0x34C759);
31 pub const RED: Color = Color::hex(0xFF3B30);
33 pub const ORANGE: Color = Color::hex(0xFF9500);
35 pub const YELLOW: Color = Color::hex(0xFFCC00);
37 pub const PINK: Color = Color::hex(0xFF2D6F);
39 pub const PURPLE: Color = Color::hex(0xAF52DE);
41 pub const TEAL: Color = Color::hex(0x30B0C7);
43 pub const INDIGO: Color = Color::hex(0x5856D6);
45
46 pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
48 Self { r, g, b, a }
49 }
50 pub const fn rgb(r: f32, g: f32, b: f32) -> Self {
52 Self { r, g, b, a: 1.0 }
53 }
54 pub const fn hex(rgb: u32) -> Self {
61 Self::rgb(
62 ((rgb >> 16) & 255) as f32 / 255.0,
63 ((rgb >> 8) & 255) as f32 / 255.0,
64 (rgb & 255) as f32 / 255.0,
65 )
66 }
67 pub const fn gray(level: f32) -> Self {
69 Self::rgb(level, level, level)
70 }
71 pub const fn black(alpha: f32) -> Self {
73 Self::rgba(0.0, 0.0, 0.0, alpha)
74 }
75 pub const fn white(alpha: f32) -> Self {
77 Self::rgba(1.0, 1.0, 1.0, alpha)
78 }
79 pub const fn alpha(self, alpha: f32) -> Self {
81 Self { a: alpha, ..self }
82 }
83 pub fn fade(self, factor: f32) -> Self {
85 Self { a: self.a * factor, ..self }
86 }
87 pub fn lerp(self, other: Color, t: f32) -> Color {
89 Color {
90 r: self.r + (other.r - self.r) * t,
91 g: self.g + (other.g - self.g) * t,
92 b: self.b + (other.b - self.b) * t,
93 a: self.a + (other.a - self.a) * t,
94 }
95 }
96 pub fn luma(self) -> f32 {
98 0.299 * self.r + 0.587 * self.g + 0.114 * self.b
99 }
100 pub fn to_array(self) -> [f32; 4] {
102 [self.r, self.g, self.b, self.a]
103 }
104 pub fn to_hex(self) -> u32 {
106 let byte = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as u32;
107 (byte(self.r) << 16) | (byte(self.g) << 8) | byte(self.b)
108 }
109
110 pub fn from_hsv(hue: f32, saturation: f32, value: f32) -> Color {
112 let (s, v) = (saturation, value);
113 let h = hue.rem_euclid(1.0) * 6.0;
114 let i = h.floor();
115 let f = h - i;
116 let (p, q, t) = (v * (1.0 - s), v * (1.0 - s * f), v * (1.0 - s * (1.0 - f)));
117 match i as i32 {
118 0 => Color::rgb(v, t, p),
119 1 => Color::rgb(q, v, p),
120 2 => Color::rgb(p, v, t),
121 3 => Color::rgb(p, q, v),
122 4 => Color::rgb(t, p, v),
123 _ => Color::rgb(v, p, q),
124 }
125 }
126
127 pub fn to_hsv(self) -> (f32, f32, f32) {
129 let max = self.r.max(self.g).max(self.b);
130 let min = self.r.min(self.g).min(self.b);
131 let d = max - min;
132 let h = if d < 1e-5 {
133 0.0
134 } else if max == self.r {
135 ((self.g - self.b) / d).rem_euclid(6.0) / 6.0
136 } else if max == self.g {
137 ((self.b - self.r) / d + 2.0) / 6.0
138 } else {
139 ((self.r - self.g) / d + 4.0) / 6.0
140 };
141 (h, if max < 1e-5 { 0.0 } else { d / max }, max)
142 }
143}
144
145#[cfg(test)]
146mod tests {
147 use super::*;
148
149 #[test]
150 fn hsv_round_trip() {
151 for c in [Color::BLUE, Color::ORANGE, Color::PINK, Color::GREEN, Color::gray(0.4)] {
152 let (h, s, v) = c.to_hsv();
153 assert_eq!(Color::from_hsv(h, s, v).to_hex(), c.to_hex());
154 }
155 }
156}