scale.rs

  1use gpui2::{AppContext, Hsla, SharedString};
  2
  3use crate::{ActiveTheme, Appearance};
  4
  5/// A one-based step in a [`ColorScale`].
  6pub type ColorScaleStep = usize;
  7
  8pub struct ColorScale(Vec<Hsla>);
  9
 10impl FromIterator<Hsla> for ColorScale {
 11    fn from_iter<T: IntoIterator<Item = Hsla>>(iter: T) -> Self {
 12        Self(Vec::from_iter(iter))
 13    }
 14}
 15
 16impl std::ops::Index<ColorScaleStep> for ColorScale {
 17    type Output = Hsla;
 18
 19    fn index(&self, index: ColorScaleStep) -> &Self::Output {
 20        &self.0[index - 1]
 21    }
 22}
 23
 24pub struct ColorScales {
 25    pub gray: ColorScaleSet,
 26    pub mauve: ColorScaleSet,
 27    pub slate: ColorScaleSet,
 28    pub sage: ColorScaleSet,
 29    pub olive: ColorScaleSet,
 30    pub sand: ColorScaleSet,
 31    pub gold: ColorScaleSet,
 32    pub bronze: ColorScaleSet,
 33    pub brown: ColorScaleSet,
 34    pub yellow: ColorScaleSet,
 35    pub amber: ColorScaleSet,
 36    pub orange: ColorScaleSet,
 37    pub tomato: ColorScaleSet,
 38    pub red: ColorScaleSet,
 39    pub ruby: ColorScaleSet,
 40    pub crimson: ColorScaleSet,
 41    pub pink: ColorScaleSet,
 42    pub plum: ColorScaleSet,
 43    pub purple: ColorScaleSet,
 44    pub violet: ColorScaleSet,
 45    pub iris: ColorScaleSet,
 46    pub indigo: ColorScaleSet,
 47    pub blue: ColorScaleSet,
 48    pub cyan: ColorScaleSet,
 49    pub teal: ColorScaleSet,
 50    pub jade: ColorScaleSet,
 51    pub green: ColorScaleSet,
 52    pub grass: ColorScaleSet,
 53    pub lime: ColorScaleSet,
 54    pub mint: ColorScaleSet,
 55    pub sky: ColorScaleSet,
 56    pub black: ColorScaleSet,
 57    pub white: ColorScaleSet,
 58}
 59
 60impl IntoIterator for ColorScales {
 61    type Item = ColorScaleSet;
 62
 63    type IntoIter = std::vec::IntoIter<Self::Item>;
 64
 65    fn into_iter(self) -> Self::IntoIter {
 66        vec![
 67            self.gray,
 68            self.mauve,
 69            self.slate,
 70            self.sage,
 71            self.olive,
 72            self.sand,
 73            self.gold,
 74            self.bronze,
 75            self.brown,
 76            self.yellow,
 77            self.amber,
 78            self.orange,
 79            self.tomato,
 80            self.red,
 81            self.ruby,
 82            self.crimson,
 83            self.pink,
 84            self.plum,
 85            self.purple,
 86            self.violet,
 87            self.iris,
 88            self.indigo,
 89            self.blue,
 90            self.cyan,
 91            self.teal,
 92            self.jade,
 93            self.green,
 94            self.grass,
 95            self.lime,
 96            self.mint,
 97            self.sky,
 98            self.black,
 99            self.white,
100        ]
101        .into_iter()
102    }
103}
104
105pub struct ColorScaleSet {
106    name: SharedString,
107    light: ColorScale,
108    dark: ColorScale,
109    light_alpha: ColorScale,
110    dark_alpha: ColorScale,
111}
112
113impl ColorScaleSet {
114    pub fn new(
115        name: impl Into<SharedString>,
116        light: ColorScale,
117        light_alpha: ColorScale,
118        dark: ColorScale,
119        dark_alpha: ColorScale,
120    ) -> Self {
121        Self {
122            name: name.into(),
123            light,
124            light_alpha,
125            dark,
126            dark_alpha,
127        }
128    }
129
130    pub fn name(&self) -> &SharedString {
131        &self.name
132    }
133
134    pub fn light(&self, step: ColorScaleStep) -> Hsla {
135        self.light[step - 1]
136    }
137
138    pub fn light_alpha(&self, step: ColorScaleStep) -> Hsla {
139        self.light_alpha[step - 1]
140    }
141
142    pub fn dark(&self, step: ColorScaleStep) -> Hsla {
143        self.dark[step - 1]
144    }
145
146    pub fn dark_alpha(&self, step: ColorScaleStep) -> Hsla {
147        self.dark_alpha[step - 1]
148    }
149
150    pub fn step(&self, cx: &AppContext, step: ColorScaleStep) -> Hsla {
151        match cx.theme().appearance {
152            Appearance::Light => self.light(step),
153            Appearance::Dark => self.dark(step),
154        }
155    }
156
157    pub fn step_alpha(&self, cx: &AppContext, step: ColorScaleStep) -> Hsla {
158        match cx.theme().appearance {
159            Appearance::Light => self.light_alpha(step),
160            Appearance::Dark => self.dark_alpha(step),
161        }
162    }
163}