This repository was archived by the owner on Jun 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paththemes.rs
303 lines (275 loc) · 9.34 KB
/
themes.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
use std::collections::HashMap;
use std::fmt;
use std::ops::Add;
use std::str::FromStr;
use color_space::{Hsv, Rgb};
use serde::de::{self, Deserialize, Deserializer, MapAccess, Visitor};
use serde::{Serialize, Serializer};
use serde_derive::Deserialize;
use smartstring::alias::String;
use crate::errors::{self, OptionExt, ResultExt, ToSerdeError};
use crate::util;
use crate::widget::State;
// TODO docs
// TODO tests
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Color {
None,
Auto,
Rgba(Rgb, u8),
Hsva(Hsv, u8),
}
impl Serialize for Color {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let format_rgb = |rgb: Rgb, a: u8| {
format!(
"#{:02X}{:02X}{:02X}{:02X}",
rgb.r as u8, rgb.g as u8, rgb.b as u8, a
)
};
match *self {
Self::None | Self::Auto => serializer.serialize_none(),
Self::Rgba(rgb, a) => serializer.serialize_str(&format_rgb(rgb, a)),
Self::Hsva(hsv, a) => serializer.serialize_str(&format_rgb(hsv.into(), a)),
}
}
}
impl Color {
pub fn skip_ser(&self) -> bool {
matches!(self, Self::None | Self::Auto)
}
}
impl Default for Color {
fn default() -> Self {
Self::None
}
}
impl Add for Color {
type Output = Color;
fn add(self, rhs: Self) -> Self::Output {
let add_hsv = |a: Hsv, b: Hsv| {
Hsv::new(
(a.h + b.h) % 360.,
(a.s + b.s).clamp(0., 1.),
(a.v + b.v).clamp(0., 1.),
)
};
match (self, rhs) {
// Do nothing
(x, Color::None | Color::Auto) => x,
(Color::None | Color::Auto, x) => x,
// Hsv + Hsv => Hsv
(Color::Hsva(hsv1, a1), Color::Hsva(hsv2, a2)) => {
Color::Hsva(add_hsv(hsv1, hsv2), a1.saturating_add(a2))
}
// Rgb + Rgb => Rgb
(Color::Rgba(rgb1, a1), Color::Rgba(rgb2, a2)) => Color::Rgba(
Rgb::new(
(rgb1.r + rgb2.r).clamp(0., 255.),
(rgb1.g + rgb2.g).clamp(0., 255.),
(rgb1.b + rgb2.b).clamp(0., 255.),
),
a1.saturating_add(a2),
),
// Hsv + Rgb => Hsv
// Rgb + Hsv => Hsv
(Color::Hsva(hsv, a1), Color::Rgba(rgb, a2))
| (Color::Rgba(rgb, a1), Color::Hsva(hsv, a2)) => {
Color::Hsva(add_hsv(hsv, rgb.into()), a1.saturating_add(a2))
}
}
}
}
impl FromStr for Color {
type Err = crate::errors::Error;
fn from_str(color: &str) -> Result<Self, Self::Err> {
Ok(if color == "none" || color.is_empty() {
Color::None
} else if color == "auto" {
Color::Auto
} else if color.starts_with("hsv:") {
let err_msg = || format!("'{}' is not a vaild HSVA color", color);
let color = color.split_at(4).1;
let mut components = color.split(':').map(|x| x.parse::<f64>().or_error(err_msg));
let h = components.next().or_error(err_msg)??;
let s = components.next().or_error(err_msg)??;
let v = components.next().or_error(err_msg)??;
let a = components.next().unwrap_or(Ok(100.))?;
Color::Hsva(Hsv::new(h, s / 100., v / 100.), (a / 100. * 255.) as u8)
} else {
let err_msg = || format!("'{}' is not a vaild RGBA color", color);
let rgb = color.get(1..7).or_error(err_msg)?;
let a = color.get(7..9).unwrap_or("FF");
Color::Rgba(
Rgb::from_hex(u32::from_str_radix(rgb, 16).or_error(err_msg)?),
u8::from_str_radix(a, 16).or_error(err_msg)?,
)
})
}
}
impl<'de> Deserialize<'de> for Color {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct ColorVisitor;
impl<'de> Visitor<'de> for ColorVisitor {
type Value = Color;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("color")
}
fn visit_str<E>(self, s: &str) -> Result<Color, E>
where
E: de::Error,
{
s.parse().serde_error()
}
}
deserializer.deserialize_any(ColorVisitor)
}
}
#[derive(Debug, Clone, Default)]
pub struct Theme {
pub idle_bg: Color,
pub idle_fg: Color,
pub info_bg: Color,
pub info_fg: Color,
pub good_bg: Color,
pub good_fg: Color,
pub warning_bg: Color,
pub warning_fg: Color,
pub critical_bg: Color,
pub critical_fg: Color,
pub separator: Option<String>,
pub separator_bg: Color,
pub separator_fg: Color,
pub alternating_tint_bg: Color,
pub alternating_tint_fg: Color,
}
impl Theme {
pub fn from_file(file: &str) -> errors::Result<Theme> {
let file = util::find_file(file, Some("themes"), Some("toml"))
.or_error(|| format!("Theme '{}' not found", file))?;
let map: HashMap<String, String> = util::deserialize_toml_file(&file)?;
let mut theme = Self::default();
theme.apply_overrides(&map)?;
Ok(theme)
}
pub fn get_colors(&self, state: State) -> (Color, Color) {
match state {
State::Idle => (self.idle_bg, self.idle_fg),
State::Info => (self.info_bg, self.info_fg),
State::Good => (self.good_bg, self.good_fg),
State::Warning => (self.warning_bg, self.warning_fg),
State::Critical => (self.critical_bg, self.critical_fg),
}
}
pub fn apply_overrides(
&mut self,
overrides: &HashMap<String, String>,
) -> Result<(), crate::errors::Error> {
if let Some(separator) = overrides.get("separator") {
self.separator = Some(separator.clone());
}
macro_rules! apply {
($prop:tt) => {
if let Some(val) = overrides.get(stringify!($prop)) {
self.$prop = val.parse()?;
}
};
}
apply!(idle_bg);
apply!(idle_fg);
apply!(info_bg);
apply!(info_fg);
apply!(good_bg);
apply!(good_fg);
apply!(warning_bg);
apply!(warning_fg);
apply!(critical_bg);
apply!(critical_fg);
apply!(separator_bg);
apply!(separator_fg);
apply!(alternating_tint_bg);
apply!(alternating_tint_fg);
Ok(())
}
}
impl<'de> Deserialize<'de> for Theme {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(field_identifier, rename_all = "lowercase")]
enum Field {
Name,
File,
Overrides,
}
struct ThemeVisitor;
impl<'de> Visitor<'de> for ThemeVisitor {
type Value = Theme;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("struct Theme")
}
/// Handle configs like:
///
/// ```toml
/// theme = "slick"
/// ```
fn visit_str<E>(self, file: &str) -> Result<Theme, E>
where
E: de::Error,
{
Theme::from_file(file).serde_error()
}
/// Handle configs like:
///
/// ```toml
/// [theme]
/// name = "modern"
/// ```
fn visit_map<V>(self, mut map: V) -> Result<Theme, V::Error>
where
V: MapAccess<'de>,
{
let mut theme: Option<String> = None;
let mut overrides: Option<HashMap<String, String>> = None;
while let Some(key) = map.next_key()? {
match key {
// TODO merge name and file into one option (let's say "theme")
Field::Name => {
if theme.is_some() {
return Err(de::Error::duplicate_field("name or file"));
}
theme = Some(map.next_value()?);
}
Field::File => {
if theme.is_some() {
return Err(de::Error::duplicate_field("name or file"));
}
theme = Some(map.next_value()?);
}
Field::Overrides => {
if overrides.is_some() {
return Err(de::Error::duplicate_field("overrides"));
}
overrides = Some(map.next_value()?);
}
}
}
let theme = theme.unwrap_or_else(|| "plain".into());
let mut theme = Theme::from_file(&theme).serde_error()?;
if let Some(ref overrides) = overrides {
theme.apply_overrides(overrides).serde_error()?;
}
Ok(theme)
}
}
deserializer.deserialize_any(ThemeVisitor)
}
}