| 1 |
|
| 2 |
|
| 3 |
use crate::{Rgb, ThemeColors, ThemeMeta, darken, lighten, readable_on}; |
| 4 |
use serde::Serialize; |
| 5 |
use std::collections::BTreeMap; |
| 6 |
|
| 7 |
|
| 8 |
#[allow(unused_imports)] |
| 9 |
use crate::derive_tonal_steps; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
pub const BASE_INTENTS: &[(&str, &str)] = &[ |
| 19 |
("surface.page", "surface-page"), |
| 20 |
("surface.raised", "surface-raised"), |
| 21 |
("surface.sunken", "surface-sunken"), |
| 22 |
("surface.overlay", "surface-overlay"), |
| 23 |
("content.primary", "content"), |
| 24 |
("content.secondary", "content-secondary"), |
| 25 |
("content.muted", "content-muted"), |
| 26 |
("action.primary", "action"), |
| 27 |
("status.danger", "danger"), |
| 28 |
("status.success", "success"), |
| 29 |
("status.warning", "warning"), |
| 30 |
("status.info", "info"), |
| 31 |
("line.border", "border"), |
| 32 |
("category.one", "category-one"), |
| 33 |
("category.two", "category-two"), |
| 34 |
("category.three", "category-three"), |
| 35 |
("category.four", "category-four"), |
| 36 |
("category.five", "category-five"), |
| 37 |
("category.six", "category-six"), |
| 38 |
]; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#[derive(Debug, Clone, Serialize)] |
| 43 |
#[serde(rename_all = "camelCase")] |
| 44 |
pub struct SemanticTokens { |
| 45 |
pub meta: ThemeMeta, |
| 46 |
|
| 47 |
pub intents: BTreeMap<String, String>, |
| 48 |
} |
| 49 |
|
| 50 |
impl SemanticTokens { |
| 51 |
|
| 52 |
pub fn hex(&self, key: &str) -> Option<&str> { |
| 53 |
self.intents.get(key).map(String::as_str) |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
pub fn rgb(&self, key: &str) -> Option<(u8, u8, u8)> { |
| 63 |
self.intents |
| 64 |
.get(key) |
| 65 |
.and_then(|h| Rgb::from_hex(h)) |
| 66 |
.map(Rgb::tuple) |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
pub fn rgba(&self, key: &str) -> Option<(u8, u8, u8, u8)> { |
| 81 |
let value = self.intents.get(key)?; |
| 82 |
if let Some(rgb) = Rgb::from_hex(value) { |
| 83 |
let (r, g, b) = rgb.tuple(); |
| 84 |
return Some((r, g, b, 255)); |
| 85 |
} |
| 86 |
let inner = value.strip_prefix("rgba(")?.strip_suffix(')')?; |
| 87 |
let mut parts = inner.split(',').map(str::trim); |
| 88 |
let r = parts.next()?.parse().ok()?; |
| 89 |
let g = parts.next()?.parse().ok()?; |
| 90 |
let b = parts.next()?.parse().ok()?; |
| 91 |
let alpha: f32 = parts.next()?.parse().ok()?; |
| 92 |
if parts.next().is_some() || !(0.0..=1.0).contains(&alpha) { |
| 93 |
return None; |
| 94 |
} |
| 95 |
Some((r, g, b, (alpha * 255.0).round() as u8)) |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
pub fn resolve(theme: &ThemeColors) -> SemanticTokens { |
| 108 |
let mut intents: BTreeMap<String, String> = BTreeMap::new(); |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
for (src, token) in BASE_INTENTS { |
| 116 |
if let Some(rgb) = theme.colors.get(*src).and_then(|v| Rgb::from_hex(v)) { |
| 117 |
intents.insert((*token).to_string(), rgb.to_hex()); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
let get = |m: &BTreeMap<String, String>, k: &str| m.get(k).and_then(|h| Rgb::from_hex(h)); |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
let mut derived: Vec<(String, Rgb)> = Vec::new(); |
| 127 |
if let Some(action) = get(&intents, "action") { |
| 128 |
derived.push(("action-hover".into(), lighten(action, 0.05))); |
| 129 |
derived.push(("content-on-action".into(), readable_on(action))); |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
derived.push(("focus-ring".into(), action)); |
| 143 |
} |
| 144 |
if let Some(page) = get(&intents, "surface-page") { |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
let mut o = page.to_oklab(); |
| 150 |
o.l = 0.08; |
| 151 |
let s = Rgb::from_oklab(o); |
| 152 |
intents.insert( |
| 153 |
"overlay".into(), |
| 154 |
format!("rgba({}, {}, {}, 0.5)", s.r, s.g, s.b), |
| 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 |
intents.insert( |
| 182 |
"elevation".into(), |
| 183 |
format!("rgba({}, {}, {}, 0.18)", s.r, s.g, s.b), |
| 184 |
); |
| 185 |
} |
| 186 |
if let Some(raised) = get(&intents, "surface-raised") { |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
derived.push(("bevel-light".into(), lighten(raised, 0.14))); |
| 206 |
derived.push(("bevel-dark".into(), darken(raised, 0.18))); |
| 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 |
if let Some(content) = get(&intents, "content") { |
| 232 |
let content_is_darker = content.to_oklab().l < raised.to_oklab().l; |
| 233 |
let well = if content_is_darker { |
| 234 |
lighten(raised, 0.07) |
| 235 |
} else { |
| 236 |
darken(raised, 0.09) |
| 237 |
}; |
| 238 |
derived.push(("surface-well".into(), well)); |
| 239 |
} |
| 240 |
} |
| 241 |
if let Some(sunken) = get(&intents, "surface-sunken") { |
| 242 |
derived.push(("hover-surface".into(), sunken)); |
| 243 |
} |
| 244 |
if let Some(border) = get(&intents, "border") { |
| 245 |
derived.push(("border-strong".into(), darken(border, 0.05))); |
| 246 |
} |
| 247 |
|
| 248 |
for (token, rgb) in derived { |
| 249 |
intents.insert(token, rgb.to_hex()); |
| 250 |
} |
| 251 |
|
| 252 |
SemanticTokens { |
| 253 |
meta: theme.meta.clone(), |
| 254 |
intents, |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
pub fn intent_css_declarations(tokens: &SemanticTokens) -> String { |
| 261 |
let mut out = String::new(); |
| 262 |
for (token, hex) in &tokens.intents { |
| 263 |
out.push_str(" --"); |
| 264 |
out.push_str(token); |
| 265 |
out.push_str(": "); |
| 266 |
out.push_str(hex); |
| 267 |
out.push_str(";\n"); |
| 268 |
} |
| 269 |
out |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
pub fn intent_css_vars(tokens: &SemanticTokens) -> String { |
| 275 |
format!(":root {{\n{}}}\n", intent_css_declarations(tokens)) |
| 276 |
} |
| 277 |
|
| 278 |
#[cfg(test)] |
| 279 |
mod tests { |
| 280 |
use super::*; |
| 281 |
use crate::fixture::nord_toml; |
| 282 |
use crate::{Emphasis, embedded_themes, emphasized, parse_theme_str}; |
| 283 |
|
| 284 |
#[test] |
| 285 |
fn resolve_base_intents_passthrough() { |
| 286 |
let theme = parse_theme_str("nord", nord_toml(), false).unwrap(); |
| 287 |
let t = resolve(&theme); |
| 288 |
assert_eq!(t.hex("surface-page"), Some("#2e3440")); |
| 289 |
assert_eq!(t.hex("content"), Some("#d8dee9")); |
| 290 |
|
| 291 |
assert_eq!( |
| 292 |
t.hex("content-muted").unwrap(), |
| 293 |
emphasized( |
| 294 |
Rgb::from_hex("#d8dee9").unwrap(), |
| 295 |
Rgb::from_hex("#2e3440").unwrap(), |
| 296 |
Emphasis::Muted |
| 297 |
) |
| 298 |
.to_hex() |
| 299 |
); |
| 300 |
assert_eq!(t.hex("action"), Some("#81a1c1")); |
| 301 |
assert_eq!(t.hex("danger"), Some("#bf616a")); |
| 302 |
assert_eq!(t.hex("border"), Some("#4c566a")); |
| 303 |
assert_eq!(t.hex("category-five"), Some("#b48ead")); |
| 304 |
} |
| 305 |
|
| 306 |
#[test] |
| 307 |
fn resolve_derived_intents() { |
| 308 |
let theme = parse_theme_str("nord", nord_toml(), false).unwrap(); |
| 309 |
let t = resolve(&theme); |
| 310 |
let action = Rgb::from_hex("#81a1c1").unwrap(); |
| 311 |
let page = Rgb::from_hex("#2e3440").unwrap(); |
| 312 |
let _ = page; |
| 313 |
assert_eq!( |
| 314 |
t.hex("action-hover").unwrap(), |
| 315 |
lighten(action, 0.05).to_hex() |
| 316 |
); |
| 317 |
assert_eq!( |
| 318 |
t.hex("content-on-action").unwrap(), |
| 319 |
readable_on(action).to_hex() |
| 320 |
); |
| 321 |
assert_eq!(t.hex("focus-ring"), Some("#81a1c1")); |
| 322 |
assert_eq!(t.hex("hover-surface"), Some("#434c5e")); |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
assert!(t.hex("action-active").is_none()); |
| 327 |
assert!(t.hex("danger-surface").is_none()); |
| 328 |
assert!(t.hex("selection").is_none()); |
| 329 |
assert!(t.hex("row-stripe").is_none()); |
| 330 |
} |
| 331 |
|
| 332 |
#[test] |
| 333 |
fn resolve_bevel_intents() { |
| 334 |
let theme = parse_theme_str("nord", nord_toml(), false).unwrap(); |
| 335 |
let t = resolve(&theme); |
| 336 |
let raised = Rgb::from_hex("#3b4252").unwrap(); |
| 337 |
assert_eq!( |
| 338 |
t.hex("bevel-light").unwrap(), |
| 339 |
lighten(raised, 0.14).to_hex() |
| 340 |
); |
| 341 |
assert_eq!(t.hex("bevel-dark").unwrap(), darken(raised, 0.18).to_hex()); |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
#[test] |
| 353 |
fn bevel_edges_are_distinct_from_their_face() { |
| 354 |
const CANNOT_BEVEL: &[&str] = &["neobrute", "oxocarbon-light"]; |
| 355 |
|
| 356 |
let mut degenerate: Vec<String> = Vec::new(); |
| 357 |
for (id, source) in embedded_themes() { |
| 358 |
let theme = parse_theme_str(id, source, false).unwrap(); |
| 359 |
let t = resolve(&theme); |
| 360 |
let Some(raised) = t.hex("surface-raised") else { |
| 361 |
continue; |
| 362 |
}; |
| 363 |
let light = t.hex("bevel-light").expect("raised implies bevel-light"); |
| 364 |
let dark = t.hex("bevel-dark").expect("raised implies bevel-dark"); |
| 365 |
if light == raised || dark == raised { |
| 366 |
degenerate.push(id.to_string()); |
| 367 |
} |
| 368 |
} |
| 369 |
degenerate.sort(); |
| 370 |
|
| 371 |
assert_eq!( |
| 372 |
degenerate, CANNOT_BEVEL, |
| 373 |
"themes whose raised surface cannot hold both bevel edges" |
| 374 |
); |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
#[test] |
| 380 |
fn resolve_well_intent_follows_the_content_direction() { |
| 381 |
|
| 382 |
|
| 383 |
let dark = resolve(&parse_theme_str("nord", nord_toml(), false).unwrap()); |
| 384 |
let dark_raised = Rgb::from_hex("#3b4252").unwrap(); |
| 385 |
assert_eq!( |
| 386 |
dark.hex("surface-well").unwrap(), |
| 387 |
darken(dark_raised, 0.09).to_hex() |
| 388 |
); |
| 389 |
|
| 390 |
|
| 391 |
let goingson = embedded_themes() |
| 392 |
.into_iter() |
| 393 |
.find(|(id, _)| *id == "goingson") |
| 394 |
.expect("goingson is embedded") |
| 395 |
.1; |
| 396 |
let light = resolve(&parse_theme_str("goingson", goingson, false).unwrap()); |
| 397 |
let light_raised = light |
| 398 |
.hex("surface-raised") |
| 399 |
.and_then(Rgb::from_hex) |
| 400 |
.expect("goingson authors a raised surface"); |
| 401 |
assert_eq!( |
| 402 |
light.hex("surface-well").unwrap(), |
| 403 |
lighten(light_raised, 0.07).to_hex() |
| 404 |
); |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
#[test] |
| 415 |
fn well_is_distinct_from_its_face() { |
| 416 |
const CANNOT_WELL: &[&str] = &["neobrute", "oxocarbon-light"]; |
| 417 |
|
| 418 |
let mut degenerate: Vec<String> = Vec::new(); |
| 419 |
for (id, source) in embedded_themes() { |
| 420 |
let theme = parse_theme_str(id, source, false).unwrap(); |
| 421 |
let t = resolve(&theme); |
| 422 |
let Some(raised) = t.hex("surface-raised") else { |
| 423 |
continue; |
| 424 |
}; |
| 425 |
let well = t.hex("surface-well").expect("raised implies surface-well"); |
| 426 |
if well == raised { |
| 427 |
degenerate.push(id.to_string()); |
| 428 |
} |
| 429 |
} |
| 430 |
degenerate.sort(); |
| 431 |
|
| 432 |
assert_eq!( |
| 433 |
degenerate, CANNOT_WELL, |
| 434 |
"themes whose raised surface cannot hold a well" |
| 435 |
); |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
#[test] |
| 448 |
fn well_is_visible_against_its_face() { |
| 449 |
|
| 450 |
const MIN_DELTA_L: f32 = 0.02; |
| 451 |
const CANNOT_HOLD_A_VISIBLE_WELL: &[&str] = |
| 452 |
&["neobrute", "oxocarbon-light", "rosepine-dawn"]; |
| 453 |
|
| 454 |
let mut invisible: Vec<String> = Vec::new(); |
| 455 |
for (id, source) in embedded_themes() { |
| 456 |
let theme = parse_theme_str(id, source, false).unwrap(); |
| 457 |
let t = resolve(&theme); |
| 458 |
let (Some(raised), Some(well)) = ( |
| 459 |
t.hex("surface-raised").and_then(Rgb::from_hex), |
| 460 |
t.hex("surface-well").and_then(Rgb::from_hex), |
| 461 |
) else { |
| 462 |
continue; |
| 463 |
}; |
| 464 |
if (well.to_oklab().l - raised.to_oklab().l).abs() < MIN_DELTA_L { |
| 465 |
invisible.push(id.to_string()); |
| 466 |
} |
| 467 |
} |
| 468 |
invisible.sort(); |
| 469 |
|
| 470 |
assert_eq!( |
| 471 |
invisible, CANNOT_HOLD_A_VISIBLE_WELL, |
| 472 |
"themes whose well is too close to its face to read as one" |
| 473 |
); |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
#[test] |
| 501 |
fn raised_is_distinct_from_page() { |
| 502 |
|
| 503 |
|
| 504 |
const MIN_DELTA_L: f32 = 0.05; |
| 505 |
const CANNOT_LIFT_OFF_THE_PAGE: &[&str] = &[ |
| 506 |
"akari-dawn", |
| 507 |
"akari-night", |
| 508 |
"ayu-light", |
| 509 |
"ayu-mirage", |
| 510 |
"catppuccin-latte", |
| 511 |
"catppuccin-mocha", |
| 512 |
"dawnfox", |
| 513 |
"dracula", |
| 514 |
"everforest", |
| 515 |
"flatwhite", |
| 516 |
"gruvbox-light", |
| 517 |
"neobrute", |
| 518 |
"one-dark", |
| 519 |
"oxocarbon-dark", |
| 520 |
"oxocarbon-light", |
| 521 |
"poimandres", |
| 522 |
"rosepine", |
| 523 |
"rosepine-dawn", |
| 524 |
"solarized-dark", |
| 525 |
]; |
| 526 |
|
| 527 |
let mut flat: Vec<String> = Vec::new(); |
| 528 |
for (id, source) in embedded_themes() { |
| 529 |
let theme = parse_theme_str(id, source, false).unwrap(); |
| 530 |
let t = resolve(&theme); |
| 531 |
let (Some(page), Some(raised)) = ( |
| 532 |
t.hex("surface-page").and_then(Rgb::from_hex), |
| 533 |
t.hex("surface-raised").and_then(Rgb::from_hex), |
| 534 |
) else { |
| 535 |
continue; |
| 536 |
}; |
| 537 |
if (raised.to_oklab().l - page.to_oklab().l).abs() < MIN_DELTA_L { |
| 538 |
flat.push(id.to_string()); |
| 539 |
} |
| 540 |
} |
| 541 |
flat.sort(); |
| 542 |
|
| 543 |
assert_eq!( |
| 544 |
flat, CANNOT_LIFT_OFF_THE_PAGE, |
| 545 |
"themes whose raised surface is too close to the page to lift off it" |
| 546 |
); |
| 547 |
} |
| 548 |
|
| 549 |
#[test] |
| 550 |
fn resolve_overlay_is_dark_translucent_scrim() { |
| 551 |
let theme = parse_theme_str("nord", nord_toml(), false).unwrap(); |
| 552 |
let t = resolve(&theme); |
| 553 |
let overlay = t.hex("overlay").unwrap(); |
| 554 |
assert!( |
| 555 |
overlay.starts_with("rgba("), |
| 556 |
"overlay is translucent: {overlay}" |
| 557 |
); |
| 558 |
assert!(overlay.ends_with(", 0.5)")); |
| 559 |
|
| 560 |
let inner = overlay |
| 561 |
.trim_start_matches("rgba(") |
| 562 |
.trim_end_matches(", 0.5)"); |
| 563 |
let parts: Vec<u8> = inner.split(", ").map(|p| p.parse().unwrap()).collect(); |
| 564 |
let scrim = Rgb { |
| 565 |
r: parts[0], |
| 566 |
g: parts[1], |
| 567 |
b: parts[2], |
| 568 |
}; |
| 569 |
assert!(scrim.to_oklab().l < 0.2, "scrim must be near-black"); |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
#[test] |
| 576 |
fn elevation_is_a_near_black_cast_on_every_theme() { |
| 577 |
for (id, source) in embedded_themes() { |
| 578 |
let theme = parse_theme_str(id, source, false).unwrap(); |
| 579 |
let t = resolve(&theme); |
| 580 |
let Some(elevation) = t.hex("elevation") else { |
| 581 |
panic!("{id} derives no elevation"); |
| 582 |
}; |
| 583 |
assert!( |
| 584 |
elevation.starts_with("rgba(") && elevation.ends_with(", 0.18)"), |
| 585 |
"{id}: elevation is translucent: {elevation}" |
| 586 |
); |
| 587 |
let inner = elevation |
| 588 |
.trim_start_matches("rgba(") |
| 589 |
.trim_end_matches(", 0.18)"); |
| 590 |
let parts: Vec<u8> = inner.split(", ").map(|p| p.parse().unwrap()).collect(); |
| 591 |
let cast = Rgb { |
| 592 |
r: parts[0], |
| 593 |
g: parts[1], |
| 594 |
b: parts[2], |
| 595 |
}; |
| 596 |
assert!( |
| 597 |
cast.to_oklab().l < 0.2, |
| 598 |
"{id}: a cast shadow must be near-black, got {elevation}" |
| 599 |
); |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
#[test] |
| 607 |
fn elevation_and_the_scrim_are_the_same_tone() { |
| 608 |
let theme = parse_theme_str("nord", nord_toml(), false).unwrap(); |
| 609 |
let t = resolve(&theme); |
| 610 |
let scrim = t.hex("overlay").unwrap(); |
| 611 |
let cast = t.hex("elevation").unwrap(); |
| 612 |
assert_eq!( |
| 613 |
scrim.trim_end_matches(", 0.5)"), |
| 614 |
cast.trim_end_matches(", 0.18)"), |
| 615 |
); |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
#[test] |
| 622 |
fn rgba_reads_both_spellings() { |
| 623 |
let theme = parse_theme_str("nord", nord_toml(), false).unwrap(); |
| 624 |
let t = resolve(&theme); |
| 625 |
|
| 626 |
let (_, _, _, opaque) = t.rgba("surface-page").expect("page is a hex token"); |
| 627 |
assert_eq!(opaque, 255); |
| 628 |
|
| 629 |
let (r, g, b, alpha) = t.rgba("elevation").expect("elevation is translucent"); |
| 630 |
assert_eq!(alpha, 46, "0.18 of 255"); |
| 631 |
assert_eq!(t.rgb("elevation"), None, "rgb declines to drop the alpha"); |
| 632 |
|
| 633 |
let (sr, sg, sb, scrim) = t.rgba("overlay").expect("overlay is translucent"); |
| 634 |
assert_eq!((sr, sg, sb), (r, g, b), "one tone, two weights"); |
| 635 |
assert_eq!(scrim, 128); |
| 636 |
} |
| 637 |
|
| 638 |
#[test] |
| 639 |
fn resolve_drops_non_hex_base_intent() { |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
let theme = parse_theme_str( |
| 644 |
"x", |
| 645 |
"[surface]\npage = \"</style><script>alert(1)</script>\"\n[content]\nprimary = \"#111111\"\n", |
| 646 |
false, |
| 647 |
) |
| 648 |
.unwrap(); |
| 649 |
let t = resolve(&theme); |
| 650 |
assert!( |
| 651 |
t.hex("surface-page").is_none(), |
| 652 |
"non-hex base intent leaked" |
| 653 |
); |
| 654 |
assert_eq!(t.hex("content").unwrap(), "#111111"); |
| 655 |
|
| 656 |
assert!(!t.intents.values().any(|v| v.contains('<'))); |
| 657 |
} |
| 658 |
|
| 659 |
#[test] |
| 660 |
fn resolve_skips_derived_when_source_missing() { |
| 661 |
|
| 662 |
let theme = parse_theme_str( |
| 663 |
"x", |
| 664 |
"[surface]\npage = \"#000000\"\n[line]\nborder = \"#222222\"\n", |
| 665 |
false, |
| 666 |
) |
| 667 |
.unwrap(); |
| 668 |
let t = resolve(&theme); |
| 669 |
assert!(t.hex("action").is_none()); |
| 670 |
assert!(t.hex("action-hover").is_none()); |
| 671 |
assert!(t.hex("selection").is_none()); |
| 672 |
assert_eq!( |
| 673 |
t.hex("border-strong").unwrap(), |
| 674 |
darken(Rgb::from_hex("#222222").unwrap(), 0.05).to_hex() |
| 675 |
); |
| 676 |
} |
| 677 |
|
| 678 |
#[test] |
| 679 |
fn rgb_accessor_for_native_consumers() { |
| 680 |
let theme = parse_theme_str("nord", nord_toml(), false).unwrap(); |
| 681 |
let t = resolve(&theme); |
| 682 |
assert_eq!(t.rgb("action"), Some((0x81, 0xa1, 0xc1))); |
| 683 |
assert_eq!(t.rgb("nonexistent"), None); |
| 684 |
} |
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
#[test] |
| 689 |
fn intent_css_vars_wraps_root_and_includes_tokens() { |
| 690 |
let theme = parse_theme_str("nord", nord_toml(), false).unwrap(); |
| 691 |
let css = intent_css_vars(&resolve(&theme)); |
| 692 |
assert!(css.starts_with(":root {\n")); |
| 693 |
assert!(css.contains(" --surface-page: #2e3440;\n")); |
| 694 |
assert!(css.contains(" --danger: #bf616a;\n")); |
| 695 |
assert!(css.contains(" --action-hover: ")); |
| 696 |
assert!(css.trim_end().ends_with('}')); |
| 697 |
} |
| 698 |
} |
| 699 |
|