color.rs

  1#![allow(dead_code)]
  2
  3use serde::de::{self, Deserialize, Deserializer, Visitor};
  4use smallvec::SmallVec;
  5use std::fmt;
  6use std::{num::ParseIntError, ops::Range};
  7
  8pub fn rgb<C: From<Rgba>>(hex: u32) -> C {
  9    let r = ((hex >> 16) & 0xFF) as f32 / 255.0;
 10    let g = ((hex >> 8) & 0xFF) as f32 / 255.0;
 11    let b = (hex & 0xFF) as f32 / 255.0;
 12    Rgba { r, g, b, a: 1.0 }.into()
 13}
 14
 15#[derive(Clone, Copy, Default, Debug)]
 16pub struct Rgba {
 17    pub r: f32,
 18    pub g: f32,
 19    pub b: f32,
 20    pub a: f32,
 21}
 22
 23struct RgbaVisitor;
 24
 25impl<'de> Visitor<'de> for RgbaVisitor {
 26    type Value = Rgba;
 27
 28    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
 29        formatter.write_str("a string in the format #rrggbb or #rrggbbaa")
 30    }
 31
 32    fn visit_str<E: de::Error>(self, value: &str) -> Result<Rgba, E> {
 33        if value.len() == 7 || value.len() == 9 {
 34            let r = u8::from_str_radix(&value[1..3], 16).unwrap() as f32 / 255.0;
 35            let g = u8::from_str_radix(&value[3..5], 16).unwrap() as f32 / 255.0;
 36            let b = u8::from_str_radix(&value[5..7], 16).unwrap() as f32 / 255.0;
 37            let a = if value.len() == 9 {
 38                u8::from_str_radix(&value[7..9], 16).unwrap() as f32 / 255.0
 39            } else {
 40                1.0
 41            };
 42            Ok(Rgba { r, g, b, a })
 43        } else {
 44            Err(E::custom(
 45                "Bad format for RGBA. Expected #rrggbb or #rrggbbaa.",
 46            ))
 47        }
 48    }
 49}
 50
 51impl<'de> Deserialize<'de> for Rgba {
 52    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
 53        deserializer.deserialize_str(RgbaVisitor)
 54    }
 55}
 56
 57pub trait Lerp {
 58    fn lerp(&self, level: f32) -> Hsla;
 59}
 60
 61impl Lerp for Range<Hsla> {
 62    fn lerp(&self, level: f32) -> Hsla {
 63        let level = level.clamp(0., 1.);
 64        Hsla {
 65            h: self.start.h + (level * (self.end.h - self.start.h)),
 66            s: self.start.s + (level * (self.end.s - self.start.s)),
 67            l: self.start.l + (level * (self.end.l - self.start.l)),
 68            a: self.start.a + (level * (self.end.a - self.start.a)),
 69        }
 70    }
 71}
 72
 73impl From<gpui::color::Color> for Rgba {
 74    fn from(value: gpui::color::Color) -> Self {
 75        Self {
 76            r: value.0.r as f32 / 255.0,
 77            g: value.0.g as f32 / 255.0,
 78            b: value.0.b as f32 / 255.0,
 79            a: value.0.a as f32 / 255.0,
 80        }
 81    }
 82}
 83
 84impl From<Hsla> for Rgba {
 85    fn from(color: Hsla) -> Self {
 86        let h = color.h;
 87        let s = color.s;
 88        let l = color.l;
 89
 90        let c = (1.0 - (2.0 * l - 1.0).abs()) * s;
 91        let x = c * (1.0 - ((h * 6.0) % 2.0 - 1.0).abs());
 92        let m = l - c / 2.0;
 93        let cm = c + m;
 94        let xm = x + m;
 95
 96        let (r, g, b) = match (h * 6.0).floor() as i32 {
 97            0 | 6 => (cm, xm, m),
 98            1 => (xm, cm, m),
 99            2 => (m, cm, xm),
100            3 => (m, xm, cm),
101            4 => (xm, m, cm),
102            _ => (cm, m, xm),
103        };
104
105        Rgba {
106            r,
107            g,
108            b,
109            a: color.a,
110        }
111    }
112}
113
114impl TryFrom<&'_ str> for Rgba {
115    type Error = ParseIntError;
116
117    fn try_from(value: &'_ str) -> Result<Self, Self::Error> {
118        let r = u8::from_str_radix(&value[1..3], 16)? as f32 / 255.0;
119        let g = u8::from_str_radix(&value[3..5], 16)? as f32 / 255.0;
120        let b = u8::from_str_radix(&value[5..7], 16)? as f32 / 255.0;
121        let a = if value.len() > 7 {
122            u8::from_str_radix(&value[7..9], 16)? as f32 / 255.0
123        } else {
124            1.0
125        };
126
127        Ok(Rgba { r, g, b, a })
128    }
129}
130
131impl Into<gpui::color::Color> for Rgba {
132    fn into(self) -> gpui::color::Color {
133        gpui::color::rgba(self.r, self.g, self.b, self.a)
134    }
135}
136
137#[derive(Default, Copy, Clone, Debug, PartialEq)]
138pub struct Hsla {
139    pub h: f32,
140    pub s: f32,
141    pub l: f32,
142    pub a: f32,
143}
144
145pub fn hsla(h: f32, s: f32, l: f32, a: f32) -> Hsla {
146    Hsla {
147        h: h.clamp(0., 1.),
148        s: s.clamp(0., 1.),
149        l: l.clamp(0., 1.),
150        a: a.clamp(0., 1.),
151    }
152}
153
154pub fn black() -> Hsla {
155    Hsla {
156        h: 0.,
157        s: 0.,
158        l: 0.,
159        a: 1.,
160    }
161}
162
163pub fn white() -> Hsla {
164    Hsla {
165        h: 0.,
166        s: 0.,
167        l: 1.,
168        a: 1.,
169    }
170}
171
172impl From<Rgba> for Hsla {
173    fn from(color: Rgba) -> Self {
174        let r = color.r;
175        let g = color.g;
176        let b = color.b;
177
178        let max = r.max(g.max(b));
179        let min = r.min(g.min(b));
180        let delta = max - min;
181
182        let l = (max + min) / 2.0;
183        let s = if l == 0.0 || l == 1.0 {
184            0.0
185        } else if l < 0.5 {
186            delta / (2.0 * l)
187        } else {
188            delta / (2.0 - 2.0 * l)
189        };
190
191        let h = if delta == 0.0 {
192            0.0
193        } else if max == r {
194            ((g - b) / delta).rem_euclid(6.0) / 6.0
195        } else if max == g {
196            ((b - r) / delta + 2.0) / 6.0
197        } else {
198            ((r - g) / delta + 4.0) / 6.0
199        };
200
201        Hsla {
202            h,
203            s,
204            l,
205            a: color.a,
206        }
207    }
208}
209
210impl Hsla {
211    /// Scales the saturation and lightness by the given values, clamping at 1.0.
212    pub fn scale_sl(mut self, s: f32, l: f32) -> Self {
213        self.s = (self.s * s).clamp(0., 1.);
214        self.l = (self.l * l).clamp(0., 1.);
215        self
216    }
217
218    /// Increases the saturation of the color by a certain amount, with a max
219    /// value of 1.0.
220    pub fn saturate(mut self, amount: f32) -> Self {
221        self.s += amount;
222        self.s = self.s.clamp(0.0, 1.0);
223        self
224    }
225
226    /// Decreases the saturation of the color by a certain amount, with a min
227    /// value of 0.0.
228    pub fn desaturate(mut self, amount: f32) -> Self {
229        self.s -= amount;
230        self.s = self.s.max(0.0);
231        if self.s < 0.0 {
232            self.s = 0.0;
233        }
234        self
235    }
236
237    /// Brightens the color by increasing the lightness by a certain amount,
238    /// with a max value of 1.0.
239    pub fn brighten(mut self, amount: f32) -> Self {
240        self.l += amount;
241        self.l = self.l.clamp(0.0, 1.0);
242        self
243    }
244
245    /// Darkens the color by decreasing the lightness by a certain amount,
246    /// with a max value of 0.0.
247    pub fn darken(mut self, amount: f32) -> Self {
248        self.l -= amount;
249        self.l = self.l.clamp(0.0, 1.0);
250        self
251    }
252}
253
254impl From<gpui::color::Color> for Hsla {
255    fn from(value: gpui::color::Color) -> Self {
256        Rgba::from(value).into()
257    }
258}
259
260impl Into<gpui::color::Color> for Hsla {
261    fn into(self) -> gpui::color::Color {
262        Rgba::from(self).into()
263    }
264}
265
266impl<'de> Deserialize<'de> for Hsla {
267    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
268    where
269        D: Deserializer<'de>,
270    {
271        // First, deserialize it into Rgba
272        let rgba = Rgba::deserialize(deserializer)?;
273
274        // Then, use the From<Rgba> for Hsla implementation to convert it
275        Ok(Hsla::from(rgba))
276    }
277}
278
279pub struct ColorScale {
280    colors: SmallVec<[Hsla; 2]>,
281    positions: SmallVec<[f32; 2]>,
282}
283
284pub fn scale<I, C>(colors: I) -> ColorScale
285where
286    I: IntoIterator<Item = C>,
287    C: Into<Hsla>,
288{
289    let mut scale = ColorScale {
290        colors: colors.into_iter().map(Into::into).collect(),
291        positions: SmallVec::new(),
292    };
293    let num_colors: f32 = scale.colors.len() as f32 - 1.0;
294    scale.positions = (0..scale.colors.len())
295        .map(|i| i as f32 / num_colors)
296        .collect();
297    scale
298}
299
300impl ColorScale {
301    fn at(&self, t: f32) -> Hsla {
302        // Ensure that the input is within [0.0, 1.0]
303        debug_assert!(
304            0.0 <= t && t <= 1.0,
305            "t value {} is out of range. Expected value in range 0.0 to 1.0",
306            t
307        );
308
309        let position = match self
310            .positions
311            .binary_search_by(|a| a.partial_cmp(&t).unwrap())
312        {
313            Ok(index) | Err(index) => index,
314        };
315        let lower_bound = position.saturating_sub(1);
316        let upper_bound = position.min(self.colors.len() - 1);
317        let lower_color = &self.colors[lower_bound];
318        let upper_color = &self.colors[upper_bound];
319
320        match upper_bound.checked_sub(lower_bound) {
321            Some(0) | None => *lower_color,
322            Some(_) => {
323                let interval_t = (t - self.positions[lower_bound])
324                    / (self.positions[upper_bound] - self.positions[lower_bound]);
325                let h = lower_color.h + interval_t * (upper_color.h - lower_color.h);
326                let s = lower_color.s + interval_t * (upper_color.s - lower_color.s);
327                let l = lower_color.l + interval_t * (upper_color.l - lower_color.l);
328                let a = lower_color.a + interval_t * (upper_color.a - lower_color.a);
329                Hsla { h, s, l, a }
330            }
331        }
332    }
333}