#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Color {
pub r: f32,
pub g: f32,
pub b: f32,
pub a: f32,
}
impl Color {
pub const TRANSPARENT: Color = Color::rgba(0.0, 0.0, 0.0, 0.0);
pub const WHITE: Color = Color::rgb(1.0, 1.0, 1.0);
pub const BLACK: Color = Color::rgb(0.0, 0.0, 0.0);
pub const BLUE: Color = Color::hex(0x0A84FF);
pub const GREEN: Color = Color::hex(0x34C759);
pub const RED: Color = Color::hex(0xFF3B30);
pub const ORANGE: Color = Color::hex(0xFF9500);
pub const YELLOW: Color = Color::hex(0xFFCC00);
pub const PINK: Color = Color::hex(0xFF2D6F);
pub const PURPLE: Color = Color::hex(0xAF52DE);
pub const TEAL: Color = Color::hex(0x30B0C7);
pub const INDIGO: Color = Color::hex(0x5856D6);
pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
Self { r, g, b, a }
}
pub const fn rgb(r: f32, g: f32, b: f32) -> Self {
Self { r, g, b, a: 1.0 }
}
pub const fn hex(rgb: u32) -> Self {
Self::rgb(
((rgb >> 16) & 255) as f32 / 255.0,
((rgb >> 8) & 255) as f32 / 255.0,
(rgb & 255) as f32 / 255.0,
)
}
pub const fn gray(level: f32) -> Self {
Self::rgb(level, level, level)
}
pub const fn black(alpha: f32) -> Self {
Self::rgba(0.0, 0.0, 0.0, alpha)
}
pub const fn white(alpha: f32) -> Self {
Self::rgba(1.0, 1.0, 1.0, alpha)
}
pub const fn alpha(self, alpha: f32) -> Self {
Self { a: alpha, ..self }
}
pub fn fade(self, factor: f32) -> Self {
Self { a: self.a * factor, ..self }
}
pub fn lerp(self, other: Color, t: f32) -> Color {
Color {
r: self.r + (other.r - self.r) * t,
g: self.g + (other.g - self.g) * t,
b: self.b + (other.b - self.b) * t,
a: self.a + (other.a - self.a) * t,
}
}
pub fn luma(self) -> f32 {
0.299 * self.r + 0.587 * self.g + 0.114 * self.b
}
pub fn to_array(self) -> [f32; 4] {
[self.r, self.g, self.b, self.a]
}
pub fn to_hex(self) -> u32 {
let byte = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as u32;
(byte(self.r) << 16) | (byte(self.g) << 8) | byte(self.b)
}
pub fn from_hsv(hue: f32, saturation: f32, value: f32) -> Color {
let (s, v) = (saturation, value);
let h = hue.rem_euclid(1.0) * 6.0;
let i = h.floor();
let f = h - i;
let (p, q, t) = (v * (1.0 - s), v * (1.0 - s * f), v * (1.0 - s * (1.0 - f)));
match i as i32 {
0 => Color::rgb(v, t, p),
1 => Color::rgb(q, v, p),
2 => Color::rgb(p, v, t),
3 => Color::rgb(p, q, v),
4 => Color::rgb(t, p, v),
_ => Color::rgb(v, p, q),
}
}
pub fn to_hsv(self) -> (f32, f32, f32) {
let max = self.r.max(self.g).max(self.b);
let min = self.r.min(self.g).min(self.b);
let d = max - min;
let h = if d < 1e-5 {
0.0
} else if max == self.r {
((self.g - self.b) / d).rem_euclid(6.0) / 6.0
} else if max == self.g {
((self.b - self.r) / d + 2.0) / 6.0
} else {
((self.r - self.g) / d + 4.0) / 6.0
};
(h, if max < 1e-5 { 0.0 } else { d / max }, max)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn hsv_round_trip() {
for c in [Color::BLUE, Color::ORANGE, Color::PINK, Color::GREEN, Color::gray(0.4)] {
let (h, s, v) = c.to_hsv();
assert_eq!(Color::from_hsv(h, s, v).to_hex(), c.to_hex());
}
}
}