accents.rs

 1use gpui::Hsla;
 2use serde_derive::Deserialize;
 3
 4use crate::{
 5    amber, blue, cyan, gold, grass, indigo, iris, jade, lime, orange, pink, purple, tomato,
 6    try_parse_color, AccentContent,
 7};
 8
 9/// A collection of colors that are used to color indent aware lines in the editor.
10#[derive(Clone, Deserialize)]
11pub struct AccentColors(pub Vec<Hsla>);
12
13impl Default for AccentColors {
14    /// Don't use this!
15    /// We have to have a default to be `[refineable::Refinable]`.
16    /// TODO "Find a way to not need this for Refinable"
17    fn default() -> Self {
18        Self::dark()
19    }
20}
21
22impl AccentColors {
23    pub fn dark() -> Self {
24        Self(vec![
25            blue().dark().step_9(),
26            orange().dark().step_9(),
27            pink().dark().step_9(),
28            lime().dark().step_9(),
29            purple().dark().step_9(),
30            amber().dark().step_9(),
31            jade().dark().step_9(),
32            tomato().dark().step_9(),
33            cyan().dark().step_9(),
34            gold().dark().step_9(),
35            grass().dark().step_9(),
36            indigo().dark().step_9(),
37            iris().dark().step_9(),
38        ])
39    }
40
41    pub fn light() -> Self {
42        Self(vec![
43            blue().light().step_9(),
44            orange().light().step_9(),
45            pink().light().step_9(),
46            lime().light().step_9(),
47            purple().light().step_9(),
48            amber().light().step_9(),
49            jade().light().step_9(),
50            tomato().light().step_9(),
51            cyan().light().step_9(),
52            gold().light().step_9(),
53            grass().light().step_9(),
54            indigo().light().step_9(),
55            iris().light().step_9(),
56        ])
57    }
58}
59
60impl AccentColors {
61    pub fn color_for_index(&self, index: u32) -> Hsla {
62        self.0[index as usize % self.0.len()]
63    }
64
65    /// Merges the given accent colors into this [`AccentColors`] instance.
66    pub fn merge(&mut self, accent_colors: &[AccentContent]) {
67        if accent_colors.is_empty() {
68            return;
69        }
70
71        let colors = accent_colors
72            .iter()
73            .filter_map(|accent_color| {
74                accent_color
75                    .0
76                    .as_ref()
77                    .and_then(|color| try_parse_color(color).ok())
78            })
79            .collect::<Vec<_>>();
80
81        if !colors.is_empty() {
82            self.0 = colors;
83        }
84    }
85}