1use gpui::Hsla;
2use serde::Deserialize;
3
4use crate::{
5 AccentContent, amber, blue, cyan, gold, grass, indigo, iris, jade, lime, orange, pink, purple,
6 tomato, try_parse_color,
7};
8
9/// A collection of colors that are used to color indent aware lines in the editor.
10#[derive(Clone, Debug, Deserialize, PartialEq)]
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 /// Returns the set of dark accent colors.
24 pub fn dark() -> Self {
25 Self(vec![
26 blue().dark().step_9(),
27 orange().dark().step_9(),
28 pink().dark().step_9(),
29 lime().dark().step_9(),
30 purple().dark().step_9(),
31 amber().dark().step_9(),
32 jade().dark().step_9(),
33 tomato().dark().step_9(),
34 cyan().dark().step_9(),
35 gold().dark().step_9(),
36 grass().dark().step_9(),
37 indigo().dark().step_9(),
38 iris().dark().step_9(),
39 ])
40 }
41
42 /// Returns the set of light accent colors.
43 pub fn light() -> Self {
44 Self(vec![
45 blue().light().step_9(),
46 orange().light().step_9(),
47 pink().light().step_9(),
48 lime().light().step_9(),
49 purple().light().step_9(),
50 amber().light().step_9(),
51 jade().light().step_9(),
52 tomato().light().step_9(),
53 cyan().light().step_9(),
54 gold().light().step_9(),
55 grass().light().step_9(),
56 indigo().light().step_9(),
57 iris().light().step_9(),
58 ])
59 }
60}
61
62impl AccentColors {
63 /// Returns the color for the given index.
64 pub fn color_for_index(&self, index: u32) -> Hsla {
65 self.0[index as usize % self.0.len()]
66 }
67
68 /// Merges the given accent colors into this [`AccentColors`] instance.
69 pub fn merge(&mut self, accent_colors: &[AccentContent]) {
70 if accent_colors.is_empty() {
71 return;
72 }
73
74 let colors = accent_colors
75 .iter()
76 .filter_map(|accent_color| {
77 accent_color
78 .0
79 .as_ref()
80 .and_then(|color| try_parse_color(color).ok())
81 })
82 .collect::<Vec<_>>();
83
84 if !colors.is_empty() {
85 self.0 = colors;
86 }
87 }
88}