| 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 |
#![allow(clippy::many_single_char_names, clippy::unreadable_literal)] |
| 40 |
|
| 41 |
use serde::Serialize; |
| 42 |
use std::collections::{BTreeMap, HashMap}; |
| 43 |
use std::path::{Path, PathBuf}; |
| 44 |
|
| 45 |
|
| 46 |
pub const COLOR_SECTIONS: &[&str] = &["surface", "content", "action", "status", "line", "category"]; |
| 47 |
|
| 48 |
|
| 49 |
#[derive(Debug, Clone, Serialize)] |
| 50 |
#[serde(rename_all = "camelCase")] |
| 51 |
pub struct ThemeMeta { |
| 52 |
pub id: String, |
| 53 |
pub name: String, |
| 54 |
pub variant: String, |
| 55 |
pub is_custom: bool, |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
#[derive(Debug, Serialize)] |
| 61 |
#[serde(rename_all = "camelCase")] |
| 62 |
pub struct ThemeColors { |
| 63 |
pub meta: ThemeMeta, |
| 64 |
pub colors: HashMap<String, String>, |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 79 |
pub struct Rgb { |
| 80 |
pub r: u8, |
| 81 |
pub g: u8, |
| 82 |
pub b: u8, |
| 83 |
} |
| 84 |
|
| 85 |
impl Rgb { |
| 86 |
|
| 87 |
pub fn from_hex(s: &str) -> Option<Rgb> { |
| 88 |
let h = s.strip_prefix('#')?; |
| 89 |
let (r, g, b) = match h.len() { |
| 90 |
6 => ( |
| 91 |
u8::from_str_radix(&h[0..2], 16).ok()?, |
| 92 |
u8::from_str_radix(&h[2..4], 16).ok()?, |
| 93 |
u8::from_str_radix(&h[4..6], 16).ok()?, |
| 94 |
), |
| 95 |
3 => { |
| 96 |
let d = |c: &str| u8::from_str_radix(c, 16).ok().map(|v| v * 17); |
| 97 |
(d(&h[0..1])?, d(&h[1..2])?, d(&h[2..3])?) |
| 98 |
} |
| 99 |
_ => return None, |
| 100 |
}; |
| 101 |
Some(Rgb { r, g, b }) |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
pub fn to_hex(self) -> String { |
| 106 |
format!("#{:02x}{:02x}{:02x}", self.r, self.g, self.b) |
| 107 |
} |
| 108 |
|
| 109 |
pub fn tuple(self) -> (u8, u8, u8) { |
| 110 |
(self.r, self.g, self.b) |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
#[derive(Clone, Copy, Debug)] |
| 116 |
pub struct Oklab { |
| 117 |
pub l: f32, |
| 118 |
pub a: f32, |
| 119 |
pub b: f32, |
| 120 |
} |
| 121 |
|
| 122 |
fn srgb_to_linear(c: u8) -> f32 { |
| 123 |
let c = c as f32 / 255.0; |
| 124 |
if c <= 0.04045 { |
| 125 |
c / 12.92 |
| 126 |
} else { |
| 127 |
((c + 0.055) / 1.055).powf(2.4) |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
fn linear_to_srgb(c: f32) -> u8 { |
| 132 |
let c = c.clamp(0.0, 1.0); |
| 133 |
let v = if c <= 0.0031308 { |
| 134 |
c * 12.92 |
| 135 |
} else { |
| 136 |
1.055 * c.powf(1.0 / 2.4) - 0.055 |
| 137 |
}; |
| 138 |
(v * 255.0).round().clamp(0.0, 255.0) as u8 |
| 139 |
} |
| 140 |
|
| 141 |
impl Rgb { |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
#[allow(clippy::excessive_precision)] |
| 148 |
pub fn to_oklab(self) -> Oklab { |
| 149 |
let (r, g, b) = ( |
| 150 |
srgb_to_linear(self.r), |
| 151 |
srgb_to_linear(self.g), |
| 152 |
srgb_to_linear(self.b), |
| 153 |
); |
| 154 |
let l = 0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b; |
| 155 |
let m = 0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b; |
| 156 |
let s = 0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b; |
| 157 |
let (l_, m_, s_) = (l.cbrt(), m.cbrt(), s.cbrt()); |
| 158 |
Oklab { |
| 159 |
l: 0.2104542553 * l_ + 0.7936177850 * m_ - 0.0040720468 * s_, |
| 160 |
a: 1.9779984951 * l_ - 2.4285922050 * m_ + 0.4505937099 * s_, |
| 161 |
b: 0.0259040371 * l_ + 0.7827717662 * m_ - 0.8086757660 * s_, |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
#[allow(clippy::excessive_precision)] |
| 169 |
pub fn from_oklab(c: Oklab) -> Rgb { |
| 170 |
let l_ = c.l + 0.3963377774 * c.a + 0.2158037573 * c.b; |
| 171 |
let m_ = c.l - 0.1055613458 * c.a - 0.0638541728 * c.b; |
| 172 |
let s_ = c.l - 0.0894841775 * c.a - 1.2914855480 * c.b; |
| 173 |
let (l, m, s) = (l_ * l_ * l_, m_ * m_ * m_, s_ * s_ * s_); |
| 174 |
Rgb { |
| 175 |
r: linear_to_srgb(4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s), |
| 176 |
g: linear_to_srgb(-1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s), |
| 177 |
b: linear_to_srgb(-0.0041960863 * l - 0.7034186147 * m + 1.7076147010 * s), |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
fn rel_luminance(c: Rgb) -> f32 { |
| 184 |
0.2126 * srgb_to_linear(c.r) + 0.7152 * srgb_to_linear(c.g) + 0.0722 * srgb_to_linear(c.b) |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
pub fn wcag_contrast(a: Rgb, b: Rgb) -> f32 { |
| 189 |
let (la, lb) = (rel_luminance(a), rel_luminance(b)); |
| 190 |
let (hi, lo) = if la >= lb { (la, lb) } else { (lb, la) }; |
| 191 |
(hi + 0.05) / (lo + 0.05) |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
pub fn readable_on(bg: Rgb) -> Rgb { |
| 197 |
let white = Rgb { |
| 198 |
r: 255, |
| 199 |
g: 255, |
| 200 |
b: 255, |
| 201 |
}; |
| 202 |
let black = Rgb { r: 0, g: 0, b: 0 }; |
| 203 |
if wcag_contrast(white, bg) >= wcag_contrast(black, bg) { |
| 204 |
white |
| 205 |
} else { |
| 206 |
black |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
pub fn lighten(c: Rgb, delta: f32) -> Rgb { |
| 212 |
let mut lab = c.to_oklab(); |
| 213 |
lab.l = (lab.l + delta).clamp(0.0, 1.0); |
| 214 |
Rgb::from_oklab(lab) |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
pub fn darken(c: Rgb, delta: f32) -> Rgb { |
| 219 |
lighten(c, -delta) |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
pub fn mix(a: Rgb, b: Rgb, t: f32) -> Rgb { |
| 224 |
let (x, y) = (a.to_oklab(), b.to_oklab()); |
| 225 |
Rgb::from_oklab(Oklab { |
| 226 |
l: x.l + (y.l - x.l) * t, |
| 227 |
a: x.a + (y.a - x.a) * t, |
| 228 |
b: x.b + (y.b - x.b) * t, |
| 229 |
}) |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
pub const ANSI_16: [Rgb; 16] = [ |
| 248 |
Rgb { |
| 249 |
r: 0x00, |
| 250 |
g: 0x00, |
| 251 |
b: 0x00, |
| 252 |
}, |
| 253 |
Rgb { |
| 254 |
r: 0xaa, |
| 255 |
g: 0x00, |
| 256 |
b: 0x00, |
| 257 |
}, |
| 258 |
Rgb { |
| 259 |
r: 0x00, |
| 260 |
g: 0xaa, |
| 261 |
b: 0x00, |
| 262 |
}, |
| 263 |
Rgb { |
| 264 |
r: 0xaa, |
| 265 |
g: 0x55, |
| 266 |
b: 0x00, |
| 267 |
}, |
| 268 |
Rgb { |
| 269 |
r: 0x00, |
| 270 |
g: 0x00, |
| 271 |
b: 0xaa, |
| 272 |
}, |
| 273 |
Rgb { |
| 274 |
r: 0xaa, |
| 275 |
g: 0x00, |
| 276 |
b: 0xaa, |
| 277 |
}, |
| 278 |
Rgb { |
| 279 |
r: 0x00, |
| 280 |
g: 0xaa, |
| 281 |
b: 0xaa, |
| 282 |
}, |
| 283 |
Rgb { |
| 284 |
r: 0xaa, |
| 285 |
g: 0xaa, |
| 286 |
b: 0xaa, |
| 287 |
}, |
| 288 |
Rgb { |
| 289 |
r: 0x55, |
| 290 |
g: 0x55, |
| 291 |
b: 0x55, |
| 292 |
}, |
| 293 |
Rgb { |
| 294 |
r: 0xff, |
| 295 |
g: 0x55, |
| 296 |
b: 0x55, |
| 297 |
}, |
| 298 |
Rgb { |
| 299 |
r: 0x55, |
| 300 |
g: 0xff, |
| 301 |
b: 0x55, |
| 302 |
}, |
| 303 |
Rgb { |
| 304 |
r: 0xff, |
| 305 |
g: 0xff, |
| 306 |
b: 0x55, |
| 307 |
}, |
| 308 |
Rgb { |
| 309 |
r: 0x55, |
| 310 |
g: 0x55, |
| 311 |
b: 0xff, |
| 312 |
}, |
| 313 |
Rgb { |
| 314 |
r: 0xff, |
| 315 |
g: 0x55, |
| 316 |
b: 0xff, |
| 317 |
}, |
| 318 |
Rgb { |
| 319 |
r: 0x55, |
| 320 |
g: 0xff, |
| 321 |
b: 0xff, |
| 322 |
}, |
| 323 |
Rgb { |
| 324 |
r: 0xff, |
| 325 |
g: 0xff, |
| 326 |
b: 0xff, |
| 327 |
}, |
| 328 |
]; |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
pub const ANSI_256: [Rgb; 256] = build_ansi_256(); |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
pub const ANSI_240: &[Rgb] = ANSI_256.split_at(16).1; |
| 348 |
|
| 349 |
|
| 350 |
pub const ANSI_240_OFFSET: usize = 16; |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
const CHROMATIC: [(usize, &str); 12] = [ |
| 366 |
(1, "status.danger"), |
| 367 |
(2, "status.success"), |
| 368 |
(3, "status.warning"), |
| 369 |
(4, "status.info"), |
| 370 |
(5, "category.five"), |
| 371 |
(6, "category.six"), |
| 372 |
(9, "action.primary"), |
| 373 |
(10, "status.success"), |
| 374 |
(11, "status.warning"), |
| 375 |
(12, "status.info"), |
| 376 |
(13, "category.five"), |
| 377 |
(14, "category.six"), |
| 378 |
]; |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
fn achromatic_slot(index: usize, variant: &str) -> Option<&'static str> { |
| 396 |
let dark = variant == "dark"; |
| 397 |
Some(match (index, dark) { |
| 398 |
(0, false) => "content.primary", |
| 399 |
(0, true) => "surface.sunken", |
| 400 |
(7, false) => "surface.raised", |
| 401 |
(7, true) => "content.secondary", |
| 402 |
(8, _) => "content.muted", |
| 403 |
(15, false) => "surface.overlay", |
| 404 |
(15, true) => "content.primary", |
| 405 |
_ => return None, |
| 406 |
}) |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
#[must_use] |
| 419 |
pub fn ansi_intent(index: usize, variant: &str) -> Option<&'static str> { |
| 420 |
achromatic_slot(index, variant).or_else(|| { |
| 421 |
CHROMATIC |
| 422 |
.iter() |
| 423 |
.find(|(slot, _)| *slot == index) |
| 424 |
.map(|(_, intent)| *intent) |
| 425 |
}) |
| 426 |
} |
| 427 |
|
| 428 |
const fn build_ansi_256() -> [Rgb; 256] { |
| 429 |
let mut table = [Rgb { r: 0, g: 0, b: 0 }; 256]; |
| 430 |
|
| 431 |
let mut i = 0; |
| 432 |
while i < 16 { |
| 433 |
table[i] = ANSI_16[i]; |
| 434 |
i += 1; |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
const LEVELS: [u8; 6] = [0, 95, 135, 175, 215, 255]; |
| 442 |
let mut r = 0; |
| 443 |
while r < 6 { |
| 444 |
let mut g = 0; |
| 445 |
while g < 6 { |
| 446 |
let mut b = 0; |
| 447 |
while b < 6 { |
| 448 |
table[16 + 36 * r + 6 * g + b] = Rgb { |
| 449 |
r: LEVELS[r], |
| 450 |
g: LEVELS[g], |
| 451 |
b: LEVELS[b], |
| 452 |
}; |
| 453 |
b += 1; |
| 454 |
} |
| 455 |
g += 1; |
| 456 |
} |
| 457 |
r += 1; |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
let mut k = 0; |
| 464 |
while k < 24 { |
| 465 |
let v = 8 + 10 * k as u8; |
| 466 |
table[232 + k as usize] = Rgb { r: v, g: v, b: v }; |
| 467 |
k += 1; |
| 468 |
} |
| 469 |
|
| 470 |
table |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
pub const DISTINCT: f32 = 3.0; |
| 479 |
|
| 480 |
|
| 481 |
fn oklab_distance(a: Rgb, b: Rgb) -> f32 { |
| 482 |
let (x, y) = (a.to_oklab(), b.to_oklab()); |
| 483 |
((x.l - y.l).powi(2) + (x.a - y.a).powi(2) + (x.b - y.b).powi(2)).sqrt() |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
pub fn quantize(c: Rgb, palette: &[Rgb]) -> usize { |
| 496 |
assert!(!palette.is_empty(), "a palette needs at least one color"); |
| 497 |
let mut best = 0; |
| 498 |
let mut best_distance = f32::INFINITY; |
| 499 |
for (index, entry) in palette.iter().enumerate() { |
| 500 |
let distance = oklab_distance(c, *entry); |
| 501 |
if distance < best_distance { |
| 502 |
best = index; |
| 503 |
best_distance = distance; |
| 504 |
} |
| 505 |
} |
| 506 |
best |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
pub fn quantize_against(fg: Rgb, bg: Rgb, palette: &[Rgb]) -> usize { |
| 534 |
assert!(!palette.is_empty(), "a palette needs at least one color"); |
| 535 |
let shown = palette[quantize(bg, palette)]; |
| 536 |
|
| 537 |
let mut order: Vec<usize> = (0..palette.len()).collect(); |
| 538 |
order.sort_by(|a, b| { |
| 539 |
oklab_distance(fg, palette[*a]).total_cmp(&oklab_distance(fg, palette[*b])) |
| 540 |
}); |
| 541 |
|
| 542 |
order |
| 543 |
.iter() |
| 544 |
.copied() |
| 545 |
.find(|index| wcag_contrast(palette[*index], shown) >= DISTINCT) |
| 546 |
.unwrap_or_else(|| { |
| 547 |
order |
| 548 |
.iter() |
| 549 |
.copied() |
| 550 |
.max_by(|a, b| { |
| 551 |
wcag_contrast(palette[*a], shown).total_cmp(&wcag_contrast(palette[*b], shown)) |
| 552 |
}) |
| 553 |
.expect("the palette is not empty") |
| 554 |
}) |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
pub const BASE_INTENTS: &[(&str, &str)] = &[ |
| 565 |
("surface.page", "surface-page"), |
| 566 |
("surface.raised", "surface-raised"), |
| 567 |
("surface.sunken", "surface-sunken"), |
| 568 |
("surface.overlay", "surface-overlay"), |
| 569 |
("content.primary", "content"), |
| 570 |
("content.secondary", "content-secondary"), |
| 571 |
("content.muted", "content-muted"), |
| 572 |
("action.primary", "action"), |
| 573 |
("status.danger", "danger"), |
| 574 |
("status.success", "success"), |
| 575 |
("status.warning", "warning"), |
| 576 |
("status.info", "info"), |
| 577 |
("line.border", "border"), |
| 578 |
("category.one", "category-one"), |
| 579 |
("category.two", "category-two"), |
| 580 |
("category.three", "category-three"), |
| 581 |
("category.four", "category-four"), |
| 582 |
("category.five", "category-five"), |
| 583 |
("category.six", "category-six"), |
| 584 |
]; |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
#[derive(Debug, Clone, Serialize)] |
| 589 |
#[serde(rename_all = "camelCase")] |
| 590 |
pub struct SemanticTokens { |
| 591 |
pub meta: ThemeMeta, |
| 592 |
|
| 593 |
pub intents: BTreeMap<String, String>, |
| 594 |
} |
| 595 |
|
| 596 |
impl SemanticTokens { |
| 597 |
|
| 598 |
pub fn hex(&self, key: &str) -> Option<&str> { |
| 599 |
self.intents.get(key).map(String::as_str) |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
pub fn rgb(&self, key: &str) -> Option<(u8, u8, u8)> { |
| 609 |
self.intents |
| 610 |
.get(key) |
| 611 |
.and_then(|h| Rgb::from_hex(h)) |
| 612 |
.map(Rgb::tuple) |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
pub fn rgba(&self, key: &str) -> Option<(u8, u8, u8, u8)> { |
| 627 |
let value = self.intents.get(key)?; |
| 628 |
if let Some(rgb) = Rgb::from_hex(value) { |
| 629 |
let (r, g, b) = rgb.tuple(); |
| 630 |
return Some((r, g, b, 255)); |
| 631 |
} |
| 632 |
let inner = value.strip_prefix("rgba(")?.strip_suffix(')')?; |
| 633 |
let mut parts = inner.split(',').map(str::trim); |
| 634 |
let r = parts.next()?.parse().ok()?; |
| 635 |
let g = parts.next()?.parse().ok()?; |
| 636 |
let b = parts.next()?.parse().ok()?; |
| 637 |
let alpha: f32 = parts.next()?.parse().ok()?; |
| 638 |
if parts.next().is_some() || !(0.0..=1.0).contains(&alpha) { |
| 639 |
return None; |
| 640 |
} |
| 641 |
Some((r, g, b, (alpha * 255.0).round() as u8)) |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
pub fn resolve(theme: &ThemeColors) -> SemanticTokens { |
| 654 |
let mut intents: BTreeMap<String, String> = BTreeMap::new(); |
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
for (src, token) in BASE_INTENTS { |
| 662 |
if let Some(rgb) = theme.colors.get(*src).and_then(|v| Rgb::from_hex(v)) { |
| 663 |
intents.insert((*token).to_string(), rgb.to_hex()); |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
|
| 668 |
let get = |m: &BTreeMap<String, String>, k: &str| m.get(k).and_then(|h| Rgb::from_hex(h)); |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
let mut derived: Vec<(String, Rgb)> = Vec::new(); |
| 673 |
if let Some(action) = get(&intents, "action") { |
| 674 |
derived.push(("action-hover".into(), lighten(action, 0.05))); |
| 675 |
derived.push(("content-on-action".into(), readable_on(action))); |
| 676 |
derived.push(("focus-ring".into(), action)); |
| 677 |
} |
| 678 |
if let Some(page) = get(&intents, "surface-page") { |
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
let mut o = page.to_oklab(); |
| 684 |
o.l = 0.08; |
| 685 |
let s = Rgb::from_oklab(o); |
| 686 |
intents.insert( |
| 687 |
"overlay".into(), |
| 688 |
format!("rgba({}, {}, {}, 0.5)", s.r, s.g, s.b), |
| 689 |
); |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
intents.insert( |
| 716 |
"elevation".into(), |
| 717 |
format!("rgba({}, {}, {}, 0.18)", s.r, s.g, s.b), |
| 718 |
); |
| 719 |
} |
| 720 |
if let Some(raised) = get(&intents, "surface-raised") { |
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
derived.push(("bevel-light".into(), lighten(raised, 0.14))); |
| 740 |
derived.push(("bevel-dark".into(), darken(raised, 0.18))); |
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
if let Some(content) = get(&intents, "content") { |
| 766 |
let content_is_darker = content.to_oklab().l < raised.to_oklab().l; |
| 767 |
let well = if content_is_darker { |
| 768 |
lighten(raised, 0.07) |
| 769 |
} else { |
| 770 |
darken(raised, 0.09) |
| 771 |
}; |
| 772 |
derived.push(("surface-well".into(), well)); |
| 773 |
} |
| 774 |
} |
| 775 |
if let Some(sunken) = get(&intents, "surface-sunken") { |
| 776 |
derived.push(("hover-surface".into(), sunken)); |
| 777 |
} |
| 778 |
if let Some(border) = get(&intents, "border") { |
| 779 |
derived.push(("border-strong".into(), darken(border, 0.05))); |
| 780 |
} |
| 781 |
|
| 782 |
for (token, rgb) in derived { |
| 783 |
intents.insert(token, rgb.to_hex()); |
| 784 |
} |
| 785 |
|
| 786 |
SemanticTokens { |
| 787 |
meta: theme.meta.clone(), |
| 788 |
intents, |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
pub fn intent_css_declarations(tokens: &SemanticTokens) -> String { |
| 795 |
let mut out = String::new(); |
| 796 |
for (token, hex) in &tokens.intents { |
| 797 |
out.push_str(" --"); |
| 798 |
out.push_str(token); |
| 799 |
out.push_str(": "); |
| 800 |
out.push_str(hex); |
| 801 |
out.push_str(";\n"); |
| 802 |
} |
| 803 |
out |
| 804 |
} |
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
pub fn intent_css_vars(tokens: &SemanticTokens) -> String { |
| 809 |
format!(":root {{\n{}}}\n", intent_css_declarations(tokens)) |
| 810 |
} |
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
pub fn validate_theme_id(id: &str) -> Result<(), String> { |
| 818 |
if !id |
| 819 |
.chars() |
| 820 |
.all(|c| c.is_alphanumeric() || c == '-' || c == '_') |
| 821 |
{ |
| 822 |
return Err(format!("Invalid theme ID: {id}")); |
| 823 |
} |
| 824 |
Ok(()) |
| 825 |
} |
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
pub fn parse_meta(id: &str, table: &toml::Table, is_custom: bool) -> ThemeMeta { |
| 831 |
let meta = table.get("meta").and_then(|m| m.as_table()); |
| 832 |
let name = meta |
| 833 |
.and_then(|m| m.get("name")) |
| 834 |
.and_then(|v| v.as_str()) |
| 835 |
.unwrap_or(id) |
| 836 |
.to_string(); |
| 837 |
let variant = meta |
| 838 |
.and_then(|m| m.get("variant")) |
| 839 |
.and_then(|v| v.as_str()) |
| 840 |
.unwrap_or("dark") |
| 841 |
.to_string(); |
| 842 |
|
| 843 |
ThemeMeta { |
| 844 |
id: id.to_string(), |
| 845 |
name, |
| 846 |
variant, |
| 847 |
is_custom, |
| 848 |
} |
| 849 |
} |
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)] |
| 873 |
#[serde(rename_all = "kebab-case")] |
| 874 |
pub enum Variant { |
| 875 |
Light, |
| 876 |
Dark, |
| 877 |
HighContrast, |
| 878 |
} |
| 879 |
|
| 880 |
impl Variant { |
| 881 |
|
| 882 |
#[must_use] |
| 883 |
pub const fn as_str(self) -> &'static str { |
| 884 |
match self { |
| 885 |
Variant::Light => "light", |
| 886 |
Variant::Dark => "dark", |
| 887 |
Variant::HighContrast => "high-contrast", |
| 888 |
} |
| 889 |
} |
| 890 |
|
| 891 |
|
| 892 |
#[must_use] |
| 893 |
pub fn parse(raw: &str) -> Option<Self> { |
| 894 |
match raw { |
| 895 |
"light" => Some(Variant::Light), |
| 896 |
"dark" => Some(Variant::Dark), |
| 897 |
"high-contrast" => Some(Variant::HighContrast), |
| 898 |
_ => None, |
| 899 |
} |
| 900 |
} |
| 901 |
} |
| 902 |
|
| 903 |
impl std::fmt::Display for Variant { |
| 904 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 905 |
f.write_str(self.as_str()) |
| 906 |
} |
| 907 |
} |
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
impl From<&str> for Variant { |
| 913 |
fn from(raw: &str) -> Self { |
| 914 |
Variant::parse(raw).unwrap_or(Variant::Dark) |
| 915 |
} |
| 916 |
} |
| 917 |
|
| 918 |
impl ThemeMeta { |
| 919 |
|
| 920 |
#[must_use] |
| 921 |
pub fn kind(&self) -> Variant { |
| 922 |
Variant::from(self.variant.as_str()) |
| 923 |
} |
| 924 |
} |
| 925 |
|
| 926 |
|
| 927 |
pub const FOLLOW: &str = "system"; |
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
#[derive(Debug, Clone, PartialEq, Eq, Default)] |
| 936 |
pub enum ThemeSelection { |
| 937 |
|
| 938 |
#[default] |
| 939 |
Follow, |
| 940 |
|
| 941 |
Fixed(String), |
| 942 |
} |
| 943 |
|
| 944 |
impl ThemeSelection { |
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
#[must_use] |
| 950 |
pub fn parse(raw: Option<&str>) -> Self { |
| 951 |
match raw.map(str::trim) { |
| 952 |
None | Some("" | FOLLOW) => ThemeSelection::Follow, |
| 953 |
Some(id) => ThemeSelection::Fixed(id.to_string()), |
| 954 |
} |
| 955 |
} |
| 956 |
|
| 957 |
|
| 958 |
#[must_use] |
| 959 |
pub fn as_str(&self) -> &str { |
| 960 |
match self { |
| 961 |
ThemeSelection::Follow => FOLLOW, |
| 962 |
ThemeSelection::Fixed(id) => id, |
| 963 |
} |
| 964 |
} |
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
#[must_use] |
| 983 |
pub fn resolve( |
| 984 |
&self, |
| 985 |
ambient: Variant, |
| 986 |
defaults: &ThemeDefaults, |
| 987 |
available: &[ThemeMeta], |
| 988 |
) -> String { |
| 989 |
let installed = |id: &str| available.iter().any(|meta| meta.id == id); |
| 990 |
|
| 991 |
if let ThemeSelection::Fixed(id) = self |
| 992 |
&& installed(id) |
| 993 |
{ |
| 994 |
return id.clone(); |
| 995 |
} |
| 996 |
|
| 997 |
let preferred = defaults.for_variant(ambient); |
| 998 |
if installed(preferred) { |
| 999 |
return preferred.to_string(); |
| 1000 |
} |
| 1001 |
available |
| 1002 |
.iter() |
| 1003 |
.find(|meta| meta.kind() == ambient) |
| 1004 |
.map_or_else(|| preferred.to_string(), |meta| meta.id.clone()) |
| 1005 |
} |
| 1006 |
} |
| 1007 |
|
| 1008 |
impl std::fmt::Display for ThemeSelection { |
| 1009 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 1010 |
f.write_str(self.as_str()) |
| 1011 |
} |
| 1012 |
} |
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
#[derive(Debug, Clone)] |
| 1019 |
pub struct ThemeDefaults { |
| 1020 |
light: String, |
| 1021 |
dark: String, |
| 1022 |
high_contrast: Option<String>, |
| 1023 |
} |
| 1024 |
|
| 1025 |
impl ThemeDefaults { |
| 1026 |
pub fn new(light: impl Into<String>, dark: impl Into<String>) -> Self { |
| 1027 |
Self { |
| 1028 |
light: light.into(), |
| 1029 |
dark: dark.into(), |
| 1030 |
high_contrast: None, |
| 1031 |
} |
| 1032 |
} |
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
#[must_use] |
| 1037 |
pub fn high_contrast(mut self, id: impl Into<String>) -> Self { |
| 1038 |
self.high_contrast = Some(id.into()); |
| 1039 |
self |
| 1040 |
} |
| 1041 |
|
| 1042 |
#[must_use] |
| 1043 |
pub fn for_variant(&self, variant: Variant) -> &str { |
| 1044 |
match variant { |
| 1045 |
Variant::Light => &self.light, |
| 1046 |
Variant::Dark => &self.dark, |
| 1047 |
Variant::HighContrast => self.high_contrast.as_ref().unwrap_or(&self.dark), |
| 1048 |
} |
| 1049 |
} |
| 1050 |
} |
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
#[derive(Debug, Default, Clone)] |
| 1075 |
pub struct ThemeDirs { |
| 1076 |
bundled: Vec<PathBuf>, |
| 1077 |
system: Vec<PathBuf>, |
| 1078 |
custom: Option<PathBuf>, |
| 1079 |
} |
| 1080 |
|
| 1081 |
impl ThemeDirs { |
| 1082 |
#[must_use] |
| 1083 |
pub fn new() -> Self { |
| 1084 |
Self::default() |
| 1085 |
} |
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
#[must_use] |
| 1093 |
pub fn bundled(mut self, dir: Option<PathBuf>) -> Self { |
| 1094 |
self.bundled.extend(dir); |
| 1095 |
self |
| 1096 |
} |
| 1097 |
|
| 1098 |
|
| 1099 |
#[must_use] |
| 1100 |
pub fn system(mut self, dir: Option<PathBuf>) -> Self { |
| 1101 |
self.system.extend(dir); |
| 1102 |
self |
| 1103 |
} |
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
#[must_use] |
| 1108 |
pub fn custom(mut self, dir: Option<PathBuf>) -> Self { |
| 1109 |
self.custom = dir; |
| 1110 |
self |
| 1111 |
} |
| 1112 |
|
| 1113 |
|
| 1114 |
#[must_use] |
| 1115 |
pub fn build(self) -> Vec<(PathBuf, bool)> { |
| 1116 |
let mut dirs = Vec::new(); |
| 1117 |
for dir in self.bundled.into_iter().chain(self.system) { |
| 1118 |
if dir.is_dir() { |
| 1119 |
dirs.push((dir, false)); |
| 1120 |
} |
| 1121 |
} |
| 1122 |
if let Some(dir) = self.custom |
| 1123 |
&& dir.is_dir() |
| 1124 |
{ |
| 1125 |
dirs.push((dir, true)); |
| 1126 |
} |
| 1127 |
dirs |
| 1128 |
} |
| 1129 |
} |
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
pub fn extract_colors(table: &toml::Table) -> HashMap<String, String> { |
| 1134 |
let mut colors = HashMap::new(); |
| 1135 |
for section in COLOR_SECTIONS { |
| 1136 |
if let Some(sect) = table.get(*section).and_then(|s| s.as_table()) { |
| 1137 |
for (key, val) in sect { |
| 1138 |
if let Some(color) = val.as_str() { |
| 1139 |
colors.insert(format!("{section}.{key}"), color.to_string()); |
| 1140 |
} |
| 1141 |
} |
| 1142 |
} |
| 1143 |
} |
| 1144 |
colors |
| 1145 |
} |
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
pub fn list_themes_from_dirs(dirs: &[(PathBuf, bool)]) -> Vec<ThemeMeta> { |
| 1152 |
let mut seen: HashMap<String, ThemeMeta> = HashMap::new(); |
| 1153 |
|
| 1154 |
for (dir, is_custom) in dirs { |
| 1155 |
let Ok(entries) = std::fs::read_dir(dir) else { |
| 1156 |
continue; |
| 1157 |
}; |
| 1158 |
|
| 1159 |
for entry in entries { |
| 1160 |
let Ok(entry) = entry else { |
| 1161 |
continue; |
| 1162 |
}; |
| 1163 |
let path = entry.path(); |
| 1164 |
if path.extension().and_then(|e| e.to_str()) != Some("toml") { |
| 1165 |
continue; |
| 1166 |
} |
| 1167 |
|
| 1168 |
let id = path |
| 1169 |
.file_stem() |
| 1170 |
.and_then(|s| s.to_str()) |
| 1171 |
.unwrap_or_default() |
| 1172 |
.to_string(); |
| 1173 |
|
| 1174 |
let Ok(content) = std::fs::read_to_string(&path) else { |
| 1175 |
continue; |
| 1176 |
}; |
| 1177 |
let table: toml::Table = match content.parse() { |
| 1178 |
Ok(t) => t, |
| 1179 |
Err(_) => continue, |
| 1180 |
}; |
| 1181 |
|
| 1182 |
seen.insert(id.clone(), parse_meta(&id, &table, *is_custom)); |
| 1183 |
} |
| 1184 |
} |
| 1185 |
|
| 1186 |
let mut themes: Vec<ThemeMeta> = seen.into_values().collect(); |
| 1187 |
themes.sort_by(|a, b| a.name.cmp(&b.name)); |
| 1188 |
themes |
| 1189 |
} |
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
pub fn find_theme_path(dirs: &[(PathBuf, bool)], id: &str) -> Option<(PathBuf, bool)> { |
| 1196 |
let filename = format!("{id}.toml"); |
| 1197 |
|
| 1198 |
for (dir, is_custom) in dirs.iter().rev() { |
| 1199 |
let path = dir.join(&filename); |
| 1200 |
if path.is_file() { |
| 1201 |
return Some((path, *is_custom)); |
| 1202 |
} |
| 1203 |
} |
| 1204 |
|
| 1205 |
None |
| 1206 |
} |
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
pub fn parse_theme_str(id: &str, content: &str, is_custom: bool) -> Result<ThemeColors, String> { |
| 1211 |
validate_theme_id(id)?; |
| 1212 |
let table: toml::Table = content |
| 1213 |
.parse() |
| 1214 |
.map_err(|e| format!("Failed to parse theme '{id}': {e}"))?; |
| 1215 |
let meta = parse_meta(id, &table, is_custom); |
| 1216 |
let colors = extract_colors(&table); |
| 1217 |
Ok(ThemeColors { meta, colors }) |
| 1218 |
} |
| 1219 |
|
| 1220 |
|
| 1221 |
pub fn load_theme(dirs: &[(PathBuf, bool)], id: &str) -> Result<ThemeColors, String> { |
| 1222 |
validate_theme_id(id)?; |
| 1223 |
|
| 1224 |
let (path, is_custom) = |
| 1225 |
find_theme_path(dirs, id).ok_or_else(|| format!("Theme '{id}' not found"))?; |
| 1226 |
|
| 1227 |
let content = std::fs::read_to_string(&path) |
| 1228 |
.map_err(|e| format!("Failed to read {}: {}", path.display(), e))?; |
| 1229 |
|
| 1230 |
let table: toml::Table = content |
| 1231 |
.parse() |
| 1232 |
.map_err(|e| format!("Failed to parse {}: {}", path.display(), e))?; |
| 1233 |
|
| 1234 |
let meta = parse_meta(id, &table, is_custom); |
| 1235 |
let colors = extract_colors(&table); |
| 1236 |
|
| 1237 |
Ok(ThemeColors { meta, colors }) |
| 1238 |
} |
| 1239 |
|
| 1240 |
|
| 1241 |
pub fn load_semantic(dirs: &[(PathBuf, bool)], id: &str) -> Result<SemanticTokens, String> { |
| 1242 |
Ok(resolve(&load_theme(dirs, id)?)) |
| 1243 |
} |
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
pub fn import_theme(source_path: &Path, custom_dir: &Path) -> Result<ThemeMeta, String> { |
| 1250 |
let content = std::fs::read_to_string(source_path) |
| 1251 |
.map_err(|e| format!("Failed to read {}: {}", source_path.display(), e))?; |
| 1252 |
|
| 1253 |
let table: toml::Table = content.parse().map_err(|e| format!("Invalid TOML: {e}"))?; |
| 1254 |
|
| 1255 |
let has_colors = COLOR_SECTIONS |
| 1256 |
.iter() |
| 1257 |
.any(|s| table.get(*s).and_then(|v| v.as_table()).is_some()); |
| 1258 |
if !has_colors { |
| 1259 |
return Err(format!( |
| 1260 |
"Theme file must have at least one color section ({})", |
| 1261 |
COLOR_SECTIONS.join(", ") |
| 1262 |
)); |
| 1263 |
} |
| 1264 |
|
| 1265 |
let id = source_path |
| 1266 |
.file_stem() |
| 1267 |
.and_then(|s| s.to_str()) |
| 1268 |
.ok_or("Invalid file name")? |
| 1269 |
.to_string(); |
| 1270 |
validate_theme_id(&id)?; |
| 1271 |
|
| 1272 |
std::fs::create_dir_all(custom_dir) |
| 1273 |
.map_err(|e| format!("Failed to create {}: {}", custom_dir.display(), e))?; |
| 1274 |
|
| 1275 |
let dest = custom_dir.join(format!("{id}.toml")); |
| 1276 |
std::fs::copy(source_path, &dest).map_err(|e| format!("Failed to copy theme: {e}"))?; |
| 1277 |
|
| 1278 |
Ok(parse_meta(&id, &table, true)) |
| 1279 |
} |
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
pub fn delete_theme(custom_dir: &Path, id: &str) -> Result<(), String> { |
| 1286 |
validate_theme_id(id)?; |
| 1287 |
|
| 1288 |
let path = custom_dir.join(format!("{id}.toml")); |
| 1289 |
if !path.is_file() { |
| 1290 |
return Err(format!("Custom theme '{id}' not found")); |
| 1291 |
} |
| 1292 |
|
| 1293 |
std::fs::remove_file(&path).map_err(|e| format!("Failed to delete {}: {}", path.display(), e)) |
| 1294 |
} |
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
#[derive(Debug, Clone, Serialize)] |
| 1299 |
#[serde(rename_all = "camelCase")] |
| 1300 |
pub struct ThemePreview { |
| 1301 |
pub meta: ThemeMeta, |
| 1302 |
|
| 1303 |
pub background: Option<String>, |
| 1304 |
|
| 1305 |
pub foreground: Option<String>, |
| 1306 |
|
| 1307 |
pub accent: Option<String>, |
| 1308 |
|
| 1309 |
pub border: Option<String>, |
| 1310 |
} |
| 1311 |
|
| 1312 |
fn color_at(table: &toml::Table, section: &str, key: &str) -> Option<String> { |
| 1313 |
table |
| 1314 |
.get(section) |
| 1315 |
.and_then(|s| s.as_table()) |
| 1316 |
.and_then(|s| s.get(key)) |
| 1317 |
.and_then(|v| v.as_str()) |
| 1318 |
.map(std::string::ToString::to_string) |
| 1319 |
} |
| 1320 |
|
| 1321 |
|
| 1322 |
pub fn load_theme_preview(dirs: &[(PathBuf, bool)], id: &str) -> Result<ThemePreview, String> { |
| 1323 |
validate_theme_id(id)?; |
| 1324 |
|
| 1325 |
let (path, is_custom) = |
| 1326 |
find_theme_path(dirs, id).ok_or_else(|| format!("Theme '{id}' not found"))?; |
| 1327 |
|
| 1328 |
let content = std::fs::read_to_string(&path) |
| 1329 |
.map_err(|e| format!("Failed to read {}: {}", path.display(), e))?; |
| 1330 |
|
| 1331 |
let table: toml::Table = content |
| 1332 |
.parse() |
| 1333 |
.map_err(|e| format!("Failed to parse {}: {}", path.display(), e))?; |
| 1334 |
|
| 1335 |
Ok(ThemePreview { |
| 1336 |
meta: parse_meta(id, &table, is_custom), |
| 1337 |
background: color_at(&table, "surface", "page"), |
| 1338 |
foreground: color_at(&table, "content", "primary"), |
| 1339 |
accent: color_at(&table, "action", "primary"), |
| 1340 |
border: color_at(&table, "line", "border"), |
| 1341 |
}) |
| 1342 |
} |
| 1343 |
|
| 1344 |
|
| 1345 |
pub fn export_theme(dirs: &[(PathBuf, bool)], id: &str, dest_path: &Path) -> Result<(), String> { |
| 1346 |
validate_theme_id(id)?; |
| 1347 |
|
| 1348 |
let (source, _) = find_theme_path(dirs, id).ok_or_else(|| format!("Theme '{id}' not found"))?; |
| 1349 |
|
| 1350 |
std::fs::copy(&source, dest_path).map_err(|e| format!("Failed to export theme: {e}"))?; |
| 1351 |
|
| 1352 |
Ok(()) |
| 1353 |
} |
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
static EMBEDDED: include_dir::Dir<'static> = |
| 1361 |
include_dir::include_dir!("$CARGO_MANIFEST_DIR/themes"); |
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
pub fn embedded_themes() -> impl Iterator<Item = (&'static str, &'static str)> { |
| 1375 |
EMBEDDED.files().filter_map(|file| { |
| 1376 |
let path = file.path(); |
| 1377 |
if path.extension().and_then(|e| e.to_str()) != Some("toml") { |
| 1378 |
return None; |
| 1379 |
} |
| 1380 |
let id = path.file_stem()?.to_str()?; |
| 1381 |
Some((id, file.contents_utf8()?)) |
| 1382 |
}) |
| 1383 |
} |
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
pub fn bundled_themes_dir() -> Option<PathBuf> { |
| 1398 |
let themes = Path::new(env!("CARGO_MANIFEST_DIR")).join("themes"); |
| 1399 |
if themes.is_dir() { Some(themes) } else { None } |
| 1400 |
} |
| 1401 |
|
| 1402 |
#[cfg(test)] |
| 1403 |
mod tests { |
| 1404 |
use super::*; |
| 1405 |
use std::fs; |
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
#[test] |
| 1410 |
fn validate_theme_id_alphanumeric() { |
| 1411 |
assert!(validate_theme_id("darkmode").is_ok()); |
| 1412 |
assert!(validate_theme_id("Theme123").is_ok()); |
| 1413 |
} |
| 1414 |
|
| 1415 |
#[test] |
| 1416 |
fn validate_theme_id_hyphens_underscores() { |
| 1417 |
assert!(validate_theme_id("dark-mode").is_ok()); |
| 1418 |
assert!(validate_theme_id("my_theme_v2").is_ok()); |
| 1419 |
} |
| 1420 |
|
| 1421 |
#[test] |
| 1422 |
fn validate_theme_id_rejects_path_traversal() { |
| 1423 |
assert!(validate_theme_id("../etc/passwd").is_err()); |
| 1424 |
assert!(validate_theme_id("foo/bar").is_err()); |
| 1425 |
assert!(validate_theme_id("theme.toml").is_err()); |
| 1426 |
} |
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
#[test] |
| 1431 |
fn the_ansi_palette_is_sixteen_distinct_colors() { |
| 1432 |
let mut seen: Vec<(u8, u8, u8)> = ANSI_16.iter().map(|c| c.tuple()).collect(); |
| 1433 |
seen.sort_unstable(); |
| 1434 |
seen.dedup(); |
| 1435 |
assert_eq!(seen.len(), 16); |
| 1436 |
} |
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
#[test] |
| 1445 |
fn every_ansi_slot_names_an_intent_on_either_polarity() { |
| 1446 |
for variant in ["light", "dark", "high-contrast"] { |
| 1447 |
for index in 0..16 { |
| 1448 |
assert!( |
| 1449 |
ansi_intent(index, variant).is_some(), |
| 1450 |
"slot {index} unanswered on {variant}" |
| 1451 |
); |
| 1452 |
} |
| 1453 |
assert_eq!(ansi_intent(16, variant), None); |
| 1454 |
} |
| 1455 |
} |
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
#[test] |
| 1462 |
fn ansi_zero_is_darker_than_ansi_fifteen_on_either_polarity() { |
| 1463 |
for id in ["akari-dawn", "akari-night"] { |
| 1464 |
let theme = bundled(id); |
| 1465 |
let slot = |i: usize| -> Rgb { |
| 1466 |
let key = ansi_intent(i, &theme.meta.variant).expect("in range"); |
| 1467 |
Rgb::from_hex(theme.colors.get(key).expect("theme carries it")).expect("valid hex") |
| 1468 |
}; |
| 1469 |
assert!( |
| 1470 |
rel_luminance(slot(0)) < rel_luminance(slot(15)), |
| 1471 |
"{id}: ANSI 0 {} should be darker than ANSI 15 {}", |
| 1472 |
slot(0).to_hex(), |
| 1473 |
slot(15).to_hex(), |
| 1474 |
); |
| 1475 |
} |
| 1476 |
} |
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
#[test] |
| 1482 |
fn the_container_slot_and_the_text_slot_stay_legible() { |
| 1483 |
for id in ["akari-dawn", "akari-night"] { |
| 1484 |
let theme = bundled(id); |
| 1485 |
let slot = |i: usize| -> Rgb { |
| 1486 |
let key = ansi_intent(i, &theme.meta.variant).expect("in range"); |
| 1487 |
Rgb::from_hex(theme.colors.get(key).expect("theme carries it")).expect("valid hex") |
| 1488 |
}; |
| 1489 |
let contrast = wcag_contrast(slot(0), slot(7)); |
| 1490 |
assert!(contrast >= 4.5, "{id}: ANSI 0 on ANSI 7 is {contrast:.2}:1"); |
| 1491 |
} |
| 1492 |
} |
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
#[test] |
| 1498 |
fn the_chromatic_slots_do_not_vary_with_polarity() { |
| 1499 |
for index in [1, 2, 3, 4, 5, 6, 9, 10, 11, 12, 13, 14] { |
| 1500 |
assert_eq!( |
| 1501 |
ansi_intent(index, "light"), |
| 1502 |
ansi_intent(index, "dark"), |
| 1503 |
"slot {index} moved with polarity" |
| 1504 |
); |
| 1505 |
} |
| 1506 |
} |
| 1507 |
|
| 1508 |
fn bundled(id: &str) -> ThemeColors { |
| 1509 |
let dir = bundled_themes_dir().expect("makeover ships its themes"); |
| 1510 |
load_theme(&[(dir, false)], id).expect("the akari pair ships") |
| 1511 |
} |
| 1512 |
|
| 1513 |
#[test] |
| 1514 |
fn quantize_picks_the_obvious_entry() { |
| 1515 |
let black = Rgb { r: 0, g: 0, b: 0 }; |
| 1516 |
let white = Rgb { |
| 1517 |
r: 255, |
| 1518 |
g: 255, |
| 1519 |
b: 255, |
| 1520 |
}; |
| 1521 |
assert_eq!(quantize(black, &ANSI_16), 0); |
| 1522 |
assert_eq!(quantize(white, &ANSI_16), 15); |
| 1523 |
} |
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
#[test] |
| 1530 |
fn two_colors_can_quantize_to_one_entry() { |
| 1531 |
let page = Rgb::from_hex("#a8a8a8").unwrap(); |
| 1532 |
let border = Rgb::from_hex("#b4b4b4").unwrap(); |
| 1533 |
|
| 1534 |
assert_eq!(quantize(page, &ANSI_16), quantize(border, &ANSI_16)); |
| 1535 |
assert_ne!( |
| 1536 |
quantize_against(border, page, &ANSI_16), |
| 1537 |
quantize(page, &ANSI_16) |
| 1538 |
); |
| 1539 |
} |
| 1540 |
|
| 1541 |
#[test] |
| 1542 |
fn quantize_against_keeps_the_border_off_the_page() { |
| 1543 |
let page = Rgb::from_hex("#e4ded6").unwrap(); |
| 1544 |
let border = Rgb::from_hex("#7f786d").unwrap(); |
| 1545 |
|
| 1546 |
let shown_page = ANSI_16[quantize(page, &ANSI_16)]; |
| 1547 |
let shown_border = ANSI_16[quantize_against(border, page, &ANSI_16)]; |
| 1548 |
|
| 1549 |
assert!( |
| 1550 |
wcag_contrast(shown_border, shown_page) >= DISTINCT, |
| 1551 |
"border {} on page {} is {:.2}:1", |
| 1552 |
shown_border.to_hex(), |
| 1553 |
shown_page.to_hex(), |
| 1554 |
wcag_contrast(shown_border, shown_page) |
| 1555 |
); |
| 1556 |
} |
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
#[test] |
| 1561 |
fn quantize_against_leaves_a_readable_color_alone() { |
| 1562 |
let page = Rgb::from_hex("#e4ded6").unwrap(); |
| 1563 |
let text = Rgb::from_hex("#1a1816").unwrap(); |
| 1564 |
|
| 1565 |
assert_eq!( |
| 1566 |
quantize_against(text, page, &ANSI_16), |
| 1567 |
quantize(text, &ANSI_16) |
| 1568 |
); |
| 1569 |
} |
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
#[test] |
| 1575 |
fn an_impossible_palette_gets_the_most_legible_entry() { |
| 1576 |
let page = Rgb::from_hex("#ffffff").unwrap(); |
| 1577 |
let border = Rgb::from_hex("#fefefe").unwrap(); |
| 1578 |
let palette = [ |
| 1579 |
Rgb::from_hex("#ffffff").unwrap(), |
| 1580 |
Rgb::from_hex("#fdfdfd").unwrap(), |
| 1581 |
]; |
| 1582 |
|
| 1583 |
let chosen = palette[quantize_against(border, page, &palette)]; |
| 1584 |
assert_eq!(chosen.to_hex(), "#fdfdfd"); |
| 1585 |
} |
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
#[test] |
| 1590 |
fn parse_meta_with_name_and_variant() { |
| 1591 |
let table: toml::Table = "[meta]\nname = \"Nord\"\nvariant = \"light\"\n" |
| 1592 |
.parse() |
| 1593 |
.unwrap(); |
| 1594 |
let meta = parse_meta("nord", &table, false); |
| 1595 |
assert_eq!(meta.id, "nord"); |
| 1596 |
assert_eq!(meta.name, "Nord"); |
| 1597 |
assert_eq!(meta.variant, "light"); |
| 1598 |
assert!(!meta.is_custom); |
| 1599 |
} |
| 1600 |
|
| 1601 |
#[test] |
| 1602 |
fn parse_meta_defaults_to_id_and_dark() { |
| 1603 |
let table: toml::Table = "".parse().unwrap(); |
| 1604 |
let meta = parse_meta("fallback", &table, true); |
| 1605 |
assert_eq!(meta.name, "fallback"); |
| 1606 |
assert_eq!(meta.variant, "dark"); |
| 1607 |
assert!(meta.is_custom); |
| 1608 |
} |
| 1609 |
|
| 1610 |
|
| 1611 |
|
| 1612 |
#[test] |
| 1613 |
fn rgb_hex_roundtrip() { |
| 1614 |
assert_eq!( |
| 1615 |
Rgb::from_hex("#6196FF").unwrap(), |
| 1616 |
Rgb { |
| 1617 |
r: 0x61, |
| 1618 |
g: 0x96, |
| 1619 |
b: 0xff |
| 1620 |
} |
| 1621 |
); |
| 1622 |
assert_eq!( |
| 1623 |
Rgb::from_hex("#abc").unwrap(), |
| 1624 |
Rgb { |
| 1625 |
r: 0xaa, |
| 1626 |
g: 0xbb, |
| 1627 |
b: 0xcc |
| 1628 |
} |
| 1629 |
); |
| 1630 |
assert_eq!( |
| 1631 |
Rgb { |
| 1632 |
r: 0x61, |
| 1633 |
g: 0x96, |
| 1634 |
b: 0xff |
| 1635 |
} |
| 1636 |
.to_hex(), |
| 1637 |
"#6196ff" |
| 1638 |
); |
| 1639 |
assert!(Rgb::from_hex("not-a-color").is_none()); |
| 1640 |
} |
| 1641 |
|
| 1642 |
#[test] |
| 1643 |
fn oklab_roundtrips_within_tolerance() { |
| 1644 |
for hex in ["#6196ff", "#2e3440", "#ffffff", "#000000", "#c0392b"] { |
| 1645 |
let c = Rgb::from_hex(hex).unwrap(); |
| 1646 |
let back = Rgb::from_oklab(c.to_oklab()); |
| 1647 |
|
| 1648 |
assert!((c.r as i16 - back.r as i16).abs() <= 1, "{hex} r"); |
| 1649 |
assert!((c.g as i16 - back.g as i16).abs() <= 1, "{hex} g"); |
| 1650 |
assert!((c.b as i16 - back.b as i16).abs() <= 1, "{hex} b"); |
| 1651 |
} |
| 1652 |
} |
| 1653 |
|
| 1654 |
#[test] |
| 1655 |
fn wcag_contrast_known_pairs() { |
| 1656 |
let white = Rgb { |
| 1657 |
r: 255, |
| 1658 |
g: 255, |
| 1659 |
b: 255, |
| 1660 |
}; |
| 1661 |
let black = Rgb { r: 0, g: 0, b: 0 }; |
| 1662 |
assert!((wcag_contrast(white, black) - 21.0).abs() < 0.01); |
| 1663 |
assert!((wcag_contrast(white, white) - 1.0).abs() < 0.01); |
| 1664 |
} |
| 1665 |
|
| 1666 |
#[test] |
| 1667 |
fn readable_on_picks_by_wcag() { |
| 1668 |
assert_eq!( |
| 1669 |
readable_on(Rgb { |
| 1670 |
r: 255, |
| 1671 |
g: 255, |
| 1672 |
b: 255 |
| 1673 |
}), |
| 1674 |
Rgb { r: 0, g: 0, b: 0 } |
| 1675 |
); |
| 1676 |
assert_eq!( |
| 1677 |
readable_on(Rgb { r: 0, g: 0, b: 0 }), |
| 1678 |
Rgb { |
| 1679 |
r: 255, |
| 1680 |
g: 255, |
| 1681 |
b: 255 |
| 1682 |
} |
| 1683 |
); |
| 1684 |
|
| 1685 |
let action = Rgb::from_hex("#6196ff").unwrap(); |
| 1686 |
assert_eq!(readable_on(action), Rgb { r: 0, g: 0, b: 0 }); |
| 1687 |
} |
| 1688 |
|
| 1689 |
#[test] |
| 1690 |
fn lighten_darken_move_oklab_lightness() { |
| 1691 |
let c = Rgb::from_hex("#6196ff").unwrap(); |
| 1692 |
let l0 = c.to_oklab().l; |
| 1693 |
assert!(lighten(c, 0.05).to_oklab().l > l0); |
| 1694 |
assert!(darken(c, 0.05).to_oklab().l < l0); |
| 1695 |
} |
| 1696 |
|
| 1697 |
#[test] |
| 1698 |
fn mix_endpoints_and_midpoint() { |
| 1699 |
let a = Rgb::from_hex("#000000").unwrap(); |
| 1700 |
let b = Rgb::from_hex("#6196ff").unwrap(); |
| 1701 |
assert_eq!(mix(a, b, 0.0), a); |
| 1702 |
assert_eq!(mix(a, b, 1.0), b); |
| 1703 |
|
| 1704 |
let mid = mix(a, b, 0.5).to_oklab().l; |
| 1705 |
assert!(mid > a.to_oklab().l && mid < b.to_oklab().l); |
| 1706 |
} |
| 1707 |
|
| 1708 |
|
| 1709 |
|
| 1710 |
fn nord_toml() -> &'static str { |
| 1711 |
r##" |
| 1712 |
[meta] |
| 1713 |
name = "Nord" |
| 1714 |
variant = "dark" |
| 1715 |
|
| 1716 |
[surface] |
| 1717 |
page = "#2e3440" |
| 1718 |
raised = "#3b4252" |
| 1719 |
sunken = "#434c5e" |
| 1720 |
overlay = "#3b4252" |
| 1721 |
|
| 1722 |
[content] |
| 1723 |
primary = "#d8dee9" |
| 1724 |
secondary = "#e5e9f0" |
| 1725 |
muted = "#616e88" |
| 1726 |
|
| 1727 |
[action] |
| 1728 |
primary = "#81a1c1" |
| 1729 |
|
| 1730 |
[status] |
| 1731 |
danger = "#bf616a" |
| 1732 |
success = "#a3be8c" |
| 1733 |
warning = "#ebcb8b" |
| 1734 |
info = "#88c0d0" |
| 1735 |
|
| 1736 |
[line] |
| 1737 |
border = "#4c566a" |
| 1738 |
|
| 1739 |
[category] |
| 1740 |
one = "#bf616a" |
| 1741 |
two = "#a3be8c" |
| 1742 |
three = "#81a1c1" |
| 1743 |
four = "#ebcb8b" |
| 1744 |
five = "#b48ead" |
| 1745 |
six = "#88c0d0" |
| 1746 |
"## |
| 1747 |
} |
| 1748 |
|
| 1749 |
#[test] |
| 1750 |
fn extract_colors_reads_intent_sections() { |
| 1751 |
let table: toml::Table = nord_toml().parse().unwrap(); |
| 1752 |
let colors = extract_colors(&table); |
| 1753 |
assert_eq!(colors.get("surface.page").unwrap(), "#2e3440"); |
| 1754 |
assert_eq!(colors.get("content.primary").unwrap(), "#d8dee9"); |
| 1755 |
assert_eq!(colors.get("action.primary").unwrap(), "#81a1c1"); |
| 1756 |
assert_eq!(colors.get("status.danger").unwrap(), "#bf616a"); |
| 1757 |
assert_eq!(colors.get("line.border").unwrap(), "#4c566a"); |
| 1758 |
assert_eq!(colors.get("category.five").unwrap(), "#b48ead"); |
| 1759 |
assert_eq!(colors.len(), 19); |
| 1760 |
} |
| 1761 |
|
| 1762 |
#[test] |
| 1763 |
fn resolve_base_intents_passthrough() { |
| 1764 |
let theme = parse_theme_str("nord", nord_toml(), false).unwrap(); |
| 1765 |
let t = resolve(&theme); |
| 1766 |
assert_eq!(t.hex("surface-page"), Some("#2e3440")); |
| 1767 |
assert_eq!(t.hex("content"), Some("#d8dee9")); |
| 1768 |
assert_eq!(t.hex("content-muted"), Some("#616e88")); |
| 1769 |
assert_eq!(t.hex("action"), Some("#81a1c1")); |
| 1770 |
assert_eq!(t.hex("danger"), Some("#bf616a")); |
| 1771 |
assert_eq!(t.hex("border"), Some("#4c566a")); |
| 1772 |
assert_eq!(t.hex("category-five"), Some("#b48ead")); |
| 1773 |
} |
| 1774 |
|
| 1775 |
#[test] |
| 1776 |
fn resolve_derived_intents() { |
| 1777 |
let theme = parse_theme_str("nord", nord_toml(), false).unwrap(); |
| 1778 |
let t = resolve(&theme); |
| 1779 |
let action = Rgb::from_hex("#81a1c1").unwrap(); |
| 1780 |
let page = Rgb::from_hex("#2e3440").unwrap(); |
| 1781 |
let _ = page; |
| 1782 |
assert_eq!( |
| 1783 |
t.hex("action-hover").unwrap(), |
| 1784 |
lighten(action, 0.05).to_hex() |
| 1785 |
); |
| 1786 |
assert_eq!( |
| 1787 |
t.hex("content-on-action").unwrap(), |
| 1788 |
readable_on(action).to_hex() |
| 1789 |
); |
| 1790 |
assert_eq!(t.hex("focus-ring"), Some("#81a1c1")); |
| 1791 |
assert_eq!(t.hex("hover-surface"), Some("#434c5e")); |
| 1792 |
|
| 1793 |
|
| 1794 |
|
| 1795 |
assert!(t.hex("action-active").is_none()); |
| 1796 |
assert!(t.hex("danger-surface").is_none()); |
| 1797 |
assert!(t.hex("selection").is_none()); |
| 1798 |
assert!(t.hex("row-stripe").is_none()); |
| 1799 |
} |
| 1800 |
|
| 1801 |
#[test] |
| 1802 |
fn resolve_bevel_intents() { |
| 1803 |
let theme = parse_theme_str("nord", nord_toml(), false).unwrap(); |
| 1804 |
let t = resolve(&theme); |
| 1805 |
let raised = Rgb::from_hex("#3b4252").unwrap(); |
| 1806 |
assert_eq!( |
| 1807 |
t.hex("bevel-light").unwrap(), |
| 1808 |
lighten(raised, 0.14).to_hex() |
| 1809 |
); |
| 1810 |
assert_eq!(t.hex("bevel-dark").unwrap(), darken(raised, 0.18).to_hex()); |
| 1811 |
} |
| 1812 |
|
| 1813 |
|
| 1814 |
|
| 1815 |
|
| 1816 |
|
| 1817 |
|
| 1818 |
|
| 1819 |
|
| 1820 |
|
| 1821 |
#[test] |
| 1822 |
fn bevel_edges_are_distinct_from_their_face() { |
| 1823 |
const CANNOT_BEVEL: &[&str] = &["neobrute", "oxocarbon-light"]; |
| 1824 |
|
| 1825 |
let mut degenerate: Vec<String> = Vec::new(); |
| 1826 |
for (id, source) in embedded_themes() { |
| 1827 |
let theme = parse_theme_str(id, source, false).unwrap(); |
| 1828 |
let t = resolve(&theme); |
| 1829 |
let Some(raised) = t.hex("surface-raised") else { |
| 1830 |
continue; |
| 1831 |
}; |
| 1832 |
let light = t.hex("bevel-light").expect("raised implies bevel-light"); |
| 1833 |
let dark = t.hex("bevel-dark").expect("raised implies bevel-dark"); |
| 1834 |
if light == raised || dark == raised { |
| 1835 |
degenerate.push(id.to_string()); |
| 1836 |
} |
| 1837 |
} |
| 1838 |
degenerate.sort(); |
| 1839 |
|
| 1840 |
assert_eq!( |
| 1841 |
degenerate, CANNOT_BEVEL, |
| 1842 |
"themes whose raised surface cannot hold both bevel edges" |
| 1843 |
); |
| 1844 |
} |
| 1845 |
|
| 1846 |
|
| 1847 |
|
| 1848 |
#[test] |
| 1849 |
fn resolve_well_intent_follows_the_content_direction() { |
| 1850 |
|
| 1851 |
|
| 1852 |
let dark = resolve(&parse_theme_str("nord", nord_toml(), false).unwrap()); |
| 1853 |
let dark_raised = Rgb::from_hex("#3b4252").unwrap(); |
| 1854 |
assert_eq!( |
| 1855 |
dark.hex("surface-well").unwrap(), |
| 1856 |
darken(dark_raised, 0.09).to_hex() |
| 1857 |
); |
| 1858 |
|
| 1859 |
|
| 1860 |
let goingson = embedded_themes() |
| 1861 |
.into_iter() |
| 1862 |
.find(|(id, _)| *id == "goingson") |
| 1863 |
.expect("goingson is embedded") |
| 1864 |
.1; |
| 1865 |
let light = resolve(&parse_theme_str("goingson", goingson, false).unwrap()); |
| 1866 |
let light_raised = light |
| 1867 |
.hex("surface-raised") |
| 1868 |
.and_then(Rgb::from_hex) |
| 1869 |
.expect("goingson authors a raised surface"); |
| 1870 |
assert_eq!( |
| 1871 |
light.hex("surface-well").unwrap(), |
| 1872 |
lighten(light_raised, 0.07).to_hex() |
| 1873 |
); |
| 1874 |
} |
| 1875 |
|
| 1876 |
|
| 1877 |
|
| 1878 |
|
| 1879 |
|
| 1880 |
|
| 1881 |
|
| 1882 |
|
| 1883 |
#[test] |
| 1884 |
fn well_is_distinct_from_its_face() { |
| 1885 |
const CANNOT_WELL: &[&str] = &["neobrute", "oxocarbon-light"]; |
| 1886 |
|
| 1887 |
let mut degenerate: Vec<String> = Vec::new(); |
| 1888 |
for (id, source) in embedded_themes() { |
| 1889 |
let theme = parse_theme_str(id, source, false).unwrap(); |
| 1890 |
let t = resolve(&theme); |
| 1891 |
let Some(raised) = t.hex("surface-raised") else { |
| 1892 |
continue; |
| 1893 |
}; |
| 1894 |
let well = t.hex("surface-well").expect("raised implies surface-well"); |
| 1895 |
if well == raised { |
| 1896 |
degenerate.push(id.to_string()); |
| 1897 |
} |
| 1898 |
} |
| 1899 |
degenerate.sort(); |
| 1900 |
|
| 1901 |
assert_eq!( |
| 1902 |
degenerate, CANNOT_WELL, |
| 1903 |
"themes whose raised surface cannot hold a well" |
| 1904 |
); |
| 1905 |
} |
| 1906 |
|
| 1907 |
|
| 1908 |
|
| 1909 |
|
| 1910 |
|
| 1911 |
|
| 1912 |
|
| 1913 |
|
| 1914 |
|
| 1915 |
|
| 1916 |
#[test] |
| 1917 |
fn well_is_visible_against_its_face() { |
| 1918 |
|
| 1919 |
const MIN_DELTA_L: f32 = 0.02; |
| 1920 |
const CANNOT_HOLD_A_VISIBLE_WELL: &[&str] = |
| 1921 |
&["neobrute", "oxocarbon-light", "rosepine-dawn"]; |
| 1922 |
|
| 1923 |
let mut invisible: Vec<String> = Vec::new(); |
| 1924 |
for (id, source) in embedded_themes() { |
| 1925 |
let theme = parse_theme_str(id, source, false).unwrap(); |
| 1926 |
let t = resolve(&theme); |
| 1927 |
let (Some(raised), Some(well)) = ( |
| 1928 |
t.hex("surface-raised").and_then(Rgb::from_hex), |
| 1929 |
t.hex("surface-well").and_then(Rgb::from_hex), |
| 1930 |
) else { |
| 1931 |
continue; |
| 1932 |
}; |
| 1933 |
if (well.to_oklab().l - raised.to_oklab().l).abs() < MIN_DELTA_L { |
| 1934 |
invisible.push(id.to_string()); |
| 1935 |
} |
| 1936 |
} |
| 1937 |
invisible.sort(); |
| 1938 |
|
| 1939 |
assert_eq!( |
| 1940 |
invisible, CANNOT_HOLD_A_VISIBLE_WELL, |
| 1941 |
"themes whose well is too close to its face to read as one" |
| 1942 |
); |
| 1943 |
} |
| 1944 |
|
| 1945 |
|
| 1946 |
|
| 1947 |
|
| 1948 |
|
| 1949 |
|
| 1950 |
|
| 1951 |
|
| 1952 |
|
| 1953 |
|
| 1954 |
|
| 1955 |
|
| 1956 |
|
| 1957 |
|
| 1958 |
|
| 1959 |
|
| 1960 |
|
| 1961 |
|
| 1962 |
|
| 1963 |
|
| 1964 |
|
| 1965 |
#[test] |
| 1966 |
fn raised_is_distinct_from_page() { |
| 1967 |
|
| 1968 |
|
| 1969 |
const MIN_DELTA_L: f32 = 0.05; |
| 1970 |
const CANNOT_LIFT_OFF_THE_PAGE: &[&str] = &[ |
| 1971 |
"akari-dawn", |
| 1972 |
"akari-night", |
| 1973 |
"ayu-light", |
| 1974 |
"ayu-mirage", |
| 1975 |
"catppuccin-latte", |
| 1976 |
"catppuccin-mocha", |
| 1977 |
"dawnfox", |
| 1978 |
"dracula", |
| 1979 |
"everforest", |
| 1980 |
"flatwhite", |
| 1981 |
"gruvbox-light", |
| 1982 |
"neobrute", |
| 1983 |
"one-dark", |
| 1984 |
"oxocarbon-dark", |
| 1985 |
"oxocarbon-light", |
| 1986 |
"poimandres", |
| 1987 |
"rosepine", |
| 1988 |
"rosepine-dawn", |
| 1989 |
"solarized-dark", |
| 1990 |
"tokyonight", |
| 1991 |
]; |
| 1992 |
|
| 1993 |
let mut flat: Vec<String> = Vec::new(); |
| 1994 |
for (id, source) in embedded_themes() { |
| 1995 |
let theme = parse_theme_str(id, source, false).unwrap(); |
| 1996 |
let t = resolve(&theme); |
| 1997 |
let (Some(page), Some(raised)) = ( |
| 1998 |
t.hex("surface-page").and_then(Rgb::from_hex), |
| 1999 |
t.hex("surface-raised").and_then(Rgb::from_hex), |
| 2000 |
) else { |
| 2001 |
continue; |
| 2002 |
}; |
| 2003 |
if (raised.to_oklab().l - page.to_oklab().l).abs() < MIN_DELTA_L { |
| 2004 |
flat.push(id.to_string()); |
| 2005 |
} |
| 2006 |
} |
| 2007 |
flat.sort(); |
| 2008 |
|
| 2009 |
assert_eq!( |
| 2010 |
flat, CANNOT_LIFT_OFF_THE_PAGE, |
| 2011 |
"themes whose raised surface is too close to the page to lift off it" |
| 2012 |
); |
| 2013 |
} |
| 2014 |
|
| 2015 |
|
| 2016 |
|
| 2017 |
|
| 2018 |
|
| 2019 |
|
| 2020 |
|
| 2021 |
|
| 2022 |
|
| 2023 |
|
| 2024 |
|
| 2025 |
|
| 2026 |
|
| 2027 |
|
| 2028 |
|
| 2029 |
|
| 2030 |
|
| 2031 |
|
| 2032 |
|
| 2033 |
|
| 2034 |
#[test] |
| 2035 |
fn a_sixteen_color_terminal_gets_one_bevel_edge_and_not_two() { |
| 2036 |
for (id, source) in embedded_themes() { |
| 2037 |
let theme = parse_theme_str(id, source, false).unwrap(); |
| 2038 |
let t = resolve(&theme); |
| 2039 |
let (Some(face), Some(light), Some(dark)) = ( |
| 2040 |
t.hex("surface-raised").and_then(Rgb::from_hex), |
| 2041 |
t.hex("bevel-light").and_then(Rgb::from_hex), |
| 2042 |
t.hex("bevel-dark").and_then(Rgb::from_hex), |
| 2043 |
) else { |
| 2044 |
continue; |
| 2045 |
}; |
| 2046 |
|
| 2047 |
let face_index = quantize(face, &ANSI_16); |
| 2048 |
let light_survives = quantize(light, &ANSI_16) != face_index; |
| 2049 |
let dark_survives = quantize(dark, &ANSI_16) != face_index; |
| 2050 |
assert!( |
| 2051 |
light_survives != dark_survives, |
| 2052 |
"{id}: expected exactly one bevel edge to survive 16 colors, \ |
| 2053 |
highlight {light_survives} shadow {dark_survives}" |
| 2054 |
); |
| 2055 |
|
| 2056 |
|
| 2057 |
assert_eq!( |
| 2058 |
quantize_against(light, face, &ANSI_16), |
| 2059 |
quantize_against(dark, face, &ANSI_16), |
| 2060 |
"{id}: quantize_against is expected to be unusable for a bevel pair" |
| 2061 |
); |
| 2062 |
} |
| 2063 |
} |
| 2064 |
|
| 2065 |
|
| 2066 |
|
| 2067 |
|
| 2068 |
|
| 2069 |
|
| 2070 |
|
| 2071 |
|
| 2072 |
|
| 2073 |
|
| 2074 |
#[test] |
| 2075 |
fn two_hundred_fifty_six_colors_keep_both_bevel_edges() { |
| 2076 |
const LOSES_AN_EDGE: &[&str] = &[ |
| 2077 |
"gruvbox-light", |
| 2078 |
"neobrute", |
| 2079 |
"oxocarbon-light", |
| 2080 |
"rosepine-dawn", |
| 2081 |
]; |
| 2082 |
|
| 2083 |
let mut lost: Vec<String> = Vec::new(); |
| 2084 |
for (id, source) in embedded_themes() { |
| 2085 |
let theme = parse_theme_str(id, source, false).unwrap(); |
| 2086 |
let t = resolve(&theme); |
| 2087 |
let (Some(face), Some(light), Some(dark)) = ( |
| 2088 |
t.hex("surface-raised").and_then(Rgb::from_hex), |
| 2089 |
t.hex("bevel-light").and_then(Rgb::from_hex), |
| 2090 |
t.hex("bevel-dark").and_then(Rgb::from_hex), |
| 2091 |
) else { |
| 2092 |
continue; |
| 2093 |
}; |
| 2094 |
|
| 2095 |
|
| 2096 |
|
| 2097 |
let f = quantize(face, ANSI_240); |
| 2098 |
let l = quantize(light, ANSI_240); |
| 2099 |
let d = quantize(dark, ANSI_240); |
| 2100 |
if l == f || d == f || l == d { |
| 2101 |
lost.push(id.to_string()); |
| 2102 |
} |
| 2103 |
} |
| 2104 |
lost.sort(); |
| 2105 |
|
| 2106 |
assert_eq!( |
| 2107 |
lost, LOSES_AN_EDGE, |
| 2108 |
"themes that cannot hold a two-tone bevel on a 256-color terminal" |
| 2109 |
); |
| 2110 |
} |
| 2111 |
|
| 2112 |
#[test] |
| 2113 |
fn the_256_table_has_its_three_regions() { |
| 2114 |
|
| 2115 |
assert_eq!(ANSI_256[..16], ANSI_16); |
| 2116 |
|
| 2117 |
assert_eq!(ANSI_256[16].tuple(), (0, 0, 0)); |
| 2118 |
assert_eq!(ANSI_256[231].tuple(), (255, 255, 255)); |
| 2119 |
assert_eq!(ANSI_256[16 + 36 * 2 + 6 * 3 + 4].tuple(), (135, 175, 215)); |
| 2120 |
|
| 2121 |
assert_eq!(ANSI_256[232].tuple(), (8, 8, 8)); |
| 2122 |
assert_eq!(ANSI_256[255].tuple(), (238, 238, 238)); |
| 2123 |
|
| 2124 |
assert_eq!(ANSI_240.len(), 240); |
| 2125 |
assert_eq!(ANSI_240[0], ANSI_256[ANSI_240_OFFSET]); |
| 2126 |
} |
| 2127 |
|
| 2128 |
#[test] |
| 2129 |
fn resolve_overlay_is_dark_translucent_scrim() { |
| 2130 |
let theme = parse_theme_str("nord", nord_toml(), false).unwrap(); |
| 2131 |
let t = resolve(&theme); |
| 2132 |
let overlay = t.hex("overlay").unwrap(); |
| 2133 |
assert!( |
| 2134 |
overlay.starts_with("rgba("), |
| 2135 |
"overlay is translucent: {overlay}" |
| 2136 |
); |
| 2137 |
assert!(overlay.ends_with(", 0.5)")); |
| 2138 |
|
| 2139 |
let inner = overlay |
| 2140 |
.trim_start_matches("rgba(") |
| 2141 |
.trim_end_matches(", 0.5)"); |
| 2142 |
let parts: Vec<u8> = inner.split(", ").map(|p| p.parse().unwrap()).collect(); |
| 2143 |
let scrim = Rgb { |
| 2144 |
r: parts[0], |
| 2145 |
g: parts[1], |
| 2146 |
b: parts[2], |
| 2147 |
}; |
| 2148 |
assert!(scrim.to_oklab().l < 0.2, "scrim must be near-black"); |
| 2149 |
} |
| 2150 |
|
| 2151 |
|
| 2152 |
|
| 2153 |
|
| 2154 |
#[test] |
| 2155 |
fn elevation_is_a_near_black_cast_on_every_theme() { |
| 2156 |
for (id, source) in embedded_themes() { |
| 2157 |
let theme = parse_theme_str(id, source, false).unwrap(); |
| 2158 |
let t = resolve(&theme); |
| 2159 |
let Some(elevation) = t.hex("elevation") else { |
| 2160 |
panic!("{id} derives no elevation"); |
| 2161 |
}; |
| 2162 |
assert!( |
| 2163 |
elevation.starts_with("rgba(") && elevation.ends_with(", 0.18)"), |
| 2164 |
"{id}: elevation is translucent: {elevation}" |
| 2165 |
); |
| 2166 |
let inner = elevation |
| 2167 |
.trim_start_matches("rgba(") |
| 2168 |
.trim_end_matches(", 0.18)"); |
| 2169 |
let parts: Vec<u8> = inner.split(", ").map(|p| p.parse().unwrap()).collect(); |
| 2170 |
let cast = Rgb { |
| 2171 |
r: parts[0], |
| 2172 |
g: parts[1], |
| 2173 |
b: parts[2], |
| 2174 |
}; |
| 2175 |
assert!( |
| 2176 |
cast.to_oklab().l < 0.2, |
| 2177 |
"{id}: a cast shadow must be near-black, got {elevation}" |
| 2178 |
); |
| 2179 |
} |
| 2180 |
} |
| 2181 |
|
| 2182 |
|
| 2183 |
|
| 2184 |
|
| 2185 |
#[test] |
| 2186 |
fn elevation_and_the_scrim_are_the_same_tone() { |
| 2187 |
let theme = parse_theme_str("nord", nord_toml(), false).unwrap(); |
| 2188 |
let t = resolve(&theme); |
| 2189 |
let scrim = t.hex("overlay").unwrap(); |
| 2190 |
let cast = t.hex("elevation").unwrap(); |
| 2191 |
assert_eq!( |
| 2192 |
scrim.trim_end_matches(", 0.5)"), |
| 2193 |
cast.trim_end_matches(", 0.18)"), |
| 2194 |
); |
| 2195 |
} |
| 2196 |
|
| 2197 |
|
| 2198 |
|
| 2199 |
|
| 2200 |
#[test] |
| 2201 |
fn rgba_reads_both_spellings() { |
| 2202 |
let theme = parse_theme_str("nord", nord_toml(), false).unwrap(); |
| 2203 |
let t = resolve(&theme); |
| 2204 |
|
| 2205 |
let (_, _, _, opaque) = t.rgba("surface-page").expect("page is a hex token"); |
| 2206 |
assert_eq!(opaque, 255); |
| 2207 |
|
| 2208 |
let (r, g, b, alpha) = t.rgba("elevation").expect("elevation is translucent"); |
| 2209 |
assert_eq!(alpha, 46, "0.18 of 255"); |
| 2210 |
assert_eq!(t.rgb("elevation"), None, "rgb declines to drop the alpha"); |
| 2211 |
|
| 2212 |
let (sr, sg, sb, scrim) = t.rgba("overlay").expect("overlay is translucent"); |
| 2213 |
assert_eq!((sr, sg, sb), (r, g, b), "one tone, two weights"); |
| 2214 |
assert_eq!(scrim, 128); |
| 2215 |
} |
| 2216 |
|
| 2217 |
#[test] |
| 2218 |
fn resolve_drops_non_hex_base_intent() { |
| 2219 |
|
| 2220 |
|
| 2221 |
|
| 2222 |
let theme = parse_theme_str( |
| 2223 |
"x", |
| 2224 |
"[surface]\npage = \"</style><script>alert(1)</script>\"\n[content]\nprimary = \"#111111\"\n", |
| 2225 |
false, |
| 2226 |
) |
| 2227 |
.unwrap(); |
| 2228 |
let t = resolve(&theme); |
| 2229 |
assert!( |
| 2230 |
t.hex("surface-page").is_none(), |
| 2231 |
"non-hex base intent leaked" |
| 2232 |
); |
| 2233 |
assert_eq!(t.hex("content").unwrap(), "#111111"); |
| 2234 |
|
| 2235 |
assert!(!t.intents.values().any(|v| v.contains('<'))); |
| 2236 |
} |
| 2237 |
|
| 2238 |
#[test] |
| 2239 |
fn resolve_skips_derived_when_source_missing() { |
| 2240 |
|
| 2241 |
let theme = parse_theme_str( |
| 2242 |
"x", |
| 2243 |
"[surface]\npage = \"#000000\"\n[line]\nborder = \"#222222\"\n", |
| 2244 |
false, |
| 2245 |
) |
| 2246 |
.unwrap(); |
| 2247 |
let t = resolve(&theme); |
| 2248 |
assert!(t.hex("action").is_none()); |
| 2249 |
assert!(t.hex("action-hover").is_none()); |
| 2250 |
assert!(t.hex("selection").is_none()); |
| 2251 |
assert_eq!( |
| 2252 |
t.hex("border-strong").unwrap(), |
| 2253 |
darken(Rgb::from_hex("#222222").unwrap(), 0.05).to_hex() |
| 2254 |
); |
| 2255 |
} |
| 2256 |
|
| 2257 |
#[test] |
| 2258 |
fn rgb_accessor_for_native_consumers() { |
| 2259 |
let theme = parse_theme_str("nord", nord_toml(), false).unwrap(); |
| 2260 |
let t = resolve(&theme); |
| 2261 |
assert_eq!(t.rgb("action"), Some((0x81, 0xa1, 0xc1))); |
| 2262 |
assert_eq!(t.rgb("nonexistent"), None); |
| 2263 |
} |
| 2264 |
|
| 2265 |
|
| 2266 |
|
| 2267 |
#[test] |
| 2268 |
fn intent_css_vars_wraps_root_and_includes_tokens() { |
| 2269 |
let theme = parse_theme_str("nord", nord_toml(), false).unwrap(); |
| 2270 |
let css = intent_css_vars(&resolve(&theme)); |
| 2271 |
assert!(css.starts_with(":root {\n")); |
| 2272 |
assert!(css.contains(" --surface-page: #2e3440;\n")); |
| 2273 |
assert!(css.contains(" --danger: #bf616a;\n")); |
| 2274 |
assert!(css.contains(" --action-hover: ")); |
| 2275 |
assert!(css.trim_end().ends_with('}')); |
| 2276 |
} |
| 2277 |
|
| 2278 |
|
| 2279 |
|
| 2280 |
#[test] |
| 2281 |
fn load_and_resolve_round_trip() { |
| 2282 |
let dir = tempfile::tempdir().unwrap(); |
| 2283 |
fs::write(dir.path().join("nord.toml"), nord_toml()).unwrap(); |
| 2284 |
let dirs = vec![(dir.path().to_path_buf(), false)]; |
| 2285 |
let t = load_semantic(&dirs, "nord").unwrap(); |
| 2286 |
assert_eq!(t.meta.name, "Nord"); |
| 2287 |
assert_eq!(t.hex("action"), Some("#81a1c1")); |
| 2288 |
} |
| 2289 |
|
| 2290 |
#[test] |
| 2291 |
fn load_theme_rejects_invalid_id() { |
| 2292 |
assert!(load_theme(&[], "../evil").is_err()); |
| 2293 |
} |
| 2294 |
|
| 2295 |
fn meta(id: &str, variant: &str) -> ThemeMeta { |
| 2296 |
ThemeMeta { |
| 2297 |
id: id.to_string(), |
| 2298 |
name: id.to_string(), |
| 2299 |
variant: variant.to_string(), |
| 2300 |
is_custom: false, |
| 2301 |
} |
| 2302 |
} |
| 2303 |
|
| 2304 |
fn defaults() -> ThemeDefaults { |
| 2305 |
ThemeDefaults::new("flatwhite", "nord") |
| 2306 |
} |
| 2307 |
|
| 2308 |
|
| 2309 |
#[test] |
| 2310 |
fn every_shipped_variant_parses() { |
| 2311 |
assert_eq!(Variant::parse("light"), Some(Variant::Light)); |
| 2312 |
assert_eq!(Variant::parse("dark"), Some(Variant::Dark)); |
| 2313 |
assert_eq!(Variant::parse("high-contrast"), Some(Variant::HighContrast)); |
| 2314 |
assert_eq!(Variant::parse("sepia"), None); |
| 2315 |
} |
| 2316 |
|
| 2317 |
|
| 2318 |
|
| 2319 |
|
| 2320 |
#[test] |
| 2321 |
fn an_unrecognized_variant_reads_the_way_a_missing_one_does() { |
| 2322 |
assert_eq!(Variant::from("sepia"), Variant::Dark); |
| 2323 |
assert_eq!(Variant::from(""), Variant::Dark); |
| 2324 |
|
| 2325 |
let missing: toml::Table = "[meta]\nname = \"X\"\n".parse().unwrap(); |
| 2326 |
assert_eq!(parse_meta("x", &missing, false).kind(), Variant::Dark); |
| 2327 |
} |
| 2328 |
|
| 2329 |
#[test] |
| 2330 |
fn a_selection_round_trips_through_any_store() { |
| 2331 |
for (stored, expect) in [ |
| 2332 |
(Some("system"), ThemeSelection::Follow), |
| 2333 |
(None, ThemeSelection::Follow), |
| 2334 |
(Some(""), ThemeSelection::Follow), |
| 2335 |
(Some(" "), ThemeSelection::Follow), |
| 2336 |
(Some("nord"), ThemeSelection::Fixed("nord".into())), |
| 2337 |
] { |
| 2338 |
let parsed = ThemeSelection::parse(stored); |
| 2339 |
assert_eq!(parsed, expect, "{stored:?}"); |
| 2340 |
assert_eq!( |
| 2341 |
ThemeSelection::parse(Some(parsed.as_str())), |
| 2342 |
expect, |
| 2343 |
"what is written reads back as what was meant", |
| 2344 |
); |
| 2345 |
} |
| 2346 |
} |
| 2347 |
|
| 2348 |
|
| 2349 |
|
| 2350 |
|
| 2351 |
#[test] |
| 2352 |
fn nothing_chosen_yet_is_follow() { |
| 2353 |
assert_eq!(ThemeSelection::default(), ThemeSelection::Follow); |
| 2354 |
} |
| 2355 |
|
| 2356 |
#[test] |
| 2357 |
fn a_fixed_selection_wins_when_its_theme_is_installed() { |
| 2358 |
let available = [meta("nord", "dark"), meta("flatwhite", "light")]; |
| 2359 |
let fixed = ThemeSelection::Fixed("nord".into()); |
| 2360 |
assert_eq!( |
| 2361 |
fixed.resolve(Variant::Light, &defaults(), &available), |
| 2362 |
"nord", |
| 2363 |
"a chosen theme is not overridden by the ambient mode", |
| 2364 |
); |
| 2365 |
} |
| 2366 |
|
| 2367 |
|
| 2368 |
|
| 2369 |
#[test] |
| 2370 |
fn a_fixed_selection_whose_theme_is_gone_falls_back() { |
| 2371 |
let available = [meta("nord", "dark"), meta("flatwhite", "light")]; |
| 2372 |
let fixed = ThemeSelection::Fixed("deleted".into()); |
| 2373 |
assert_eq!( |
| 2374 |
fixed.resolve(Variant::Light, &defaults(), &available), |
| 2375 |
"flatwhite", |
| 2376 |
); |
| 2377 |
} |
| 2378 |
|
| 2379 |
#[test] |
| 2380 |
fn follow_picks_the_apps_default_for_the_ambient_mode() { |
| 2381 |
let available = [meta("nord", "dark"), meta("flatwhite", "light")]; |
| 2382 |
let follow = ThemeSelection::Follow; |
| 2383 |
assert_eq!( |
| 2384 |
follow.resolve(Variant::Dark, &defaults(), &available), |
| 2385 |
"nord", |
| 2386 |
); |
| 2387 |
assert_eq!( |
| 2388 |
follow.resolve(Variant::Light, &defaults(), &available), |
| 2389 |
"flatwhite", |
| 2390 |
); |
| 2391 |
} |
| 2392 |
|
| 2393 |
|
| 2394 |
|
| 2395 |
#[test] |
| 2396 |
fn follow_uses_any_installed_theme_of_the_right_variant() { |
| 2397 |
let available = [meta("solarized-light", "light"), meta("mine", "dark")]; |
| 2398 |
assert_eq!( |
| 2399 |
ThemeSelection::Follow.resolve(Variant::Dark, &defaults(), &available), |
| 2400 |
"mine", |
| 2401 |
"the app's `nord` is not installed, but a dark theme is", |
| 2402 |
); |
| 2403 |
} |
| 2404 |
|
| 2405 |
|
| 2406 |
|
| 2407 |
#[test] |
| 2408 |
fn an_empty_catalog_still_names_the_apps_default() { |
| 2409 |
assert_eq!( |
| 2410 |
ThemeSelection::Follow.resolve(Variant::Dark, &defaults(), &[]), |
| 2411 |
"nord", |
| 2412 |
); |
| 2413 |
} |
| 2414 |
|
| 2415 |
#[test] |
| 2416 |
fn high_contrast_falls_back_to_dark_unless_named() { |
| 2417 |
let plain = defaults(); |
| 2418 |
assert_eq!(plain.for_variant(Variant::HighContrast), "nord"); |
| 2419 |
|
| 2420 |
let named = defaults().high_contrast("sharp"); |
| 2421 |
assert_eq!(named.for_variant(Variant::HighContrast), "sharp"); |
| 2422 |
} |
| 2423 |
|
| 2424 |
|
| 2425 |
|
| 2426 |
|
| 2427 |
|
| 2428 |
#[test] |
| 2429 |
fn the_users_own_themes_outrank_everything() { |
| 2430 |
let root = tempfile::tempdir().unwrap(); |
| 2431 |
let make = |name: &str| { |
| 2432 |
let dir = root.path().join(name); |
| 2433 |
std::fs::create_dir_all(&dir).unwrap(); |
| 2434 |
dir |
| 2435 |
}; |
| 2436 |
let (bundled, system, custom) = (make("bundled"), make("system"), make("custom")); |
| 2437 |
|
| 2438 |
let dirs = ThemeDirs::new() |
| 2439 |
.custom(Some(custom.clone())) |
| 2440 |
.bundled(Some(bundled.clone())) |
| 2441 |
.system(Some(system.clone())) |
| 2442 |
.build(); |
| 2443 |
|
| 2444 |
assert_eq!( |
| 2445 |
dirs, |
| 2446 |
vec![(bundled, false), (system, false), (custom.clone(), true)], |
| 2447 |
"lowest precedence first, whatever order the tiers were added in", |
| 2448 |
); |
| 2449 |
assert!(dirs.last().unwrap().1, "only the user's tier is custom"); |
| 2450 |
|
| 2451 |
|
| 2452 |
for dir in dirs.iter().map(|(dir, _)| dir) { |
| 2453 |
std::fs::write(dir.join("shared.toml"), "[meta]\nname = \"x\"\n").unwrap(); |
| 2454 |
} |
| 2455 |
assert_eq!( |
| 2456 |
find_theme_path(&dirs, "shared").unwrap().0, |
| 2457 |
custom.join("shared.toml"), |
| 2458 |
"the user's copy is the one that loads", |
| 2459 |
); |
| 2460 |
} |
| 2461 |
|
| 2462 |
#[test] |
| 2463 |
fn a_directory_that_does_not_exist_is_dropped() { |
| 2464 |
let root = tempfile::tempdir().unwrap(); |
| 2465 |
let real = root.path().join("real"); |
| 2466 |
std::fs::create_dir_all(&real).unwrap(); |
| 2467 |
|
| 2468 |
let dirs = ThemeDirs::new() |
| 2469 |
.bundled(Some(root.path().join("nope"))) |
| 2470 |
.system(None) |
| 2471 |
.custom(Some(real.clone())) |
| 2472 |
.build(); |
| 2473 |
|
| 2474 |
assert_eq!(dirs, vec![(real, true)]); |
| 2475 |
} |
| 2476 |
|
| 2477 |
|
| 2478 |
|
| 2479 |
#[test] |
| 2480 |
fn more_than_one_bundled_tier_is_allowed() { |
| 2481 |
let root = tempfile::tempdir().unwrap(); |
| 2482 |
let (first, second) = (root.path().join("a"), root.path().join("b")); |
| 2483 |
std::fs::create_dir_all(&first).unwrap(); |
| 2484 |
std::fs::create_dir_all(&second).unwrap(); |
| 2485 |
|
| 2486 |
let dirs = ThemeDirs::new() |
| 2487 |
.bundled(Some(first.clone())) |
| 2488 |
.bundled(Some(second.clone())) |
| 2489 |
.build(); |
| 2490 |
assert_eq!(dirs, vec![(first, false), (second, false)]); |
| 2491 |
} |
| 2492 |
|
| 2493 |
#[test] |
| 2494 |
fn list_themes_from_dirs_finds_toml_files() { |
| 2495 |
let dir = tempfile::tempdir().unwrap(); |
| 2496 |
fs::write(dir.path().join("t.toml"), "[meta]\nname = \"T\"\n").unwrap(); |
| 2497 |
fs::write(dir.path().join("x.txt"), "ignored").unwrap(); |
| 2498 |
let dirs = vec![(dir.path().to_path_buf(), false)]; |
| 2499 |
let themes = list_themes_from_dirs(&dirs); |
| 2500 |
assert_eq!(themes.len(), 1); |
| 2501 |
assert_eq!(themes[0].id, "t"); |
| 2502 |
} |
| 2503 |
|
| 2504 |
#[test] |
| 2505 |
fn find_theme_path_reverse_priority() { |
| 2506 |
let d1 = tempfile::tempdir().unwrap(); |
| 2507 |
let d2 = tempfile::tempdir().unwrap(); |
| 2508 |
fs::write(d1.path().join("s.toml"), "[meta]\n").unwrap(); |
| 2509 |
fs::write(d2.path().join("s.toml"), "[meta]\n").unwrap(); |
| 2510 |
let dirs = vec![ |
| 2511 |
(d1.path().to_path_buf(), false), |
| 2512 |
(d2.path().to_path_buf(), true), |
| 2513 |
]; |
| 2514 |
let (path, is_custom) = find_theme_path(&dirs, "s").unwrap(); |
| 2515 |
assert!(is_custom); |
| 2516 |
assert_eq!(path, d2.path().join("s.toml")); |
| 2517 |
} |
| 2518 |
|
| 2519 |
#[test] |
| 2520 |
fn import_theme_valid_and_rejects_empty() { |
| 2521 |
let src_dir = tempfile::tempdir().unwrap(); |
| 2522 |
let custom_dir = tempfile::tempdir().unwrap(); |
| 2523 |
|
| 2524 |
let good = src_dir.path().join("my-theme.toml"); |
| 2525 |
fs::write(&good, "[surface]\npage = \"#1a1b26\"\n").unwrap(); |
| 2526 |
let meta = import_theme(&good, custom_dir.path()).unwrap(); |
| 2527 |
assert_eq!(meta.id, "my-theme"); |
| 2528 |
assert!(custom_dir.path().join("my-theme.toml").exists()); |
| 2529 |
|
| 2530 |
let empty = src_dir.path().join("empty.toml"); |
| 2531 |
fs::write(&empty, "[meta]\nname = \"E\"\n").unwrap(); |
| 2532 |
assert!(import_theme(&empty, custom_dir.path()).is_err()); |
| 2533 |
} |
| 2534 |
|
| 2535 |
#[test] |
| 2536 |
fn import_theme_rejects_invalid_toml() { |
| 2537 |
let src_dir = tempfile::tempdir().unwrap(); |
| 2538 |
let custom_dir = tempfile::tempdir().unwrap(); |
| 2539 |
let src = src_dir.path().join("bad.toml"); |
| 2540 |
fs::write(&src, "this is not [valid toml [[[").unwrap(); |
| 2541 |
assert!(import_theme(&src, custom_dir.path()).is_err()); |
| 2542 |
} |
| 2543 |
|
| 2544 |
#[test] |
| 2545 |
fn delete_theme_removes_and_guards() { |
| 2546 |
let custom = tempfile::tempdir().unwrap(); |
| 2547 |
let path = custom.path().join("doomed.toml"); |
| 2548 |
fs::write(&path, "[surface]\npage = \"#000\"\n").unwrap(); |
| 2549 |
delete_theme(custom.path(), "doomed").unwrap(); |
| 2550 |
assert!(!path.exists()); |
| 2551 |
assert!(delete_theme(custom.path(), "../etc/passwd").is_err()); |
| 2552 |
assert!(delete_theme(custom.path(), "ghost").is_err()); |
| 2553 |
} |
| 2554 |
|
| 2555 |
#[test] |
| 2556 |
fn export_theme_copies_file() { |
| 2557 |
let src_dir = tempfile::tempdir().unwrap(); |
| 2558 |
let dest_dir = tempfile::tempdir().unwrap(); |
| 2559 |
let content = "[meta]\nname = \"E\"\n[surface]\npage = \"#ffffff\"\n"; |
| 2560 |
fs::write(src_dir.path().join("e.toml"), content).unwrap(); |
| 2561 |
let dirs = vec![(src_dir.path().to_path_buf(), false)]; |
| 2562 |
let dest = dest_dir.path().join("out.toml"); |
| 2563 |
export_theme(&dirs, "e", &dest).unwrap(); |
| 2564 |
assert_eq!(fs::read_to_string(&dest).unwrap(), content); |
| 2565 |
assert!(export_theme(&dirs, "missing", &dest).is_err()); |
| 2566 |
} |
| 2567 |
|
| 2568 |
#[test] |
| 2569 |
fn load_theme_preview_returns_role_swatches() { |
| 2570 |
let dir = tempfile::tempdir().unwrap(); |
| 2571 |
fs::write(dir.path().join("nord.toml"), nord_toml()).unwrap(); |
| 2572 |
let dirs = vec![(dir.path().to_path_buf(), false)]; |
| 2573 |
let p = load_theme_preview(&dirs, "nord").unwrap(); |
| 2574 |
assert_eq!(p.background.as_deref(), Some("#2e3440")); |
| 2575 |
assert_eq!(p.foreground.as_deref(), Some("#d8dee9")); |
| 2576 |
assert_eq!(p.accent.as_deref(), Some("#81a1c1")); |
| 2577 |
assert_eq!(p.border.as_deref(), Some("#4c566a")); |
| 2578 |
} |
| 2579 |
|
| 2580 |
#[test] |
| 2581 |
fn bundled_themes_dir_resolves_to_shipped_themes() { |
| 2582 |
|
| 2583 |
|
| 2584 |
let dir = bundled_themes_dir().expect("makeover ships a themes/ directory"); |
| 2585 |
assert!(dir.join("akari-dawn.toml").is_file()); |
| 2586 |
assert!(dir.join("akari-night.toml").is_file()); |
| 2587 |
} |
| 2588 |
|
| 2589 |
#[test] |
| 2590 |
fn every_theme_is_accounted_for_in_third_party_notices() { |
| 2591 |
|
| 2592 |
|
| 2593 |
|
| 2594 |
let notices = std::fs::read_to_string( |
| 2595 |
Path::new(env!("CARGO_MANIFEST_DIR")).join("THIRD-PARTY-NOTICES.md"), |
| 2596 |
) |
| 2597 |
.expect("THIRD-PARTY-NOTICES.md must exist"); |
| 2598 |
let missing: Vec<&str> = embedded_themes() |
| 2599 |
.map(|(id, _)| id) |
| 2600 |
.filter(|id| !notices.contains(*id)) |
| 2601 |
.collect(); |
| 2602 |
assert!( |
| 2603 |
missing.is_empty(), |
| 2604 |
"themes missing from THIRD-PARTY-NOTICES.md: {missing:?}" |
| 2605 |
); |
| 2606 |
} |
| 2607 |
|
| 2608 |
#[test] |
| 2609 |
fn adapted_themes_carry_inline_attribution() { |
| 2610 |
|
| 2611 |
|
| 2612 |
const ORIGINALS: [&str; 5] = [ |
| 2613 |
"makenotwork", |
| 2614 |
"goingson", |
| 2615 |
"audiofiles", |
| 2616 |
"high-contrast", |
| 2617 |
"neobrute", |
| 2618 |
]; |
| 2619 |
for (id, source) in embedded_themes() { |
| 2620 |
if ORIGINALS.contains(&id) { |
| 2621 |
continue; |
| 2622 |
} |
| 2623 |
assert!( |
| 2624 |
source.contains("adapted from"), |
| 2625 |
"adapted theme `{id}` is missing its inline attribution header" |
| 2626 |
); |
| 2627 |
} |
| 2628 |
} |
| 2629 |
|
| 2630 |
#[test] |
| 2631 |
fn embedded_themes_match_the_directory() { |
| 2632 |
|
| 2633 |
|
| 2634 |
|
| 2635 |
|
| 2636 |
let dir = bundled_themes_dir().unwrap(); |
| 2637 |
let mut on_disk: Vec<String> = std::fs::read_dir(&dir) |
| 2638 |
.unwrap() |
| 2639 |
.filter_map(|e| { |
| 2640 |
let path = e.ok()?.path(); |
| 2641 |
if path.extension()? != "toml" { |
| 2642 |
return None; |
| 2643 |
} |
| 2644 |
Some(path.file_stem()?.to_str()?.to_string()) |
| 2645 |
}) |
| 2646 |
.collect(); |
| 2647 |
let mut embedded: Vec<String> = embedded_themes().map(|(id, _)| id.to_string()).collect(); |
| 2648 |
on_disk.sort(); |
| 2649 |
embedded.sort(); |
| 2650 |
assert_eq!(embedded, on_disk, "embedded theme set drifted from themes/"); |
| 2651 |
} |
| 2652 |
|
| 2653 |
#[test] |
| 2654 |
fn every_embedded_theme_parses() { |
| 2655 |
|
| 2656 |
|
| 2657 |
let mut count = 0; |
| 2658 |
for (id, source) in embedded_themes() { |
| 2659 |
parse_theme_str(id, source, false) |
| 2660 |
.unwrap_or_else(|e| panic!("embedded theme `{id}` failed to parse: {e}")); |
| 2661 |
count += 1; |
| 2662 |
} |
| 2663 |
assert!(count >= 30, "expected the full theme set, got {count}"); |
| 2664 |
} |
| 2665 |
|
| 2666 |
#[test] |
| 2667 |
fn every_shipped_theme_loads() { |
| 2668 |
|
| 2669 |
|
| 2670 |
|
| 2671 |
let dir = bundled_themes_dir().unwrap(); |
| 2672 |
let dirs = vec![(dir.clone(), false)]; |
| 2673 |
let themes = list_themes_from_dirs(&dirs); |
| 2674 |
assert!( |
| 2675 |
themes.len() >= 30, |
| 2676 |
"expected the full theme set, got {}", |
| 2677 |
themes.len() |
| 2678 |
); |
| 2679 |
for meta in &themes { |
| 2680 |
load_theme(&dirs, &meta.id) |
| 2681 |
.unwrap_or_else(|e| panic!("shipped theme `{}` failed to load: {e}", meta.id)); |
| 2682 |
} |
| 2683 |
} |
| 2684 |
} |
| 2685 |
|