1use crate::color::Rgba as Color32;
4
5#[cfg(feature = "serde")]
6use super::mix;
7use super::{Theme, contrast_ratio, until_contrast};
8
9impl Theme {
10 pub fn high_contrast(mut self) -> Self {
13 let dark = self.is_dark();
14 self.background = if dark { Color32::BLACK } else { Color32::WHITE };
15 self.surface = if dark {
16 Color32::from_rgb(0x0a, 0x0a, 0x0a)
17 } else {
18 Color32::from_rgb(0xf5, 0xf5, 0xf5)
19 };
20 self.surface_raised = if dark {
21 Color32::from_rgb(0x14, 0x14, 0x14)
22 } else {
23 Color32::WHITE
24 };
25 self.sunken = if dark {
26 Color32::from_rgb(0x18, 0x18, 0x18)
27 } else {
28 Color32::from_rgb(0xea, 0xea, 0xea)
29 };
30 self.text = if dark { Color32::WHITE } else { Color32::BLACK };
31 self.text_muted = if dark {
32 Color32::from_rgb(0xd0, 0xd0, 0xd0)
33 } else {
34 Color32::from_rgb(0x30, 0x30, 0x30)
35 };
36 self.border = self.text;
37 self.focus = if dark {
38 Color32::from_rgb(0xff, 0xd7, 0x00)
39 } else {
40 Color32::from_rgb(0x00, 0x40, 0x90)
41 };
42 self.scrollbar = self.text_muted;
43 self.ensure_aa()
44 }
45
46 pub fn ensure_aa(mut self) -> Self {
49 let towards = if self.is_dark() { Color32::WHITE } else { Color32::BLACK };
50 if contrast_ratio(self.text, self.background) < 4.5 {
51 self.text = until_contrast(self.text, self.background, towards, 4.5);
52 }
53 if contrast_ratio(self.text_muted, self.background) < 4.5 {
54 self.text_muted = until_contrast(self.text_muted, self.background, towards, 4.5);
55 }
56 if contrast_ratio(self.text, self.surface) < 4.5 {
57 self.surface = until_contrast(self.surface, self.text, towards, 4.5);
58 }
59 if contrast_ratio(self.accent_text, self.accent) < 4.5 {
60 self = self.with_accent(self.accent);
61 }
62 if contrast_ratio(self.link, self.background) < 4.5 {
63 self.link = until_contrast(self.link, self.background, towards, 4.5);
64 }
65 if contrast_ratio(self.focus, self.background) < 3.0 {
66 self.focus = until_contrast(self.focus, self.background, towards, 3.0);
67 }
68 self
69 }
70
71 pub fn to_css_variables(&self) -> String {
73 let mut css = String::from(":root {\n");
74 for (name, color) in [
75 ("background", self.background),
76 ("surface", self.surface),
77 ("surface-raised", self.surface_raised),
78 ("sunken", self.sunken),
79 ("text", self.text),
80 ("text-muted", self.text_muted),
81 ("accent", self.accent),
82 ("accent-text", self.accent_text),
83 ("border", self.border),
84 ("selection", self.selection),
85 ("hover", self.hover),
86 ("danger", self.danger),
87 ("warning", self.warning),
88 ("success", self.success),
89 ("done", self.done),
90 ("link", self.link),
91 ("scrollbar", self.scrollbar),
92 ("focus", self.focus),
93 ] {
94 css.push_str(&format!(" --wb-{name}: {};\n", hex(color)));
95 }
96 css.push_str("}\n");
97 css
98 }
99
100 #[cfg(feature = "serde")]
106 pub fn from_vscode_json(text: &str) -> Result<Self, ThemeImportError> {
107 let value: serde_json::Value =
108 serde_json::from_str(text).map_err(|error| ThemeImportError::Json(error.to_string()))?;
109 let colors = value
110 .get("colors")
111 .and_then(serde_json::Value::as_object)
112 .ok_or(ThemeImportError::Missing("colors"))?;
113 let get = |key: &str| {
114 colors
115 .get(key)
116 .and_then(serde_json::Value::as_str)
117 .and_then(parse_color)
118 };
119 let background = get("editor.background").ok_or(ThemeImportError::Missing("editor.background"))?;
120 let text = get("foreground").ok_or(ThemeImportError::Missing("foreground"))?;
121 let accent = get("focusBorder")
122 .or_else(|| get("activityBarBadge.background"))
123 .or_else(|| get("button.background"))
124 .unwrap_or(Color32::from_rgb(0x2f, 0x81, 0xf7));
125 let theme = Self {
126 background,
127 surface: get("sideBar.background").unwrap_or(mix(background, text, 0.06)),
128 surface_raised: get("panel.background").unwrap_or(mix(background, text, 0.09)),
129 sunken: get("input.background").unwrap_or(mix(background, text, 0.04)),
130 text,
131 text_muted: get("descriptionForeground").unwrap_or(mix(text, background, 0.35)),
132 accent,
133 accent_text: get("button.foreground").unwrap_or(Color32::WHITE),
134 border: get("panel.border").unwrap_or(mix(background, text, 0.22)),
135 selection: get("list.activeSelectionBackground").unwrap_or(mix(background, accent, 0.35)),
136 hover: get("list.hoverBackground").unwrap_or(mix(background, text, 0.1)),
137 danger: get("errorForeground").unwrap_or(Color32::from_rgb(0xf8, 0x51, 0x49)),
138 warning: get("editorWarning.foreground").unwrap_or(Color32::from_rgb(0xd2, 0x9a, 0x00)),
139 success: get("terminal.ansiGreen").unwrap_or(Color32::from_rgb(0x3f, 0xb9, 0x50)),
140 done: get("terminal.ansiMagenta").unwrap_or(Color32::from_rgb(0xa3, 0x71, 0xf7)),
141 link: get("textLink.foreground").unwrap_or(accent),
142 scrollbar: get("scrollbarSlider.background").unwrap_or(mix(background, text, 0.3)),
143 focus: get("focusBorder").unwrap_or(accent),
144 };
145 Ok(theme.ensure_aa())
146 }
147
148 #[cfg(feature = "serde")]
150 pub fn to_json(&self) -> serde_json::Value {
151 serde_json::json!({
152 "background": hex(self.background),
153 "surface": hex(self.surface),
154 "surface_raised": hex(self.surface_raised),
155 "sunken": hex(self.sunken),
156 "text": hex(self.text),
157 "text_muted": hex(self.text_muted),
158 "accent": hex(self.accent),
159 "accent_text": hex(self.accent_text),
160 "border": hex(self.border),
161 "selection": hex(self.selection),
162 "hover": hex(self.hover),
163 "danger": hex(self.danger),
164 "warning": hex(self.warning),
165 "success": hex(self.success),
166 "done": hex(self.done),
167 "link": hex(self.link),
168 "scrollbar": hex(self.scrollbar),
169 "focus": hex(self.focus),
170 })
171 }
172}
173
174fn hex(color: Color32) -> String {
175 format!("#{:02x}{:02x}{:02x}", color.r(), color.g(), color.b())
176}
177
178#[cfg(feature = "serde")]
179fn parse_color(text: &str) -> Option<Color32> {
180 let text = text.trim().trim_start_matches('#');
181 if text.len() == 8 {
182 return Some(Color32::from_rgba_unmultiplied(
183 u8::from_str_radix(&text[0..2], 16).ok()?,
184 u8::from_str_radix(&text[2..4], 16).ok()?,
185 u8::from_str_radix(&text[4..6], 16).ok()?,
186 u8::from_str_radix(&text[6..8], 16).ok()?,
187 ));
188 }
189 if text.len() != 6 {
190 return None;
191 }
192 Some(Color32::from_rgb(
193 u8::from_str_radix(&text[0..2], 16).ok()?,
194 u8::from_str_radix(&text[2..4], 16).ok()?,
195 u8::from_str_radix(&text[4..6], 16).ok()?,
196 ))
197}
198
199#[cfg(feature = "serde")]
201#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
202#[non_exhaustive]
203pub enum ThemeImportError {
209 #[error("{0}")]
211 Json(String),
212 #[error("missing VS Code color `{0}`")]
214 Missing(&'static str),
215}