| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
use makeover::{Rgb, ThemeColors}; |
| 19 |
use ratatui::style::Color; |
| 20 |
|
| 21 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 22 |
pub enum Mode { |
| 23 |
Light, |
| 24 |
Dark, |
| 25 |
HighContrast, |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#[derive(Debug, Clone, Copy)] |
| 43 |
#[non_exhaustive] |
| 44 |
pub struct Theme { |
| 45 |
pub mode: Mode, |
| 46 |
|
| 47 |
pub surface_page: Color, |
| 48 |
pub surface_raised: Color, |
| 49 |
pub surface_sunken: Color, |
| 50 |
pub surface_overlay: Color, |
| 51 |
|
| 52 |
pub content_primary: Color, |
| 53 |
pub content_secondary: Color, |
| 54 |
pub content_muted: Color, |
| 55 |
|
| 56 |
pub action_primary: Color, |
| 57 |
|
| 58 |
pub status_danger: Color, |
| 59 |
pub status_success: Color, |
| 60 |
pub status_warning: Color, |
| 61 |
pub status_info: Color, |
| 62 |
|
| 63 |
pub line_border: Color, |
| 64 |
pub border_subtle: Color, |
| 65 |
pub border_strong: Color, |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
pub bevel_light: Color, |
| 76 |
pub bevel_dark: Color, |
| 77 |
|
| 78 |
pub category: [Color; 6], |
| 79 |
} |
| 80 |
|
| 81 |
#[derive(Debug, Clone)] |
| 82 |
pub enum ThemeError { |
| 83 |
MissingKey(&'static str), |
| 84 |
InvalidHex { key: &'static str, value: String }, |
| 85 |
} |
| 86 |
|
| 87 |
impl std::fmt::Display for ThemeError { |
| 88 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 89 |
match self { |
| 90 |
ThemeError::MissingKey(k) => write!(f, "theme missing required key `{k}`"), |
| 91 |
ThemeError::InvalidHex { key, value } => { |
| 92 |
write!(f, "theme key `{key}` has invalid hex value `{value}`") |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
impl std::error::Error for ThemeError {} |
| 99 |
|
| 100 |
impl Theme { |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
pub fn from_theme(theme: &ThemeColors) -> Result<Self, ThemeError> { |
| 105 |
let get = |key: &'static str| -> Result<Rgb, ThemeError> { |
| 106 |
let hex = theme.colors.get(key).ok_or(ThemeError::MissingKey(key))?; |
| 107 |
Rgb::from_hex(hex).ok_or_else(|| ThemeError::InvalidHex { |
| 108 |
key, |
| 109 |
value: hex.clone(), |
| 110 |
}) |
| 111 |
}; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
let resolved = makeover::resolve(theme); |
| 117 |
let intent = |key: &'static str| -> Result<Rgb, ThemeError> { |
| 118 |
let hex = resolved.hex(key).ok_or(ThemeError::MissingKey(key))?; |
| 119 |
Rgb::from_hex(hex).ok_or_else(|| ThemeError::InvalidHex { |
| 120 |
key, |
| 121 |
value: hex.to_string(), |
| 122 |
}) |
| 123 |
}; |
| 124 |
|
| 125 |
let surface_page = get("surface.page")?; |
| 126 |
let content_primary = get("content.primary")?; |
| 127 |
let line_border = get("line.border")?; |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
let border_subtle = border_subtle(line_border, surface_page); |
| 138 |
let border_strong = border_strong(line_border, content_primary); |
| 139 |
|
| 140 |
let mode = match theme.meta.variant.as_str() { |
| 141 |
"dark" => Mode::Dark, |
| 142 |
"high-contrast" => Mode::HighContrast, |
| 143 |
_ => Mode::Light, |
| 144 |
}; |
| 145 |
|
| 146 |
Ok(Self { |
| 147 |
mode, |
| 148 |
|
| 149 |
surface_page: rgb(surface_page), |
| 150 |
surface_raised: rgb(get("surface.raised")?), |
| 151 |
surface_sunken: rgb(get("surface.sunken")?), |
| 152 |
surface_overlay: rgb(get("surface.overlay")?), |
| 153 |
|
| 154 |
content_primary: rgb(content_primary), |
| 155 |
content_secondary: rgb(get("content.secondary")?), |
| 156 |
content_muted: rgb(get("content.muted")?), |
| 157 |
|
| 158 |
action_primary: rgb(get("action.primary")?), |
| 159 |
|
| 160 |
status_danger: rgb(get("status.danger")?), |
| 161 |
status_success: rgb(get("status.success")?), |
| 162 |
status_warning: rgb(get("status.warning")?), |
| 163 |
status_info: rgb(get("status.info")?), |
| 164 |
|
| 165 |
line_border: rgb(line_border), |
| 166 |
border_subtle: rgb(border_subtle), |
| 167 |
border_strong: rgb(border_strong), |
| 168 |
|
| 169 |
bevel_light: rgb(intent("bevel-light")?), |
| 170 |
bevel_dark: rgb(intent("bevel-dark")?), |
| 171 |
|
| 172 |
category: [ |
| 173 |
rgb(get("category.one")?), |
| 174 |
rgb(get("category.two")?), |
| 175 |
rgb(get("category.three")?), |
| 176 |
rgb(get("category.four")?), |
| 177 |
rgb(get("category.five")?), |
| 178 |
rgb(get("category.six")?), |
| 179 |
], |
| 180 |
}) |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
fn rgb(c: Rgb) -> Color { |
| 185 |
Color::Rgb(c.r, c.g, c.b) |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 190 |
pub enum ColorDepth { |
| 191 |
|
| 192 |
Full, |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
Ansi256, |
| 200 |
|
| 201 |
Ansi16, |
| 202 |
} |
| 203 |
|
| 204 |
impl ColorDepth { |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
fn palette(self) -> Option<(&'static [makeover::Rgb], usize)> { |
| 212 |
match self { |
| 213 |
ColorDepth::Full => None, |
| 214 |
ColorDepth::Ansi256 => Some((makeover::ANSI_240, makeover::ANSI_240_OFFSET)), |
| 215 |
ColorDepth::Ansi16 => Some((&makeover::ANSI_16, 0)), |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
pub fn detect_color_depth() -> ColorDepth { |
| 237 |
depth_from_env( |
| 238 |
&std::env::var("COLORTERM").unwrap_or_default(), |
| 239 |
&std::env::var("TERM").unwrap_or_default(), |
| 240 |
) |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
fn depth_from_env(colorterm: &str, term: &str) -> ColorDepth { |
| 246 |
if colorterm == "truecolor" || colorterm == "24bit" { |
| 247 |
return ColorDepth::Full; |
| 248 |
} |
| 249 |
match term { |
| 250 |
"linux" | "vt100" | "vt220" | "ansi" | "dumb" => ColorDepth::Ansi16, |
| 251 |
_ if term.ends_with("-256color") => ColorDepth::Ansi256, |
| 252 |
_ => ColorDepth::Full, |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
fn indexed(c: Color, palette: &[Rgb], offset: usize) -> Color { |
| 258 |
match c { |
| 259 |
Color::Rgb(r, g, b) => { |
| 260 |
Color::Indexed((makeover::quantize(Rgb { r, g, b }, palette) + offset) as u8) |
| 261 |
} |
| 262 |
other => other, |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
fn indexed_against(c: Color, on: Color, palette: &[Rgb], offset: usize) -> Color { |
| 275 |
match (c, on) { |
| 276 |
(Color::Rgb(r, g, b), Color::Rgb(br, bg, bb)) => Color::Indexed( |
| 277 |
(makeover::quantize_against( |
| 278 |
Rgb { r, g, b }, |
| 279 |
Rgb { |
| 280 |
r: br, |
| 281 |
g: bg, |
| 282 |
b: bb, |
| 283 |
}, |
| 284 |
palette, |
| 285 |
) + offset) as u8, |
| 286 |
), |
| 287 |
_ => indexed(c, palette, offset), |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
impl Theme { |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
#[must_use] |
| 315 |
pub fn for_terminal(self, depth: ColorDepth) -> Theme { |
| 316 |
let Some((palette, offset)) = depth.palette() else { |
| 317 |
return self; |
| 318 |
}; |
| 319 |
|
| 320 |
let plain = |c: Color| indexed(c, palette, offset); |
| 321 |
let on_page = |c: Color| indexed_against(c, self.surface_page, palette, offset); |
| 322 |
|
| 323 |
Theme { |
| 324 |
mode: self.mode, |
| 325 |
|
| 326 |
surface_page: plain(self.surface_page), |
| 327 |
surface_raised: plain(self.surface_raised), |
| 328 |
surface_sunken: plain(self.surface_sunken), |
| 329 |
surface_overlay: plain(self.surface_overlay), |
| 330 |
|
| 331 |
content_primary: on_page(self.content_primary), |
| 332 |
content_secondary: on_page(self.content_secondary), |
| 333 |
content_muted: on_page(self.content_muted), |
| 334 |
|
| 335 |
action_primary: on_page(self.action_primary), |
| 336 |
|
| 337 |
status_danger: on_page(self.status_danger), |
| 338 |
status_success: on_page(self.status_success), |
| 339 |
status_warning: on_page(self.status_warning), |
| 340 |
status_info: on_page(self.status_info), |
| 341 |
|
| 342 |
line_border: on_page(self.line_border), |
| 343 |
border_subtle: on_page(self.border_subtle), |
| 344 |
border_strong: on_page(self.border_strong), |
| 345 |
|
| 346 |
bevel_light: plain(self.bevel_light), |
| 347 |
bevel_dark: plain(self.bevel_dark), |
| 348 |
|
| 349 |
category: self.category.map(on_page), |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
pub fn border_subtle(line_border: Rgb, surface_page: Rgb) -> Rgb { |
| 362 |
mix_linear_srgb(line_border, surface_page, 0.60) |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
pub fn border_strong(line_border: Rgb, content_primary: Rgb) -> Rgb { |
| 371 |
mix_linear_srgb(line_border, content_primary, 0.65) |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
pub fn mix_linear_srgb(a: Rgb, b: Rgb, t: f32) -> Rgb { |
| 383 |
let al = srgb_to_linear(a); |
| 384 |
let bl = srgb_to_linear(b); |
| 385 |
let m = ( |
| 386 |
al.0 + (bl.0 - al.0) * t, |
| 387 |
al.1 + (bl.1 - al.1) * t, |
| 388 |
al.2 + (bl.2 - al.2) * t, |
| 389 |
); |
| 390 |
linear_to_srgb(m) |
| 391 |
} |
| 392 |
|
| 393 |
fn srgb_to_linear(c: Rgb) -> (f32, f32, f32) { |
| 394 |
( |
| 395 |
channel_to_linear(c.r), |
| 396 |
channel_to_linear(c.g), |
| 397 |
channel_to_linear(c.b), |
| 398 |
) |
| 399 |
} |
| 400 |
|
| 401 |
fn linear_to_srgb(c: (f32, f32, f32)) -> Rgb { |
| 402 |
Rgb { |
| 403 |
r: channel_to_srgb(c.0), |
| 404 |
g: channel_to_srgb(c.1), |
| 405 |
b: channel_to_srgb(c.2), |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
fn channel_to_linear(c: u8) -> f32 { |
| 410 |
let c = c as f32 / 255.0; |
| 411 |
if c <= 0.04045 { |
| 412 |
c / 12.92 |
| 413 |
} else { |
| 414 |
((c + 0.055) / 1.055).powf(2.4) |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
fn channel_to_srgb(c: f32) -> u8 { |
| 419 |
let v = if c <= 0.003_130_8 { |
| 420 |
c * 12.92 |
| 421 |
} else { |
| 422 |
1.055 * c.powf(1.0 / 2.4) - 0.055 |
| 423 |
}; |
| 424 |
(v * 255.0).round().clamp(0.0, 255.0) as u8 |
| 425 |
} |
| 426 |
|
| 427 |
#[cfg(test)] |
| 428 |
mod tests { |
| 429 |
use super::*; |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
#[test] |
| 436 |
fn akari_dawn_border_strong_matches_tokens_md() { |
| 437 |
let border = Rgb::from_hex("#cabeae").unwrap(); |
| 438 |
let primary = Rgb::from_hex("#1a1816").unwrap(); |
| 439 |
let got = border_strong(border, primary); |
| 440 |
assert_eq!( |
| 441 |
(got.r, got.g, got.b), |
| 442 |
(0x7f, 0x78, 0x6d), |
| 443 |
"border-strong derivation drifted; got #{:02x}{:02x}{:02x}, expected #7f786d", |
| 444 |
got.r, |
| 445 |
got.g, |
| 446 |
got.b |
| 447 |
); |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
#[test] |
| 454 |
fn akari_dawn_border_subtle_matches_the_shipped_skeleton() { |
| 455 |
let border = Rgb::from_hex("#cabeae").unwrap(); |
| 456 |
let page = Rgb::from_hex("#e4ded6").unwrap(); |
| 457 |
let got = border_subtle(border, page); |
| 458 |
assert_eq!( |
| 459 |
(got.r, got.g, got.b), |
| 460 |
(0xda, 0xd2, 0xc7), |
| 461 |
"border-subtle derivation drifted; got #{:02x}{:02x}{:02x}, expected #dad2c7", |
| 462 |
got.r, |
| 463 |
got.g, |
| 464 |
got.b |
| 465 |
); |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
fn akari_dawn() -> Theme { |
| 471 |
let page = Color::Rgb(0xe4, 0xde, 0xd6); |
| 472 |
Theme { |
| 473 |
mode: Mode::Light, |
| 474 |
surface_page: page, |
| 475 |
surface_raised: page, |
| 476 |
surface_sunken: page, |
| 477 |
surface_overlay: page, |
| 478 |
content_primary: Color::Rgb(0x1a, 0x18, 0x16), |
| 479 |
content_secondary: Color::Rgb(0x1a, 0x18, 0x16), |
| 480 |
content_muted: Color::Rgb(0x7f, 0x78, 0x6d), |
| 481 |
action_primary: Color::Rgb(0x8a, 0x45, 0x30), |
| 482 |
status_danger: Color::Rgb(0x8a, 0x45, 0x30), |
| 483 |
status_success: Color::Rgb(0x8a, 0x45, 0x30), |
| 484 |
status_warning: Color::Rgb(0x8a, 0x45, 0x30), |
| 485 |
status_info: Color::Rgb(0x8a, 0x45, 0x30), |
| 486 |
line_border: Color::Rgb(0xca, 0xbe, 0xae), |
| 487 |
border_subtle: Color::Rgb(0xda, 0xd2, 0xc7), |
| 488 |
border_strong: Color::Rgb(0x7f, 0x78, 0x6d), |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
bevel_light: Color::Rgb(0xff, 0xfe, 0xf5), |
| 493 |
bevel_dark: Color::Rgb(0xb3, 0xad, 0xa5), |
| 494 |
category: [Color::Rgb(0x8a, 0x45, 0x30); 6], |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
#[test] |
| 499 |
fn a_capable_terminal_gets_the_theme_as_authored() { |
| 500 |
let theme = akari_dawn().for_terminal(ColorDepth::Full); |
| 501 |
assert_eq!(theme.border_strong, Color::Rgb(0x7f, 0x78, 0x6d)); |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
#[test] |
| 507 |
fn a_sixteen_color_terminal_gets_indices() { |
| 508 |
let theme = akari_dawn().for_terminal(ColorDepth::Ansi16); |
| 509 |
for color in [ |
| 510 |
theme.surface_page, |
| 511 |
theme.content_primary, |
| 512 |
theme.border_strong, |
| 513 |
theme.border_subtle, |
| 514 |
theme.line_border, |
| 515 |
] { |
| 516 |
assert!(matches!(color, Color::Indexed(_)), "{color:?}"); |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
#[test] |
| 523 |
fn a_256_color_terminal_keeps_both_bevel_edges() { |
| 524 |
let theme = akari_dawn().for_terminal(ColorDepth::Ansi256); |
| 525 |
assert_ne!(theme.bevel_light, theme.surface_raised); |
| 526 |
assert_ne!(theme.bevel_dark, theme.surface_raised); |
| 527 |
assert_ne!(theme.bevel_light, theme.bevel_dark); |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
#[test] |
| 534 |
fn a_sixteen_color_terminal_loses_one_bevel_edge() { |
| 535 |
let theme = akari_dawn().for_terminal(ColorDepth::Ansi16); |
| 536 |
let light_survives = theme.bevel_light != theme.surface_raised; |
| 537 |
let dark_survives = theme.bevel_dark != theme.surface_raised; |
| 538 |
assert!( |
| 539 |
light_survives != dark_survives, |
| 540 |
"expected exactly one edge to survive, light {light_survives} dark {dark_survives}" |
| 541 |
); |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
#[test] |
| 548 |
fn the_256_indices_land_outside_the_repaintable_low_sixteen() { |
| 549 |
let theme = akari_dawn().for_terminal(ColorDepth::Ansi256); |
| 550 |
for color in [ |
| 551 |
theme.surface_page, |
| 552 |
theme.content_primary, |
| 553 |
theme.border_strong, |
| 554 |
theme.bevel_light, |
| 555 |
theme.bevel_dark, |
| 556 |
] { |
| 557 |
let Color::Indexed(i) = color else { |
| 558 |
panic!("{color:?} is not an index") |
| 559 |
}; |
| 560 |
assert!(i >= 16, "index {i} is in the repaintable range"); |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
#[test] |
| 565 |
fn a_256_color_term_is_detected_from_its_name() { |
| 566 |
let depth = depth_from_env; |
| 567 |
assert_eq!(depth("", "xterm-256color"), ColorDepth::Ansi256); |
| 568 |
assert_eq!(depth("", "screen-256color"), ColorDepth::Ansi256); |
| 569 |
|
| 570 |
assert_eq!(depth("truecolor", "xterm-256color"), ColorDepth::Full); |
| 571 |
|
| 572 |
assert_eq!(depth("", "linux"), ColorDepth::Ansi16); |
| 573 |
assert_eq!(depth("", "foot"), ColorDepth::Full); |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
#[test] |
| 579 |
fn the_frame_stays_visible_against_the_page() { |
| 580 |
let theme = akari_dawn().for_terminal(ColorDepth::Ansi16); |
| 581 |
assert_ne!(theme.border_strong, theme.surface_page); |
| 582 |
assert_ne!(theme.border_subtle, theme.surface_page); |
| 583 |
assert_ne!(theme.content_primary, theme.surface_page); |
| 584 |
} |
| 585 |
} |
| 586 |
|