| 1 |
|
| 2 |
|
| 3 |
use crate::{Rgb, wcag_contrast}; |
| 4 |
|
| 5 |
|
| 6 |
#[allow(unused_imports)] |
| 7 |
use crate::{ThemeColors, mix}; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
pub const ANSI_16: [Rgb; 16] = [ |
| 21 |
Rgb { |
| 22 |
r: 0x00, |
| 23 |
g: 0x00, |
| 24 |
b: 0x00, |
| 25 |
}, |
| 26 |
Rgb { |
| 27 |
r: 0xaa, |
| 28 |
g: 0x00, |
| 29 |
b: 0x00, |
| 30 |
}, |
| 31 |
Rgb { |
| 32 |
r: 0x00, |
| 33 |
g: 0xaa, |
| 34 |
b: 0x00, |
| 35 |
}, |
| 36 |
Rgb { |
| 37 |
r: 0xaa, |
| 38 |
g: 0x55, |
| 39 |
b: 0x00, |
| 40 |
}, |
| 41 |
Rgb { |
| 42 |
r: 0x00, |
| 43 |
g: 0x00, |
| 44 |
b: 0xaa, |
| 45 |
}, |
| 46 |
Rgb { |
| 47 |
r: 0xaa, |
| 48 |
g: 0x00, |
| 49 |
b: 0xaa, |
| 50 |
}, |
| 51 |
Rgb { |
| 52 |
r: 0x00, |
| 53 |
g: 0xaa, |
| 54 |
b: 0xaa, |
| 55 |
}, |
| 56 |
Rgb { |
| 57 |
r: 0xaa, |
| 58 |
g: 0xaa, |
| 59 |
b: 0xaa, |
| 60 |
}, |
| 61 |
Rgb { |
| 62 |
r: 0x55, |
| 63 |
g: 0x55, |
| 64 |
b: 0x55, |
| 65 |
}, |
| 66 |
Rgb { |
| 67 |
r: 0xff, |
| 68 |
g: 0x55, |
| 69 |
b: 0x55, |
| 70 |
}, |
| 71 |
Rgb { |
| 72 |
r: 0x55, |
| 73 |
g: 0xff, |
| 74 |
b: 0x55, |
| 75 |
}, |
| 76 |
Rgb { |
| 77 |
r: 0xff, |
| 78 |
g: 0xff, |
| 79 |
b: 0x55, |
| 80 |
}, |
| 81 |
Rgb { |
| 82 |
r: 0x55, |
| 83 |
g: 0x55, |
| 84 |
b: 0xff, |
| 85 |
}, |
| 86 |
Rgb { |
| 87 |
r: 0xff, |
| 88 |
g: 0x55, |
| 89 |
b: 0xff, |
| 90 |
}, |
| 91 |
Rgb { |
| 92 |
r: 0x55, |
| 93 |
g: 0xff, |
| 94 |
b: 0xff, |
| 95 |
}, |
| 96 |
Rgb { |
| 97 |
r: 0xff, |
| 98 |
g: 0xff, |
| 99 |
b: 0xff, |
| 100 |
}, |
| 101 |
]; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
pub const ANSI_256: [Rgb; 256] = build_ansi_256(); |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
pub const ANSI_240: &[Rgb] = ANSI_256.split_at(16).1; |
| 121 |
|
| 122 |
|
| 123 |
pub const ANSI_240_OFFSET: usize = 16; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
const CHROMATIC: [(usize, &str); 12] = [ |
| 135 |
(1, "status.danger"), |
| 136 |
(2, "status.success"), |
| 137 |
(3, "status.warning"), |
| 138 |
(4, "status.info"), |
| 139 |
(5, "category.five"), |
| 140 |
(6, "category.six"), |
| 141 |
(9, "action.primary"), |
| 142 |
(10, "status.success"), |
| 143 |
(11, "status.warning"), |
| 144 |
(12, "status.info"), |
| 145 |
(13, "category.five"), |
| 146 |
(14, "category.six"), |
| 147 |
]; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
fn achromatic_slot(index: usize, variant: &str) -> Option<&'static str> { |
| 165 |
let dark = variant == "dark"; |
| 166 |
Some(match (index, dark) { |
| 167 |
(0, false) => "content.primary", |
| 168 |
(0, true) => "surface.sunken", |
| 169 |
(7, false) => "surface.raised", |
| 170 |
(7, true) => "content.secondary", |
| 171 |
(8, _) => "content.muted", |
| 172 |
(15, false) => "surface.overlay", |
| 173 |
(15, true) => "content.primary", |
| 174 |
_ => return None, |
| 175 |
}) |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
#[must_use] |
| 188 |
pub fn ansi_intent(index: usize, variant: &str) -> Option<&'static str> { |
| 189 |
achromatic_slot(index, variant).or_else(|| { |
| 190 |
CHROMATIC |
| 191 |
.iter() |
| 192 |
.find(|(slot, _)| *slot == index) |
| 193 |
.map(|(_, intent)| *intent) |
| 194 |
}) |
| 195 |
} |
| 196 |
|
| 197 |
const fn build_ansi_256() -> [Rgb; 256] { |
| 198 |
let mut table = [Rgb { r: 0, g: 0, b: 0 }; 256]; |
| 199 |
|
| 200 |
let mut i = 0; |
| 201 |
while i < 16 { |
| 202 |
table[i] = ANSI_16[i]; |
| 203 |
i += 1; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
const LEVELS: [u8; 6] = [0, 95, 135, 175, 215, 255]; |
| 211 |
let mut r = 0; |
| 212 |
while r < 6 { |
| 213 |
let mut g = 0; |
| 214 |
while g < 6 { |
| 215 |
let mut b = 0; |
| 216 |
while b < 6 { |
| 217 |
table[16 + 36 * r + 6 * g + b] = Rgb { |
| 218 |
r: LEVELS[r], |
| 219 |
g: LEVELS[g], |
| 220 |
b: LEVELS[b], |
| 221 |
}; |
| 222 |
b += 1; |
| 223 |
} |
| 224 |
g += 1; |
| 225 |
} |
| 226 |
r += 1; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
let mut k = 0; |
| 233 |
while k < 24 { |
| 234 |
let v = 8 + 10 * k as u8; |
| 235 |
table[232 + k as usize] = Rgb { r: v, g: v, b: v }; |
| 236 |
k += 1; |
| 237 |
} |
| 238 |
|
| 239 |
table |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
pub const DISTINCT: f32 = 3.0; |
| 248 |
|
| 249 |
|
| 250 |
fn oklab_distance(a: Rgb, b: Rgb) -> f32 { |
| 251 |
let (x, y) = (a.to_oklab(), b.to_oklab()); |
| 252 |
((x.l - y.l).powi(2) + (x.a - y.a).powi(2) + (x.b - y.b).powi(2)).sqrt() |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
pub fn quantize(c: Rgb, palette: &[Rgb]) -> usize { |
| 265 |
assert!(!palette.is_empty(), "a palette needs at least one color"); |
| 266 |
let mut best = 0; |
| 267 |
let mut best_distance = f32::INFINITY; |
| 268 |
for (index, entry) in palette.iter().enumerate() { |
| 269 |
let distance = oklab_distance(c, *entry); |
| 270 |
if distance < best_distance { |
| 271 |
best = index; |
| 272 |
best_distance = distance; |
| 273 |
} |
| 274 |
} |
| 275 |
best |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
pub fn quantize_against(fg: Rgb, bg: Rgb, palette: &[Rgb]) -> usize { |
| 303 |
assert!(!palette.is_empty(), "a palette needs at least one color"); |
| 304 |
let shown = palette[quantize(bg, palette)]; |
| 305 |
|
| 306 |
let mut order: Vec<usize> = (0..palette.len()).collect(); |
| 307 |
order.sort_by(|a, b| { |
| 308 |
oklab_distance(fg, palette[*a]).total_cmp(&oklab_distance(fg, palette[*b])) |
| 309 |
}); |
| 310 |
|
| 311 |
order |
| 312 |
.iter() |
| 313 |
.copied() |
| 314 |
.find(|index| wcag_contrast(palette[*index], shown) >= DISTINCT) |
| 315 |
.unwrap_or_else(|| { |
| 316 |
order |
| 317 |
.iter() |
| 318 |
.copied() |
| 319 |
.max_by(|a, b| { |
| 320 |
wcag_contrast(palette[*a], shown).total_cmp(&wcag_contrast(palette[*b], shown)) |
| 321 |
}) |
| 322 |
.expect("the palette is not empty") |
| 323 |
}) |
| 324 |
} |
| 325 |
|
| 326 |
#[cfg(test)] |
| 327 |
mod tests { |
| 328 |
use super::*; |
| 329 |
use crate::color::rel_luminance; |
| 330 |
use crate::fixture::bundled; |
| 331 |
use crate::{embedded_themes, parse_theme_str, resolve}; |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
#[test] |
| 336 |
fn the_ansi_palette_is_sixteen_distinct_colors() { |
| 337 |
let mut seen: Vec<(u8, u8, u8)> = ANSI_16.iter().map(|c| c.tuple()).collect(); |
| 338 |
seen.sort_unstable(); |
| 339 |
seen.dedup(); |
| 340 |
assert_eq!(seen.len(), 16); |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
#[test] |
| 350 |
fn every_ansi_slot_names_an_intent_on_either_polarity() { |
| 351 |
for variant in ["light", "dark", "high-contrast"] { |
| 352 |
for index in 0..16 { |
| 353 |
assert!( |
| 354 |
ansi_intent(index, variant).is_some(), |
| 355 |
"slot {index} unanswered on {variant}" |
| 356 |
); |
| 357 |
} |
| 358 |
assert_eq!(ansi_intent(16, variant), None); |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
#[test] |
| 367 |
fn ansi_zero_is_darker_than_ansi_fifteen_on_either_polarity() { |
| 368 |
for id in ["akari-dawn", "akari-night"] { |
| 369 |
let theme = bundled(id); |
| 370 |
let slot = |i: usize| -> Rgb { |
| 371 |
let key = ansi_intent(i, &theme.meta.variant).expect("in range"); |
| 372 |
Rgb::from_hex(theme.colors.get(key).expect("theme carries it")).expect("valid hex") |
| 373 |
}; |
| 374 |
assert!( |
| 375 |
rel_luminance(slot(0)) < rel_luminance(slot(15)), |
| 376 |
"{id}: ANSI 0 {} should be darker than ANSI 15 {}", |
| 377 |
slot(0).to_hex(), |
| 378 |
slot(15).to_hex(), |
| 379 |
); |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
#[test] |
| 387 |
fn the_container_slot_and_the_text_slot_stay_legible() { |
| 388 |
for id in ["akari-dawn", "akari-night"] { |
| 389 |
let theme = bundled(id); |
| 390 |
let slot = |i: usize| -> Rgb { |
| 391 |
let key = ansi_intent(i, &theme.meta.variant).expect("in range"); |
| 392 |
Rgb::from_hex(theme.colors.get(key).expect("theme carries it")).expect("valid hex") |
| 393 |
}; |
| 394 |
let contrast = wcag_contrast(slot(0), slot(7)); |
| 395 |
assert!(contrast >= 4.5, "{id}: ANSI 0 on ANSI 7 is {contrast:.2}:1"); |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
#[test] |
| 403 |
fn the_chromatic_slots_do_not_vary_with_polarity() { |
| 404 |
for index in [1, 2, 3, 4, 5, 6, 9, 10, 11, 12, 13, 14] { |
| 405 |
assert_eq!( |
| 406 |
ansi_intent(index, "light"), |
| 407 |
ansi_intent(index, "dark"), |
| 408 |
"slot {index} moved with polarity" |
| 409 |
); |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
#[test] |
| 414 |
fn quantize_picks_the_obvious_entry() { |
| 415 |
let black = Rgb { r: 0, g: 0, b: 0 }; |
| 416 |
let white = Rgb { |
| 417 |
r: 255, |
| 418 |
g: 255, |
| 419 |
b: 255, |
| 420 |
}; |
| 421 |
assert_eq!(quantize(black, &ANSI_16), 0); |
| 422 |
assert_eq!(quantize(white, &ANSI_16), 15); |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
#[test] |
| 430 |
fn two_colors_can_quantize_to_one_entry() { |
| 431 |
let page = Rgb::from_hex("#a8a8a8").unwrap(); |
| 432 |
let border = Rgb::from_hex("#b4b4b4").unwrap(); |
| 433 |
|
| 434 |
assert_eq!(quantize(page, &ANSI_16), quantize(border, &ANSI_16)); |
| 435 |
assert_ne!( |
| 436 |
quantize_against(border, page, &ANSI_16), |
| 437 |
quantize(page, &ANSI_16) |
| 438 |
); |
| 439 |
} |
| 440 |
|
| 441 |
#[test] |
| 442 |
fn quantize_against_keeps_the_border_off_the_page() { |
| 443 |
let page = Rgb::from_hex("#e4ded6").unwrap(); |
| 444 |
let border = Rgb::from_hex("#7f786d").unwrap(); |
| 445 |
|
| 446 |
let shown_page = ANSI_16[quantize(page, &ANSI_16)]; |
| 447 |
let shown_border = ANSI_16[quantize_against(border, page, &ANSI_16)]; |
| 448 |
|
| 449 |
assert!( |
| 450 |
wcag_contrast(shown_border, shown_page) >= DISTINCT, |
| 451 |
"border {} on page {} is {:.2}:1", |
| 452 |
shown_border.to_hex(), |
| 453 |
shown_page.to_hex(), |
| 454 |
wcag_contrast(shown_border, shown_page) |
| 455 |
); |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
#[test] |
| 461 |
fn quantize_against_leaves_a_readable_color_alone() { |
| 462 |
let page = Rgb::from_hex("#e4ded6").unwrap(); |
| 463 |
let text = Rgb::from_hex("#1a1816").unwrap(); |
| 464 |
|
| 465 |
assert_eq!( |
| 466 |
quantize_against(text, page, &ANSI_16), |
| 467 |
quantize(text, &ANSI_16) |
| 468 |
); |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
#[test] |
| 475 |
fn an_impossible_palette_gets_the_most_legible_entry() { |
| 476 |
let page = Rgb::from_hex("#ffffff").unwrap(); |
| 477 |
let border = Rgb::from_hex("#fefefe").unwrap(); |
| 478 |
let palette = [ |
| 479 |
Rgb::from_hex("#ffffff").unwrap(), |
| 480 |
Rgb::from_hex("#fdfdfd").unwrap(), |
| 481 |
]; |
| 482 |
|
| 483 |
let chosen = palette[quantize_against(border, page, &palette)]; |
| 484 |
assert_eq!(chosen.to_hex(), "#fdfdfd"); |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
#[test] |
| 507 |
fn a_sixteen_color_terminal_gets_one_bevel_edge_and_not_two() { |
| 508 |
for (id, source) in embedded_themes() { |
| 509 |
let theme = parse_theme_str(id, source, false).unwrap(); |
| 510 |
let t = resolve(&theme); |
| 511 |
let (Some(face), Some(light), Some(dark)) = ( |
| 512 |
t.hex("surface-raised").and_then(Rgb::from_hex), |
| 513 |
t.hex("bevel-light").and_then(Rgb::from_hex), |
| 514 |
t.hex("bevel-dark").and_then(Rgb::from_hex), |
| 515 |
) else { |
| 516 |
continue; |
| 517 |
}; |
| 518 |
|
| 519 |
let face_index = quantize(face, &ANSI_16); |
| 520 |
let light_survives = quantize(light, &ANSI_16) != face_index; |
| 521 |
let dark_survives = quantize(dark, &ANSI_16) != face_index; |
| 522 |
assert!( |
| 523 |
light_survives != dark_survives, |
| 524 |
"{id}: expected exactly one bevel edge to survive 16 colors, \ |
| 525 |
highlight {light_survives} shadow {dark_survives}" |
| 526 |
); |
| 527 |
|
| 528 |
|
| 529 |
assert_eq!( |
| 530 |
quantize_against(light, face, &ANSI_16), |
| 531 |
quantize_against(dark, face, &ANSI_16), |
| 532 |
"{id}: quantize_against is expected to be unusable for a bevel pair" |
| 533 |
); |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
#[test] |
| 547 |
fn two_hundred_fifty_six_colors_keep_both_bevel_edges() { |
| 548 |
const LOSES_AN_EDGE: &[&str] = &[ |
| 549 |
"gruvbox-light", |
| 550 |
"neobrute", |
| 551 |
"oxocarbon-light", |
| 552 |
"rosepine-dawn", |
| 553 |
]; |
| 554 |
|
| 555 |
let mut lost: Vec<String> = Vec::new(); |
| 556 |
for (id, source) in embedded_themes() { |
| 557 |
let theme = parse_theme_str(id, source, false).unwrap(); |
| 558 |
let t = resolve(&theme); |
| 559 |
let (Some(face), Some(light), Some(dark)) = ( |
| 560 |
t.hex("surface-raised").and_then(Rgb::from_hex), |
| 561 |
t.hex("bevel-light").and_then(Rgb::from_hex), |
| 562 |
t.hex("bevel-dark").and_then(Rgb::from_hex), |
| 563 |
) else { |
| 564 |
continue; |
| 565 |
}; |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
let f = quantize(face, ANSI_240); |
| 570 |
let l = quantize(light, ANSI_240); |
| 571 |
let d = quantize(dark, ANSI_240); |
| 572 |
if l == f || d == f || l == d { |
| 573 |
lost.push(id.to_string()); |
| 574 |
} |
| 575 |
} |
| 576 |
lost.sort(); |
| 577 |
|
| 578 |
assert_eq!( |
| 579 |
lost, LOSES_AN_EDGE, |
| 580 |
"themes that cannot hold a two-tone bevel on a 256-color terminal" |
| 581 |
); |
| 582 |
} |
| 583 |
|
| 584 |
#[test] |
| 585 |
fn the_256_table_has_its_three_regions() { |
| 586 |
|
| 587 |
assert_eq!(ANSI_256[..16], ANSI_16); |
| 588 |
|
| 589 |
assert_eq!(ANSI_256[16].tuple(), (0, 0, 0)); |
| 590 |
assert_eq!(ANSI_256[231].tuple(), (255, 255, 255)); |
| 591 |
assert_eq!(ANSI_256[16 + 36 * 2 + 6 * 3 + 4].tuple(), (135, 175, 215)); |
| 592 |
|
| 593 |
assert_eq!(ANSI_256[232].tuple(), (8, 8, 8)); |
| 594 |
assert_eq!(ANSI_256[255].tuple(), (238, 238, 238)); |
| 595 |
|
| 596 |
assert_eq!(ANSI_240.len(), 240); |
| 597 |
assert_eq!(ANSI_240[0], ANSI_256[ANSI_240_OFFSET]); |
| 598 |
} |
| 599 |
} |
| 600 |
|