Skip to main content

rust_glass/
color.rs

1//! Colors.
2//!
3//! Components are not premultiplied and are stored in the sRGB encoding.
4//! Blending happens in that same space, like desktop compositors and Core
5//! Animation do.
6
7/// An RGBA color with components from 0 to 1, in the sRGB encoding.
8#[derive(Clone, Copy, Debug, PartialEq)]
9pub struct Color {
10    /// Red, 0 to 1.
11    pub r: f32,
12    /// Green, 0 to 1.
13    pub g: f32,
14    /// Blue, 0 to 1.
15    pub b: f32,
16    /// Opacity, 0 to 1.
17    pub a: f32,
18}
19
20impl Color {
21    /// Fully transparent black.
22    pub const TRANSPARENT: Color = Color::rgba(0.0, 0.0, 0.0, 0.0);
23    /// Opaque white.
24    pub const WHITE: Color = Color::rgb(1.0, 1.0, 1.0);
25    /// Opaque black.
26    pub const BLACK: Color = Color::rgb(0.0, 0.0, 0.0);
27    /// The system blue. The default accent color.
28    pub const BLUE: Color = Color::hex(0x0A84FF);
29    /// The system green.
30    pub const GREEN: Color = Color::hex(0x34C759);
31    /// The system red.
32    pub const RED: Color = Color::hex(0xFF3B30);
33    /// The system orange.
34    pub const ORANGE: Color = Color::hex(0xFF9500);
35    /// The system yellow.
36    pub const YELLOW: Color = Color::hex(0xFFCC00);
37    /// The system pink.
38    pub const PINK: Color = Color::hex(0xFF2D6F);
39    /// The system purple.
40    pub const PURPLE: Color = Color::hex(0xAF52DE);
41    /// The system teal.
42    pub const TEAL: Color = Color::hex(0x30B0C7);
43    /// The system indigo.
44    pub const INDIGO: Color = Color::hex(0x5856D6);
45
46    /// Creates a color from red, green, blue and opacity.
47    pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
48        Self { r, g, b, a }
49    }
50    /// Creates an opaque color from red, green and blue.
51    pub const fn rgb(r: f32, g: f32, b: f32) -> Self {
52        Self { r, g, b, a: 1.0 }
53    }
54    /// Creates an opaque color from a `0xRRGGBB` value.
55    ///
56    /// ```
57    /// # use rust_glass::Color;
58    /// assert_eq!(Color::hex(0xFF0000), Color::rgb(1.0, 0.0, 0.0));
59    /// ```
60    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    /// Creates an opaque gray, from 0 (black) to 1 (white).
68    pub const fn gray(level: f32) -> Self {
69        Self::rgb(level, level, level)
70    }
71    /// Creates black with the given opacity.
72    pub const fn black(alpha: f32) -> Self {
73        Self::rgba(0.0, 0.0, 0.0, alpha)
74    }
75    /// Creates white with the given opacity.
76    pub const fn white(alpha: f32) -> Self {
77        Self::rgba(1.0, 1.0, 1.0, alpha)
78    }
79    /// Returns the color with its opacity replaced.
80    pub const fn alpha(self, alpha: f32) -> Self {
81        Self { a: alpha, ..self }
82    }
83    /// Returns the color with its opacity multiplied by `factor`.
84    pub fn fade(self, factor: f32) -> Self {
85        Self { a: self.a * factor, ..self }
86    }
87    /// Mixes towards `other`: `t` = 0 gives `self`, 1 gives `other`.
88    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    /// Returns the perceived brightness, from 0 to 1.
97    pub fn luma(self) -> f32 {
98        0.299 * self.r + 0.587 * self.g + 0.114 * self.b
99    }
100    /// Returns `[r, g, b, a]`.
101    pub fn to_array(self) -> [f32; 4] {
102        [self.r, self.g, self.b, self.a]
103    }
104    /// Returns the color as `0xRRGGBB`, without opacity.
105    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    /// Creates an opaque color from hue, saturation and value, each 0 to 1.
111    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    /// Returns hue, saturation and value, each 0 to 1.
128    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}