| 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 |
use makeover::{Rgb, ThemeColors}; |
| 28 |
use ratatui::style::Color; |
| 29 |
|
| 30 |
|
| 31 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 32 |
pub enum Mode { |
| 33 |
Light, |
| 34 |
Dark, |
| 35 |
HighContrast, |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
#[derive(Debug, Clone, Copy)] |
| 46 |
#[non_exhaustive] |
| 47 |
pub struct Theme { |
| 48 |
pub mode: Mode, |
| 49 |
|
| 50 |
pub surface_page: Color, |
| 51 |
pub surface_raised: Color, |
| 52 |
pub surface_sunken: Color, |
| 53 |
pub surface_overlay: Color, |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
pub surface_well: Option<Color>, |
| 69 |
|
| 70 |
pub content_primary: Color, |
| 71 |
pub content_secondary: Color, |
| 72 |
pub content_muted: Color, |
| 73 |
|
| 74 |
pub action_primary: Color, |
| 75 |
|
| 76 |
pub status_danger: Color, |
| 77 |
pub status_success: Color, |
| 78 |
pub status_warning: Color, |
| 79 |
pub status_info: Color, |
| 80 |
|
| 81 |
|
| 82 |
pub line_border: Color, |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
pub border_strong: Color, |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
pub bevel_light: Color, |
| 97 |
pub bevel_dark: Color, |
| 98 |
|
| 99 |
pub category: [Color; 6], |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
#[derive(Debug, Clone)] |
| 107 |
pub enum ThemeError { |
| 108 |
MissingKey(&'static str), |
| 109 |
InvalidHex { key: &'static str, value: String }, |
| 110 |
} |
| 111 |
|
| 112 |
impl std::fmt::Display for ThemeError { |
| 113 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 114 |
match self { |
| 115 |
Self::MissingKey(k) => write!(f, "theme missing required key `{k}`"), |
| 116 |
Self::InvalidHex { key, value } => { |
| 117 |
write!(f, "theme key `{key}` has invalid hex value `{value}`") |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
impl std::error::Error for ThemeError {} |
| 124 |
|
| 125 |
impl Theme { |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
pub fn from_theme(theme: &ThemeColors) -> Result<Self, ThemeError> { |
| 134 |
let authored = |key: &'static str| -> Result<Rgb, ThemeError> { |
| 135 |
let hex = theme.colors.get(key).ok_or(ThemeError::MissingKey(key))?; |
| 136 |
Rgb::from_hex(hex).ok_or_else(|| ThemeError::InvalidHex { |
| 137 |
key, |
| 138 |
value: hex.clone(), |
| 139 |
}) |
| 140 |
}; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
let resolved = makeover::resolve(theme); |
| 147 |
let derived = |key: &'static str| -> Result<Rgb, ThemeError> { |
| 148 |
let hex = resolved.hex(key).ok_or(ThemeError::MissingKey(key))?; |
| 149 |
Rgb::from_hex(hex).ok_or_else(|| ThemeError::InvalidHex { |
| 150 |
key, |
| 151 |
value: hex.to_string(), |
| 152 |
}) |
| 153 |
}; |
| 154 |
|
| 155 |
let mode = match theme.meta.variant.as_str() { |
| 156 |
"dark" => Mode::Dark, |
| 157 |
"high-contrast" => Mode::HighContrast, |
| 158 |
_ => Mode::Light, |
| 159 |
}; |
| 160 |
|
| 161 |
Ok(Self { |
| 162 |
mode, |
| 163 |
|
| 164 |
surface_page: rgb(authored("surface.page")?), |
| 165 |
surface_raised: rgb(authored("surface.raised")?), |
| 166 |
surface_sunken: rgb(authored("surface.sunken")?), |
| 167 |
surface_overlay: rgb(authored("surface.overlay")?), |
| 168 |
surface_well: resolved |
| 169 |
.hex("surface-well") |
| 170 |
.and_then(Rgb::from_hex) |
| 171 |
.map(rgb), |
| 172 |
|
| 173 |
content_primary: rgb(authored("content.primary")?), |
| 174 |
content_secondary: rgb(authored("content.secondary")?), |
| 175 |
content_muted: rgb(authored("content.muted")?), |
| 176 |
|
| 177 |
action_primary: rgb(authored("action.primary")?), |
| 178 |
|
| 179 |
status_danger: rgb(authored("status.danger")?), |
| 180 |
status_success: rgb(authored("status.success")?), |
| 181 |
status_warning: rgb(authored("status.warning")?), |
| 182 |
status_info: rgb(authored("status.info")?), |
| 183 |
|
| 184 |
line_border: rgb(authored("line.border")?), |
| 185 |
border_strong: rgb(derived("border-strong")?), |
| 186 |
|
| 187 |
bevel_light: rgb(derived("bevel-light")?), |
| 188 |
bevel_dark: rgb(derived("bevel-dark")?), |
| 189 |
|
| 190 |
category: [ |
| 191 |
rgb(authored("category.one")?), |
| 192 |
rgb(authored("category.two")?), |
| 193 |
rgb(authored("category.three")?), |
| 194 |
rgb(authored("category.four")?), |
| 195 |
rgb(authored("category.five")?), |
| 196 |
rgb(authored("category.six")?), |
| 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 |
#[must_use] |
| 228 |
pub fn for_terminal(self, fidelity: crate::Fidelity) -> Self { |
| 229 |
let Some(q) = Quantize::for_fidelity(fidelity) else { |
| 230 |
return self; |
| 231 |
}; |
| 232 |
|
| 233 |
let plain = |c: Color| q.plain(c); |
| 234 |
let on_page = |c: Color| q.against(c, self.surface_page); |
| 235 |
|
| 236 |
Self { |
| 237 |
mode: self.mode, |
| 238 |
|
| 239 |
surface_page: plain(self.surface_page), |
| 240 |
surface_raised: plain(self.surface_raised), |
| 241 |
surface_sunken: plain(self.surface_sunken), |
| 242 |
surface_overlay: plain(self.surface_overlay), |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
surface_well: self.surface_well.map(plain), |
| 248 |
|
| 249 |
content_primary: on_page(self.content_primary), |
| 250 |
content_secondary: on_page(self.content_secondary), |
| 251 |
content_muted: on_page(self.content_muted), |
| 252 |
|
| 253 |
action_primary: on_page(self.action_primary), |
| 254 |
|
| 255 |
status_danger: on_page(self.status_danger), |
| 256 |
status_success: on_page(self.status_success), |
| 257 |
status_warning: on_page(self.status_warning), |
| 258 |
status_info: on_page(self.status_info), |
| 259 |
|
| 260 |
line_border: on_page(self.line_border), |
| 261 |
border_strong: on_page(self.border_strong), |
| 262 |
|
| 263 |
bevel_light: plain(self.bevel_light), |
| 264 |
bevel_dark: plain(self.bevel_dark), |
| 265 |
|
| 266 |
category: self.category.map(on_page), |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
#[must_use] |
| 278 |
pub const fn palette(&self, fidelity: crate::Fidelity) -> crate::Palette { |
| 279 |
crate::Palette { |
| 280 |
page: self.surface_page, |
| 281 |
raised: self.surface_raised, |
| 282 |
overlay: self.surface_overlay, |
| 283 |
well: self.surface_well, |
| 284 |
bevel_light: self.bevel_light, |
| 285 |
bevel_dark: self.bevel_dark, |
| 286 |
fidelity, |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
fn rgb(c: Rgb) -> Color { |
| 292 |
Color::Rgb(c.r, c.g, c.b) |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
#[derive(Debug, Clone, Copy)] |
| 304 |
pub struct Quantize { |
| 305 |
palette: &'static [Rgb], |
| 306 |
offset: usize, |
| 307 |
} |
| 308 |
|
| 309 |
impl Quantize { |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
#[must_use] |
| 317 |
pub const fn for_fidelity(fidelity: crate::Fidelity) -> Option<Self> { |
| 318 |
match fidelity { |
| 319 |
crate::Fidelity::TrueColor => None, |
| 320 |
crate::Fidelity::Ansi256 => Some(Self { |
| 321 |
palette: makeover::ANSI_240, |
| 322 |
offset: makeover::ANSI_240_OFFSET, |
| 323 |
}), |
| 324 |
crate::Fidelity::Ansi16 => Some(Self { |
| 325 |
palette: &makeover::ANSI_16, |
| 326 |
offset: 0, |
| 327 |
}), |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
#[must_use] |
| 336 |
pub fn plain(&self, c: Color) -> Color { |
| 337 |
match c { |
| 338 |
Color::Rgb(r, g, b) => Color::Indexed( |
| 339 |
(makeover::quantize(Rgb { r, g, b }, self.palette) + self.offset) as u8, |
| 340 |
), |
| 341 |
other => other, |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
#[must_use] |
| 354 |
pub fn against(&self, c: Color, on: Color) -> Color { |
| 355 |
match (c, on) { |
| 356 |
(Color::Rgb(r, g, b), Color::Rgb(br, bg, bb)) => Color::Indexed( |
| 357 |
(makeover::quantize_against( |
| 358 |
Rgb { r, g, b }, |
| 359 |
Rgb { |
| 360 |
r: br, |
| 361 |
g: bg, |
| 362 |
b: bb, |
| 363 |
}, |
| 364 |
self.palette, |
| 365 |
) + self.offset) as u8, |
| 366 |
), |
| 367 |
_ => self.plain(c), |
| 368 |
} |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
#[cfg(test)] |
| 373 |
mod tests { |
| 374 |
use super::*; |
| 375 |
|
| 376 |
fn bundled(id: &str) -> ThemeColors { |
| 377 |
let dir = makeover::bundled_themes_dir().expect("makeover ships themes"); |
| 378 |
makeover::load_theme(&[(dir, false)], id).expect("bundled theme loads") |
| 379 |
} |
| 380 |
|
| 381 |
#[test] |
| 382 |
fn every_bundled_theme_resolves() { |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
let dir = makeover::bundled_themes_dir().expect("makeover ships themes"); |
| 387 |
let metas = makeover::list_themes_from_dirs(&[(dir, false)]); |
| 388 |
assert!( |
| 389 |
!metas.is_empty(), |
| 390 |
"makeover shipped no themes to test against" |
| 391 |
); |
| 392 |
for meta in &metas { |
| 393 |
let colors = bundled(&meta.id); |
| 394 |
assert!( |
| 395 |
Theme::from_theme(&colors).is_ok(), |
| 396 |
"bundled theme `{}` failed to resolve", |
| 397 |
meta.id |
| 398 |
); |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
#[test] |
| 403 |
fn a_missing_intent_names_the_key_it_wanted() { |
| 404 |
let mut colors = bundled("goingson"); |
| 405 |
colors.colors.remove("content.muted"); |
| 406 |
match Theme::from_theme(&colors) { |
| 407 |
Err(ThemeError::MissingKey(k)) => assert_eq!(k, "content.muted"), |
| 408 |
other => panic!("expected MissingKey(content.muted), got {other:?}"), |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
#[test] |
| 413 |
fn an_unparseable_hex_names_the_key_and_the_value() { |
| 414 |
let mut colors = bundled("goingson"); |
| 415 |
colors |
| 416 |
.colors |
| 417 |
.insert("content.muted".into(), "not-a-colour".into()); |
| 418 |
match Theme::from_theme(&colors) { |
| 419 |
Err(ThemeError::InvalidHex { key, value }) => { |
| 420 |
assert_eq!(key, "content.muted"); |
| 421 |
assert_eq!(value, "not-a-colour"); |
| 422 |
} |
| 423 |
other => panic!("expected InvalidHex, got {other:?}"), |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
#[test] |
| 428 |
fn a_capable_terminal_gets_the_theme_as_authored() { |
| 429 |
let theme = Theme::from_theme(&bundled("goingson")).expect("resolves"); |
| 430 |
let same = theme.for_terminal(crate::Fidelity::TrueColor); |
| 431 |
assert_eq!(same.surface_page, theme.surface_page); |
| 432 |
assert_eq!(same.content_primary, theme.content_primary); |
| 433 |
assert!(matches!(same.surface_page, Color::Rgb(..))); |
| 434 |
} |
| 435 |
|
| 436 |
#[test] |
| 437 |
fn a_limited_terminal_gets_indices_rather_than_rgb() { |
| 438 |
let theme = Theme::from_theme(&bundled("goingson")).expect("resolves"); |
| 439 |
for fidelity in [crate::Fidelity::Ansi16, crate::Fidelity::Ansi256] { |
| 440 |
let q = theme.for_terminal(fidelity); |
| 441 |
assert!( |
| 442 |
matches!(q.surface_page, Color::Indexed(_)), |
| 443 |
"{fidelity:?} left a surface as rgb" |
| 444 |
); |
| 445 |
assert!( |
| 446 |
matches!(q.content_primary, Color::Indexed(_)), |
| 447 |
"{fidelity:?} left content as rgb" |
| 448 |
); |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
#[test] |
| 453 |
fn the_256_indices_land_outside_the_repaintable_low_sixteen() { |
| 454 |
|
| 455 |
|
| 456 |
let theme = Theme::from_theme(&bundled("goingson")).expect("resolves"); |
| 457 |
let q = theme.for_terminal(crate::Fidelity::Ansi256); |
| 458 |
for (name, c) in [ |
| 459 |
("surface_page", q.surface_page), |
| 460 |
("content_primary", q.content_primary), |
| 461 |
("bevel_light", q.bevel_light), |
| 462 |
("bevel_dark", q.bevel_dark), |
| 463 |
] { |
| 464 |
match c { |
| 465 |
Color::Indexed(i) => assert!(i >= 16, "{name} landed on repaintable index {i}"), |
| 466 |
other => panic!("{name} was not quantised: {other:?}"), |
| 467 |
} |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
#[test] |
| 472 |
fn the_bevel_pair_stays_two_tones_at_256() { |
| 473 |
|
| 474 |
|
| 475 |
let theme = Theme::from_theme(&bundled("goingson")).expect("resolves"); |
| 476 |
let q = theme.for_terminal(crate::Fidelity::Ansi256); |
| 477 |
assert_ne!(q.bevel_light, q.bevel_dark); |
| 478 |
} |
| 479 |
|
| 480 |
#[test] |
| 481 |
fn the_palette_takes_the_well_and_not_the_sunken_surface() { |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
let colors = bundled("goingson"); |
| 486 |
let theme = Theme::from_theme(&colors).expect("resolves"); |
| 487 |
let palette = theme.palette(crate::Fidelity::TrueColor); |
| 488 |
assert_eq!(palette.well, theme.surface_well); |
| 489 |
assert_ne!(palette.well, Some(theme.surface_sunken)); |
| 490 |
} |
| 491 |
} |
| 492 |
|