1#![forbid(unsafe_code)]
18
19pub use llimphi_raster::peniko::Color;
20
21use std::time::Duration;
22
23pub const ENTITY_PALETTE: [(u8, u8, u8); 8] = [
31 (94, 129, 172), (163, 109, 156), (122, 162, 110), (191, 138, 92), (108, 153, 168), (170, 120, 120), (130, 140, 175), (150, 150, 110), ];
40
41pub fn stable_color(seed: &str) -> Color {
45 let mut h: u32 = 2_166_136_261;
46 for b in seed.bytes() {
47 h ^= b as u32;
48 h = h.wrapping_mul(16_777_619);
49 }
50 let (r, g, b) = ENTITY_PALETTE[(h as usize) % ENTITY_PALETTE.len()];
51 Color::from_rgba8(r, g, b, 255)
52}
53
54pub mod motion {
72 use super::Duration;
73
74 pub const MICRO: Duration = Duration::from_millis(50);
76 pub const FAST: Duration = Duration::from_millis(80);
77 pub const NORMAL: Duration = Duration::from_millis(160);
78 pub const SLOW: Duration = Duration::from_millis(320);
79 pub const DRAMATIC: Duration = Duration::from_millis(480);
81
82 #[inline]
85 pub fn ease_out_cubic(t: f32) -> f32 {
86 let inv = 1.0 - t.clamp(0.0, 1.0);
87 1.0 - inv * inv * inv
88 }
89
90 #[inline]
93 pub fn ease_in_out_cubic(t: f32) -> f32 {
94 let t = t.clamp(0.0, 1.0);
95 if t < 0.5 {
96 4.0 * t * t * t
97 } else {
98 let f = -2.0 * t + 2.0;
99 1.0 - f * f * f / 2.0
100 }
101 }
102
103 #[inline]
107 pub fn ease_out_quint(t: f32) -> f32 {
108 let inv = 1.0 - t.clamp(0.0, 1.0);
109 1.0 - inv * inv * inv * inv * inv
110 }
111
112 #[inline]
118 pub fn ease_out_back(t: f32) -> f32 {
119 let t = t.clamp(0.0, 1.0);
120 const C1: f32 = 1.701_58;
121 const C3: f32 = C1 + 1.0;
122 let u = t - 1.0;
123 1.0 + C3 * u * u * u + C1 * u * u
124 }
125
126 #[inline]
129 pub fn linear(t: f32) -> f32 {
130 t.clamp(0.0, 1.0)
131 }
132}
133
134pub mod elevation {
141 pub type Elev = (u8, f64, f64);
144
145 pub const E1: Elev = (44, 4.0, 1.5);
147 pub const E2: Elev = (60, 10.0, 4.0);
149 pub const E3: Elev = (84, 18.0, 8.0);
151 pub const E4: Elev = (110, 32.0, 14.0);
153 pub const E5: Elev = (140, 48.0, 22.0);
155}
156
157pub mod alpha {
161 pub const SCRIM: u8 = 64;
165
166 pub const GLASS_PANEL: u8 = 232;
169
170 pub const DISABLED: u8 = 140;
172
173 pub const HINT: u8 = 96;
175}
176
177pub mod radius {
181 pub const XS: f64 = 2.0;
182 pub const SM: f64 = 4.0;
183 pub const MD: f64 = 8.0;
184 pub const LG: f64 = 12.0;
185 pub const XL: f64 = 20.0;
186}
187
188#[derive(Debug, Clone, Copy)]
192pub struct Theme {
193 pub name: &'static str,
196
197 pub bg_app: Color,
200 pub bg_panel: Color,
202 pub bg_panel_alt: Color,
204 pub bg_input: Color,
206 pub bg_input_focus: Color,
208 pub bg_button: Color,
210 pub bg_button_hover: Color,
212 pub bg_selected: Color,
214 pub bg_row_hover: Color,
216
217 pub fg_text: Color,
219 pub fg_muted: Color,
220 pub fg_placeholder: Color,
221 pub fg_destructive: Color,
222
223 pub border: Color,
225 pub border_focus: Color,
226 pub accent: Color,
229}
230
231impl Default for Theme {
232 fn default() -> Self {
233 Self::dark()
234 }
235}
236
237impl Theme {
238 pub const fn dark() -> Self {
241 Self {
242 name: "Dark",
243 bg_app: Color::from_rgba8(14, 16, 22, 255),
244 bg_panel: Color::from_rgba8(22, 26, 36, 255),
245 bg_panel_alt: Color::from_rgba8(18, 22, 30, 255),
246 bg_input: Color::from_rgba8(16, 20, 28, 255),
247 bg_input_focus: Color::from_rgba8(20, 26, 38, 255),
248 bg_button: Color::from_rgba8(36, 42, 56, 255),
249 bg_button_hover: Color::from_rgba8(54, 64, 86, 255),
250 bg_selected: Color::from_rgba8(58, 78, 128, 255),
251 bg_row_hover: Color::from_rgba8(36, 44, 60, 255),
252 fg_text: Color::from_rgba8(214, 222, 232, 255),
253 fg_muted: Color::from_rgba8(140, 152, 170, 255),
254 fg_placeholder: Color::from_rgba8(95, 105, 122, 255),
255 fg_destructive: Color::from_rgba8(220, 110, 110, 255),
256 border: Color::from_rgba8(46, 54, 70, 255),
257 border_focus: Color::from_rgba8(110, 140, 220, 255),
258 accent: Color::from_rgba8(110, 140, 220, 255),
259 }
260 }
261
262 pub const fn tawa() -> Self {
283 Self {
284 name: "Tawa",
285 bg_app: Color::from_rgba8(20, 19, 17, 255), bg_panel: Color::from_rgba8(30, 28, 26, 255),
287 bg_panel_alt: Color::from_rgba8(25, 24, 22, 255),
288 bg_input: Color::from_rgba8(24, 23, 21, 255),
289 bg_input_focus: Color::from_rgba8(32, 30, 28, 255),
290 bg_button: Color::from_rgba8(42, 40, 37, 255),
291 bg_button_hover: Color::from_rgba8(56, 53, 49, 255),
292 bg_selected: Color::from_rgba8(26, 74, 64, 255), bg_row_hover: Color::from_rgba8(40, 38, 35, 255),
294 fg_text: Color::from_rgba8(232, 230, 224, 255), fg_muted: Color::from_rgba8(160, 154, 144, 255),
296 fg_placeholder: Color::from_rgba8(112, 107, 99, 255),
297 fg_destructive: Color::from_rgba8(232, 116, 97, 255), border: Color::from_rgba8(54, 51, 47, 255),
299 border_focus: Color::from_rgba8(43, 217, 166, 255), accent: Color::from_rgba8(43, 217, 166, 255), }
302 }
303
304 pub const fn light() -> Self {
310 Self {
311 name: "Light",
312 bg_app: Color::from_rgba8(244, 246, 250, 255),
313 bg_panel: Color::from_rgba8(232, 236, 242, 255),
314 bg_panel_alt: Color::from_rgba8(224, 230, 240, 255),
315 bg_input: Color::from_rgba8(255, 255, 255, 255),
316 bg_input_focus: Color::from_rgba8(250, 252, 255, 255),
317 bg_button: Color::from_rgba8(220, 226, 236, 255),
318 bg_button_hover: Color::from_rgba8(200, 210, 226, 255),
319 bg_selected: Color::from_rgba8(160, 180, 220, 255),
320 bg_row_hover: Color::from_rgba8(214, 222, 236, 255),
321 fg_text: Color::from_rgba8(24, 32, 45, 255),
322 fg_muted: Color::from_rgba8(86, 98, 116, 255),
323 fg_placeholder: Color::from_rgba8(140, 150, 168, 255),
324 fg_destructive: Color::from_rgba8(168, 48, 48, 255),
325 border: Color::from_rgba8(190, 199, 214, 255),
326 border_focus: Color::from_rgba8(48, 92, 196, 255),
327 accent: Color::from_rgba8(48, 92, 196, 255),
328 }
329 }
330
331 pub const fn aurora() -> Self {
334 Self {
335 name: "Aurora",
336 bg_app: Color::from_rgba8(8, 18, 22, 255),
337 bg_panel: Color::from_rgba8(14, 28, 34, 255),
338 bg_panel_alt: Color::from_rgba8(12, 24, 30, 255),
339 bg_input: Color::from_rgba8(10, 22, 28, 255),
340 bg_input_focus: Color::from_rgba8(14, 30, 38, 255),
341 bg_button: Color::from_rgba8(20, 44, 52, 255),
342 bg_button_hover: Color::from_rgba8(30, 66, 78, 255),
343 bg_selected: Color::from_rgba8(30, 90, 100, 255),
344 bg_row_hover: Color::from_rgba8(20, 46, 56, 255),
345 fg_text: Color::from_rgba8(214, 232, 232, 255),
346 fg_muted: Color::from_rgba8(130, 168, 168, 255),
347 fg_placeholder: Color::from_rgba8(90, 120, 120, 255),
348 fg_destructive: Color::from_rgba8(220, 110, 110, 255),
349 border: Color::from_rgba8(38, 70, 78, 255),
350 border_focus: Color::from_rgba8(80, 200, 200, 255),
351 accent: Color::from_rgba8(80, 200, 200, 255),
352 }
353 }
354
355 pub const fn sunset() -> Self {
357 Self {
358 name: "Sunset",
359 bg_app: Color::from_rgba8(22, 14, 14, 255),
360 bg_panel: Color::from_rgba8(34, 22, 22, 255),
361 bg_panel_alt: Color::from_rgba8(28, 18, 18, 255),
362 bg_input: Color::from_rgba8(28, 18, 18, 255),
363 bg_input_focus: Color::from_rgba8(36, 24, 22, 255),
364 bg_button: Color::from_rgba8(54, 34, 28, 255),
365 bg_button_hover: Color::from_rgba8(78, 50, 38, 255),
366 bg_selected: Color::from_rgba8(120, 64, 38, 255),
367 bg_row_hover: Color::from_rgba8(56, 36, 28, 255),
368 fg_text: Color::from_rgba8(238, 220, 200, 255),
369 fg_muted: Color::from_rgba8(174, 142, 120, 255),
370 fg_placeholder: Color::from_rgba8(120, 96, 80, 255),
371 fg_destructive: Color::from_rgba8(220, 100, 100, 255),
372 border: Color::from_rgba8(70, 46, 36, 255),
373 border_focus: Color::from_rgba8(232, 140, 70, 255),
374 accent: Color::from_rgba8(232, 140, 70, 255),
375 }
376 }
377
378 pub const fn print() -> Self {
384 Self {
385 name: "Print",
386 bg_app: Color::from_rgba8(255, 255, 255, 255),
387 bg_panel: Color::from_rgba8(255, 255, 255, 255),
388 bg_panel_alt: Color::from_rgba8(246, 246, 246, 255),
389 bg_input: Color::from_rgba8(255, 255, 255, 255),
390 bg_input_focus: Color::from_rgba8(248, 248, 248, 255),
391 bg_button: Color::from_rgba8(238, 238, 238, 255),
392 bg_button_hover: Color::from_rgba8(224, 224, 224, 255),
393 bg_selected: Color::from_rgba8(220, 220, 220, 255),
394 bg_row_hover: Color::from_rgba8(240, 240, 240, 255),
395 fg_text: Color::from_rgba8(0, 0, 0, 255),
396 fg_muted: Color::from_rgba8(90, 90, 90, 255),
397 fg_placeholder: Color::from_rgba8(140, 140, 140, 255),
398 fg_destructive: Color::from_rgba8(0, 0, 0, 255),
399 border: Color::from_rgba8(0, 0, 0, 255),
400 border_focus: Color::from_rgba8(0, 0, 0, 255),
401 accent: Color::from_rgba8(0, 0, 0, 255),
402 }
403 }
404
405 pub const fn xp_blue() -> Self {
408 Self {
409 name: "WinXP",
410 bg_app: Color::from_rgba8(236, 240, 249, 255),
411 bg_panel: Color::from_rgba8(214, 223, 247, 255),
412 bg_panel_alt: Color::from_rgba8(60, 100, 190, 255), bg_input: Color::from_rgba8(255, 255, 255, 255),
414 bg_input_focus: Color::from_rgba8(248, 250, 255, 255),
415 bg_button: Color::from_rgba8(222, 230, 246, 255),
416 bg_button_hover: Color::from_rgba8(198, 214, 244, 255),
417 bg_selected: Color::from_rgba8(49, 106, 197, 255), bg_row_hover: Color::from_rgba8(214, 226, 248, 255),
419 fg_text: Color::from_rgba8(20, 30, 50, 255),
420 fg_muted: Color::from_rgba8(78, 92, 120, 255),
421 fg_placeholder: Color::from_rgba8(130, 142, 168, 255),
422 fg_destructive: Color::from_rgba8(176, 32, 32, 255),
423 border: Color::from_rgba8(122, 152, 206, 255),
424 border_focus: Color::from_rgba8(49, 106, 197, 255),
425 accent: Color::from_rgba8(36, 94, 220, 255), }
427 }
428
429 pub const fn mac_light() -> Self {
432 Self {
433 name: "macOS",
434 bg_app: Color::from_rgba8(246, 246, 248, 255),
435 bg_panel: Color::from_rgba8(236, 236, 240, 255),
436 bg_panel_alt: Color::from_rgba8(242, 242, 245, 235), bg_input: Color::from_rgba8(255, 255, 255, 255),
438 bg_input_focus: Color::from_rgba8(252, 252, 255, 255),
439 bg_button: Color::from_rgba8(228, 228, 233, 255),
440 bg_button_hover: Color::from_rgba8(214, 214, 221, 255),
441 bg_selected: Color::from_rgba8(10, 132, 255, 255),
442 bg_row_hover: Color::from_rgba8(232, 234, 240, 255),
443 fg_text: Color::from_rgba8(28, 28, 32, 255),
444 fg_muted: Color::from_rgba8(110, 110, 120, 255),
445 fg_placeholder: Color::from_rgba8(160, 160, 170, 255),
446 fg_destructive: Color::from_rgba8(215, 58, 50, 255),
447 border: Color::from_rgba8(208, 208, 215, 255),
448 border_focus: Color::from_rgba8(10, 132, 255, 255),
449 accent: Color::from_rgba8(10, 132, 255, 255),
450 }
451 }
452
453 pub const fn kde_breeze() -> Self {
456 Self {
457 name: "Breeze",
458 bg_app: Color::from_rgba8(239, 240, 241, 255),
459 bg_panel: Color::from_rgba8(252, 252, 252, 255),
460 bg_panel_alt: Color::from_rgba8(49, 54, 59, 255), bg_input: Color::from_rgba8(255, 255, 255, 255),
462 bg_input_focus: Color::from_rgba8(248, 252, 254, 255),
463 bg_button: Color::from_rgba8(224, 226, 228, 255),
464 bg_button_hover: Color::from_rgba8(208, 211, 214, 255),
465 bg_selected: Color::from_rgba8(61, 174, 233, 255),
466 bg_row_hover: Color::from_rgba8(227, 229, 231, 255),
467 fg_text: Color::from_rgba8(35, 38, 41, 255),
468 fg_muted: Color::from_rgba8(99, 104, 109, 255),
469 fg_placeholder: Color::from_rgba8(150, 155, 160, 255),
470 fg_destructive: Color::from_rgba8(218, 68, 83, 255),
471 border: Color::from_rgba8(188, 192, 196, 255),
472 border_focus: Color::from_rgba8(61, 174, 233, 255),
473 accent: Color::from_rgba8(61, 174, 233, 255),
474 }
475 }
476
477 pub const fn win31() -> Self {
481 Self {
482 name: "Win3.1",
483 bg_app: Color::from_rgba8(0, 128, 128, 255), bg_panel: Color::from_rgba8(192, 192, 192, 255), bg_panel_alt: Color::from_rgba8(0, 0, 128, 255), bg_input: Color::from_rgba8(255, 255, 255, 255),
487 bg_input_focus: Color::from_rgba8(255, 255, 255, 255),
488 bg_button: Color::from_rgba8(192, 192, 192, 255),
489 bg_button_hover: Color::from_rgba8(208, 208, 208, 255),
490 bg_selected: Color::from_rgba8(0, 0, 128, 255),
491 bg_row_hover: Color::from_rgba8(200, 200, 200, 255),
492 fg_text: Color::from_rgba8(0, 0, 0, 255),
493 fg_muted: Color::from_rgba8(64, 64, 64, 255),
494 fg_placeholder: Color::from_rgba8(112, 112, 112, 255),
495 fg_destructive: Color::from_rgba8(128, 0, 0, 255),
496 border: Color::from_rgba8(128, 128, 128, 255),
497 border_focus: Color::from_rgba8(0, 0, 128, 255),
498 accent: Color::from_rgba8(0, 0, 128, 255), }
500 }
501
502 pub const fn cde() -> Self {
505 Self {
506 name: "CDE",
507 bg_app: Color::from_rgba8(45, 70, 90, 255), bg_panel: Color::from_rgba8(174, 178, 195, 255), bg_panel_alt: Color::from_rgba8(120, 130, 150, 255),
510 bg_input: Color::from_rgba8(220, 222, 230, 255),
511 bg_input_focus: Color::from_rgba8(235, 237, 244, 255),
512 bg_button: Color::from_rgba8(160, 166, 185, 255),
513 bg_button_hover: Color::from_rgba8(176, 182, 200, 255),
514 bg_selected: Color::from_rgba8(90, 130, 130, 255),
515 bg_row_hover: Color::from_rgba8(168, 174, 192, 255),
516 fg_text: Color::from_rgba8(20, 24, 32, 255),
517 fg_muted: Color::from_rgba8(64, 72, 84, 255),
518 fg_placeholder: Color::from_rgba8(100, 108, 120, 255),
519 fg_destructive: Color::from_rgba8(140, 40, 40, 255),
520 border: Color::from_rgba8(108, 116, 134, 255),
521 border_focus: Color::from_rgba8(64, 132, 132, 255),
522 accent: Color::from_rgba8(64, 132, 132, 255), }
524 }
525
526 pub fn sunken(&self) -> Color {
534 let c = self.bg_app.components;
535 let lum = 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2];
538 let factor = if lum < 0.5 { 0.5 } else { 0.93 };
539 Color::from_rgba8(
540 (c[0] * factor * 255.0).round().clamp(0.0, 255.0) as u8,
541 (c[1] * factor * 255.0).round().clamp(0.0, 255.0) as u8,
542 (c[2] * factor * 255.0).round().clamp(0.0, 255.0) as u8,
543 255,
544 )
545 }
546
547 pub fn all() -> Vec<Self> {
554 vec![
555 Self::tawa(),
556 Self::dark(),
557 Self::light(),
558 Self::aurora(),
559 Self::sunset(),
560 ]
561 }
562
563 pub fn by_name(name: &str) -> Option<Self> {
567 Self::all()
568 .into_iter()
569 .chain([
570 Self::print(),
571 Self::xp_blue(),
572 Self::mac_light(),
573 Self::kde_breeze(),
574 Self::win31(),
575 Self::cde(),
576 ])
577 .find(|t| t.name == name)
578 }
579
580 pub fn next_after(current: &str) -> Self {
583 let all = Self::all();
584 let idx = all
585 .iter()
586 .position(|t| t.name == current)
587 .map(|i| (i + 1) % all.len())
588 .unwrap_or(0);
589 all[idx]
590 }
591}
592
593#[cfg(test)]
594mod tests {
595 use super::*;
596
597 #[test]
598 fn presets_have_unique_names() {
599 let all = Theme::all();
600 let mut names: Vec<&str> = all.iter().map(|t| t.name).collect();
601 let n_before = names.len();
602 names.sort();
603 names.dedup();
604 assert_eq!(names.len(), n_before, "nombres duplicados en Theme::all()");
605 }
606
607 #[test]
608 fn by_name_finds_each_preset() {
609 for t in Theme::all() {
610 let by = Theme::by_name(t.name).expect("preset registrado");
611 assert_eq!(by.name, t.name);
612 }
613 }
614
615 #[test]
616 fn by_name_returns_none_for_unknown() {
617 assert!(Theme::by_name("ThisDoesNotExist").is_none());
618 }
619
620 #[test]
621 fn next_after_cycles_through_all_presets() {
622 let all = Theme::all();
623 let mut current = all[0].name;
624 let mut visited = vec![current];
625 for _ in 0..all.len() - 1 {
626 current = Theme::next_after(current).name;
627 visited.push(current);
628 }
629 let names: Vec<&str> = all.iter().map(|t| t.name).collect();
630 assert_eq!(visited, names);
631 let wrapped = Theme::next_after(current).name;
633 assert_eq!(wrapped, all[0].name);
634 }
635
636 #[test]
637 fn next_after_unknown_falls_back_to_first() {
638 let n = Theme::next_after("Nope").name;
639 assert_eq!(n, Theme::all()[0].name);
640 }
641
642 #[test]
643 fn dark_is_the_default() {
644 assert_eq!(Theme::default().name, "Dark");
645 }
646
647 #[test]
650 fn tawa_es_el_primero_y_se_resuelve() {
651 let all = Theme::all();
652 assert_eq!(all[0].name, "Tawa", "Tawa debe ir al frente de la rotación");
653 assert_eq!(Theme::by_name("Tawa").expect("registrado").name, "Tawa");
654 }
655
656 #[test]
660 fn sunken_is_deeper_than_bg_app() {
661 let lum = |c: Color| {
662 let k = c.components;
663 0.2126 * k[0] + 0.7152 * k[1] + 0.0722 * k[2]
664 };
665 for t in Theme::all() {
666 assert!(
667 lum(t.sunken()) < lum(t.bg_app),
668 "{}: sunken debe ser más oscura que bg_app",
669 t.name
670 );
671 }
672 }
673}