@@ -102,12 +102,13 @@ impl<'de> Deserialize<'de> for Theme {
102102 {
103103 let mut styles = HashMap::new();
104104
105− if let Ok(colors) = HashMap::<String, Value>::deserialize(deserializer) {
105+ if let Ok(mut colors) = HashMap::<String, Value>::deserialize(deserializer) {
106+ let palette = parse_palette(colors.remove("palette"));
106107 // scopes.reserve(colors.len());
107108 styles.reserve(colors.len());
108109 for (name, style_value) in colors {
109110 let mut style = Style::default();
110− parse_style(&mut style, style_value);
111+ parse_style(&mut style, style_value, &palette);
111112 // scopes.push(name);
112113 styles.insert(name, style);
113114 }
@@ -118,18 +119,31 @@ impl<'de> Deserialize<'de> for Theme {
118119 }
119120}
120121
121−fn parse_style(style: &mut Style, value: Value) {
122+fn parse_palette(value: Option<Value>) -> HashMap<String, Color> {
123+ match value {
124+ Some(Value::Table(entries)) => entries,
125+ _ => return HashMap::default(),
126+ }
127+ .into_iter()
128+ .filter_map(|(name, value)| {
129+ let color = parse_color(value, &HashMap::default())?;
130+ Some((name, color))
131+ })
132+ .collect()
133+}
134+
135+fn parse_style(style: &mut Style, value: Value, palette: &HashMap<String, Color>) {
122136 //TODO: alert user of parsing failures
123137 if let Value::Table(entries) = value {
124138 for (name, value) in entries {
125139 match name.as_str() {
126140 "fg" => {
127− if let Some(color) = parse_color(value) {
141+ if let Some(color) = parse_color(value, palette) {
128142 *style = style.fg(color);
129143 }
130144 }
131145 "bg" => {
132− if let Some(color) = parse_color(value) {
146+ if let Some(color) = parse_color(value, palette) {
133147 *style = style.bg(color);
134148 }
135149 }
@@ -143,7 +157,7 @@ fn parse_style(style: &mut Style, value: Value) {
143157 _ => (),
144158 }
145159 }
146− } else if let Some(color) = parse_color(value) {
160+ } else if let Some(color) = parse_color(value, palette) {
147161 *style = style.fg(color);
148162 }
149163}
@@ -164,9 +178,11 @@ fn hex_string_to_rgb(s: &str) -> Option<(u8, u8, u8)> {
164178 }
165179}
166180
167−fn parse_color(value: Value) -> Option<Color> {
181+fn parse_color(value: Value, palette: &HashMap<String, Color>) -> Option<Color> {
168182 if let Value::String(s) = value {
169− if let Some((red, green, blue)) = hex_string_to_rgb(&s) {
183+ if let Some(color) = palette.get(&s) {
184+ Some(*color)
185+ } else if let Some((red, green, blue)) = hex_string_to_rgb(&s) {
170186 Some(Color::Rgb(red, green, blue))
171187 } else {
172188 warn!("malformed hexcode in theme: {}", s);
@@ -226,7 +242,23 @@ fn test_parse_style_string() {
226242 let fg = Value::String("#ffffff".to_string());
227243
228244 let mut style = Style::default();
229− parse_style(&mut style, fg);
245+ parse_style(&mut style, fg, &HashMap::default());
246+
247+ assert_eq!(style, Style::default().fg(Color::Rgb(255, 255, 255)));
248+}
249+
250+#[test]
251+fn test_palette() {
252+ let fg = Value::String("my_color".to_string());
253+
254+ let mut style = Style::default();
255+ parse_style(
256+ &mut style,
257+ fg,
258+ &vec![("my_color".to_string(), Color::Rgb(255, 255, 255))]
259+ .into_iter()
260+ .collect(),
261+ );
230262
231263 assert_eq!(style, Style::default().fg(Color::Rgb(255, 255, 255)));
232264}
@@ -244,7 +276,7 @@ fn test_parse_style_table() {
244276 let mut style = Style::default();
245277 if let Value::Table(entries) = table {
246278 for (_name, value) in entries {
247− parse_style(&mut style, value);
279+ parse_style(&mut style, value, &HashMap::default());
248280 }
249281 }
250282