1use crate::{Color, color};
3
4use std::sync::LazyLock;
5
6#[derive(Debug, Clone, Copy, PartialEq)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9pub struct Palette {
10 pub background: Color,
12 pub text: Color,
14 pub primary: Color,
16 pub success: Color,
18 pub warning: Color,
20 pub danger: Color,
22}
23
24impl Palette {
25 pub const LIGHT: Self = Self {
27 background: Color::WHITE,
28 text: Color::BLACK,
29 primary: color!(0x5865F2),
30 success: color!(0x12664f),
31 warning: color!(0xffc14e),
32 danger: color!(0xc3423f),
33 };
34
35 pub const DARK: Self = Self {
37 background: color!(0x2B2D31),
38 text: Color::from_rgb(0.90, 0.90, 0.90),
39 primary: color!(0x5865F2),
40 success: color!(0x12664f),
41 warning: color!(0xffc14e),
42 danger: color!(0xc3423f),
43 };
44
45 pub const DRACULA: Self = Self {
49 background: color!(0x282A36), text: color!(0xf8f8f2), primary: color!(0xbd93f9), success: color!(0x50fa7b), warning: color!(0xf1fa8c), danger: color!(0xff5555), };
56
57 pub const NORD: Self = Self {
61 background: color!(0x2e3440), text: color!(0xeceff4), primary: color!(0x8fbcbb), success: color!(0xa3be8c), warning: color!(0xebcb8b), danger: color!(0xbf616a), };
68
69 pub const SOLARIZED_LIGHT: Self = Self {
73 background: color!(0xfdf6e3), text: color!(0x657b83), primary: color!(0x2aa198), success: color!(0x859900), warning: color!(0xb58900), danger: color!(0xdc322f), };
80
81 pub const SOLARIZED_DARK: Self = Self {
85 background: color!(0x002b36), text: color!(0x839496), primary: color!(0x2aa198), success: color!(0x859900), warning: color!(0xb58900), danger: color!(0xdc322f), };
92
93 pub const GRUVBOX_LIGHT: Self = Self {
97 background: color!(0xfbf1c7), text: color!(0x282828), primary: color!(0x458588), success: color!(0x98971a), warning: color!(0xd79921), danger: color!(0xcc241d), };
104
105 pub const GRUVBOX_DARK: Self = Self {
109 background: color!(0x282828), text: color!(0xfbf1c7), primary: color!(0x458588), success: color!(0x98971a), warning: color!(0xd79921), danger: color!(0xcc241d), };
116
117 pub const CATPPUCCIN_LATTE: Self = Self {
121 background: color!(0xeff1f5), text: color!(0x4c4f69), primary: color!(0x1e66f5), success: color!(0x40a02b), warning: color!(0xdf8e1d), danger: color!(0xd20f39), };
128
129 pub const CATPPUCCIN_FRAPPE: Self = Self {
133 background: color!(0x303446), text: color!(0xc6d0f5), primary: color!(0x8caaee), success: color!(0xa6d189), warning: color!(0xe5c890), danger: color!(0xe78284), };
140
141 pub const CATPPUCCIN_MACCHIATO: Self = Self {
145 background: color!(0x24273a), text: color!(0xcad3f5), primary: color!(0x8aadf4), success: color!(0xa6da95), warning: color!(0xeed49f), danger: color!(0xed8796), };
152
153 pub const CATPPUCCIN_MOCHA: Self = Self {
157 background: color!(0x1e1e2e), text: color!(0xcdd6f4), primary: color!(0x89b4fa), success: color!(0xa6e3a1), warning: color!(0xf9e2af), danger: color!(0xf38ba8), };
164
165 pub const TOKYO_NIGHT: Self = Self {
169 background: color!(0x1a1b26), text: color!(0x9aa5ce), primary: color!(0x2ac3de), success: color!(0x9ece6a), warning: color!(0xe0af68), danger: color!(0xf7768e), };
176
177 pub const TOKYO_NIGHT_STORM: Self = Self {
181 background: color!(0x24283b), text: color!(0x9aa5ce), primary: color!(0x2ac3de), success: color!(0x9ece6a), warning: color!(0xe0af68), danger: color!(0xf7768e), };
188
189 pub const TOKYO_NIGHT_LIGHT: Self = Self {
193 background: color!(0xd5d6db), text: color!(0x565a6e), primary: color!(0x166775), success: color!(0x485e30), warning: color!(0x8f5e15), danger: color!(0x8c4351), };
200
201 pub const KANAGAWA_WAVE: Self = Self {
205 background: color!(0x1f1f28), text: color!(0xDCD7BA), primary: color!(0x7FB4CA), success: color!(0x76946A), warning: color!(0xff9e3b), danger: color!(0xC34043), };
212
213 pub const KANAGAWA_DRAGON: Self = Self {
217 background: color!(0x181616), text: color!(0xc5c9c5), primary: color!(0x223249), success: color!(0x8a9a7b), warning: color!(0xff9e3b), danger: color!(0xc4746e), };
224
225 pub const KANAGAWA_LOTUS: Self = Self {
229 background: color!(0xf2ecbc), text: color!(0x545464), primary: color!(0x4d699b), success: color!(0x6f894e), warning: color!(0xe98a00), danger: color!(0xc84053), };
236
237 pub const MOONFLY: Self = Self {
241 background: color!(0x080808), text: color!(0xbdbdbd), primary: color!(0x80a0ff), success: color!(0x8cc85f), warning: color!(0xe3c78a), danger: color!(0xff5454), };
248
249 pub const NIGHTFLY: Self = Self {
253 background: color!(0x011627), text: color!(0xbdc1c6), primary: color!(0x82aaff), success: color!(0xa1cd5e), warning: color!(0xe3d18a), danger: color!(0xfc514e), };
260
261 pub const OXOCARBON: Self = Self {
265 background: color!(0x232323),
266 text: color!(0xd0d0d0),
267 primary: color!(0x00b4ff),
268 success: color!(0x00c15a),
269 warning: color!(0xbe95ff), danger: color!(0xf62d0f),
271 };
272
273 pub const FERRA: Self = Self {
277 background: color!(0x2b292d),
278 text: color!(0xfecdb2),
279 primary: color!(0xd1d1e0),
280 success: color!(0xb1b695),
281 warning: color!(0xf5d76e), danger: color!(0xe06b75),
283 };
284}
285
286#[derive(Debug, Clone, Copy, PartialEq)]
288pub struct Extended {
289 pub background: Background,
291 pub primary: Primary,
293 pub secondary: Secondary,
295 pub success: Success,
297 pub warning: Warning,
299 pub danger: Danger,
301 pub is_dark: bool,
303}
304
305pub static EXTENDED_LIGHT: LazyLock<Extended> =
307 LazyLock::new(|| Extended::generate(Palette::LIGHT));
308
309pub static EXTENDED_DARK: LazyLock<Extended> =
311 LazyLock::new(|| Extended::generate(Palette::DARK));
312
313pub static EXTENDED_DRACULA: LazyLock<Extended> =
315 LazyLock::new(|| Extended::generate(Palette::DRACULA));
316
317pub static EXTENDED_NORD: LazyLock<Extended> =
319 LazyLock::new(|| Extended::generate(Palette::NORD));
320
321pub static EXTENDED_SOLARIZED_LIGHT: LazyLock<Extended> =
323 LazyLock::new(|| Extended::generate(Palette::SOLARIZED_LIGHT));
324
325pub static EXTENDED_SOLARIZED_DARK: LazyLock<Extended> =
327 LazyLock::new(|| Extended::generate(Palette::SOLARIZED_DARK));
328
329pub static EXTENDED_GRUVBOX_LIGHT: LazyLock<Extended> =
331 LazyLock::new(|| Extended::generate(Palette::GRUVBOX_LIGHT));
332
333pub static EXTENDED_GRUVBOX_DARK: LazyLock<Extended> =
335 LazyLock::new(|| Extended::generate(Palette::GRUVBOX_DARK));
336
337pub static EXTENDED_CATPPUCCIN_LATTE: LazyLock<Extended> =
339 LazyLock::new(|| Extended::generate(Palette::CATPPUCCIN_LATTE));
340
341pub static EXTENDED_CATPPUCCIN_FRAPPE: LazyLock<Extended> =
343 LazyLock::new(|| Extended::generate(Palette::CATPPUCCIN_FRAPPE));
344
345pub static EXTENDED_CATPPUCCIN_MACCHIATO: LazyLock<Extended> =
347 LazyLock::new(|| Extended::generate(Palette::CATPPUCCIN_MACCHIATO));
348
349pub static EXTENDED_CATPPUCCIN_MOCHA: LazyLock<Extended> =
351 LazyLock::new(|| Extended::generate(Palette::CATPPUCCIN_MOCHA));
352
353pub static EXTENDED_TOKYO_NIGHT: LazyLock<Extended> =
355 LazyLock::new(|| Extended::generate(Palette::TOKYO_NIGHT));
356
357pub static EXTENDED_TOKYO_NIGHT_STORM: LazyLock<Extended> =
359 LazyLock::new(|| Extended::generate(Palette::TOKYO_NIGHT_STORM));
360
361pub static EXTENDED_TOKYO_NIGHT_LIGHT: LazyLock<Extended> =
363 LazyLock::new(|| Extended::generate(Palette::TOKYO_NIGHT_LIGHT));
364
365pub static EXTENDED_KANAGAWA_WAVE: LazyLock<Extended> =
367 LazyLock::new(|| Extended::generate(Palette::KANAGAWA_WAVE));
368
369pub static EXTENDED_KANAGAWA_DRAGON: LazyLock<Extended> =
371 LazyLock::new(|| Extended::generate(Palette::KANAGAWA_DRAGON));
372
373pub static EXTENDED_KANAGAWA_LOTUS: LazyLock<Extended> =
375 LazyLock::new(|| Extended::generate(Palette::KANAGAWA_LOTUS));
376
377pub static EXTENDED_MOONFLY: LazyLock<Extended> =
379 LazyLock::new(|| Extended::generate(Palette::MOONFLY));
380
381pub static EXTENDED_NIGHTFLY: LazyLock<Extended> =
383 LazyLock::new(|| Extended::generate(Palette::NIGHTFLY));
384
385pub static EXTENDED_OXOCARBON: LazyLock<Extended> =
387 LazyLock::new(|| Extended::generate(Palette::OXOCARBON));
388
389pub static EXTENDED_FERRA: LazyLock<Extended> =
391 LazyLock::new(|| Extended::generate(Palette::FERRA));
392
393impl Extended {
394 pub fn generate(palette: Palette) -> Self {
396 Self {
397 background: Background::new(palette.background, palette.text),
398 primary: Primary::generate(
399 palette.primary,
400 palette.background,
401 palette.text,
402 ),
403 secondary: Secondary::generate(palette.background, palette.text),
404 success: Success::generate(
405 palette.success,
406 palette.background,
407 palette.text,
408 ),
409 warning: Warning::generate(
410 palette.warning,
411 palette.background,
412 palette.text,
413 ),
414 danger: Danger::generate(
415 palette.danger,
416 palette.background,
417 palette.text,
418 ),
419 is_dark: is_dark(palette.background),
420 }
421 }
422}
423
424#[derive(Debug, Clone, Copy, PartialEq)]
426pub struct Pair {
427 pub color: Color,
429
430 pub text: Color,
436}
437
438impl Pair {
439 pub fn new(color: Color, text: Color) -> Self {
441 Self {
442 color,
443 text: readable(color, text),
444 }
445 }
446}
447
448#[derive(Debug, Clone, Copy, PartialEq)]
450pub struct Background {
451 pub base: Pair,
453 pub weakest: Pair,
455 pub weak: Pair,
457 pub strong: Pair,
459 pub strongest: Pair,
461}
462
463impl Background {
464 pub fn new(base: Color, text: Color) -> Self {
466 let weakest = deviate(base, 0.03);
467 let weak = muted(deviate(base, 0.1));
468 let strong = muted(deviate(base, 0.2));
469 let strongest = muted(deviate(base, 0.3));
470
471 Self {
472 base: Pair::new(base, text),
473 weakest: Pair::new(weakest, text),
474 weak: Pair::new(weak, text),
475 strong: Pair::new(strong, text),
476 strongest: Pair::new(strongest, text),
477 }
478 }
479}
480
481#[derive(Debug, Clone, Copy, PartialEq)]
483pub struct Primary {
484 pub base: Pair,
486 pub weak: Pair,
488 pub strong: Pair,
490}
491
492impl Primary {
493 pub fn generate(base: Color, background: Color, text: Color) -> Self {
495 let weak = mix(base, background, 0.4);
496 let strong = deviate(base, 0.1);
497
498 Self {
499 base: Pair::new(base, text),
500 weak: Pair::new(weak, text),
501 strong: Pair::new(strong, text),
502 }
503 }
504}
505
506#[derive(Debug, Clone, Copy, PartialEq)]
508pub struct Secondary {
509 pub base: Pair,
511 pub weak: Pair,
513 pub strong: Pair,
515}
516
517impl Secondary {
518 pub fn generate(base: Color, text: Color) -> Self {
520 let base = mix(base, text, 0.2);
521 let weak = mix(base, text, 0.1);
522 let strong = mix(base, text, 0.3);
523
524 Self {
525 base: Pair::new(base, text),
526 weak: Pair::new(weak, text),
527 strong: Pair::new(strong, text),
528 }
529 }
530}
531
532#[derive(Debug, Clone, Copy, PartialEq)]
534pub struct Success {
535 pub base: Pair,
537 pub weak: Pair,
539 pub strong: Pair,
541}
542
543impl Success {
544 pub fn generate(base: Color, background: Color, text: Color) -> Self {
546 let weak = mix(base, background, 0.4);
547 let strong = deviate(base, 0.1);
548
549 Self {
550 base: Pair::new(base, text),
551 weak: Pair::new(weak, text),
552 strong: Pair::new(strong, text),
553 }
554 }
555}
556
557#[derive(Debug, Clone, Copy, PartialEq)]
559pub struct Warning {
560 pub base: Pair,
562 pub weak: Pair,
564 pub strong: Pair,
566}
567
568impl Warning {
569 pub fn generate(base: Color, background: Color, text: Color) -> Self {
571 let weak = mix(base, background, 0.4);
572 let strong = deviate(base, 0.1);
573
574 Self {
575 base: Pair::new(base, text),
576 weak: Pair::new(weak, text),
577 strong: Pair::new(strong, text),
578 }
579 }
580}
581
582#[derive(Debug, Clone, Copy, PartialEq)]
584pub struct Danger {
585 pub base: Pair,
587 pub weak: Pair,
589 pub strong: Pair,
591}
592
593impl Danger {
594 pub fn generate(base: Color, background: Color, text: Color) -> Self {
596 let weak = mix(base, background, 0.4);
597 let strong = deviate(base, 0.1);
598
599 Self {
600 base: Pair::new(base, text),
601 weak: Pair::new(weak, text),
602 strong: Pair::new(strong, text),
603 }
604 }
605}
606
607struct Hsl {
608 h: f32,
609 s: f32,
610 l: f32,
611 a: f32,
612}
613
614fn darken(color: Color, amount: f32) -> Color {
615 let mut hsl = to_hsl(color);
616
617 hsl.l = if hsl.l - amount < 0.0 {
618 0.0
619 } else {
620 hsl.l - amount
621 };
622
623 from_hsl(hsl)
624}
625
626fn lighten(color: Color, amount: f32) -> Color {
627 let mut hsl = to_hsl(color);
628
629 hsl.l = if hsl.l + amount > 1.0 {
630 1.0
631 } else {
632 hsl.l + amount
633 };
634
635 from_hsl(hsl)
636}
637
638fn deviate(color: Color, amount: f32) -> Color {
639 if is_dark(color) {
640 lighten(color, amount)
641 } else {
642 darken(color, amount * 0.8)
643 }
644}
645
646fn muted(color: Color) -> Color {
647 let mut hsl = to_hsl(color);
648
649 hsl.s = hsl.s.min(0.5);
650
651 from_hsl(hsl)
652}
653
654fn mix(a: Color, b: Color, factor: f32) -> Color {
655 let b_amount = factor.clamp(0.0, 1.0);
656 let a_amount = 1.0 - b_amount;
657
658 let a_linear = a.into_linear().map(|c| c * a_amount);
659 let b_linear = b.into_linear().map(|c| c * b_amount);
660
661 Color::from_linear_rgba(
662 a_linear[0] + b_linear[0],
663 a_linear[1] + b_linear[1],
664 a_linear[2] + b_linear[2],
665 a_linear[3] + b_linear[3],
666 )
667}
668
669fn readable(background: Color, text: Color) -> Color {
670 if is_readable(background, text) {
671 return text;
672 }
673
674 let improve = if is_dark(background) { lighten } else { darken };
675
676 let candidate = improve(text, 0.1);
678
679 if is_readable(background, candidate) {
680 return candidate;
681 }
682
683 let white_contrast = relative_contrast(background, Color::WHITE);
684 let black_contrast = relative_contrast(background, Color::BLACK);
685
686 if white_contrast >= black_contrast {
687 mix(Color::WHITE, background, 0.05)
688 } else {
689 mix(Color::BLACK, background, 0.05)
690 }
691}
692
693fn is_dark(color: Color) -> bool {
694 to_hsl(color).l < 0.6
695}
696
697fn is_readable(a: Color, b: Color) -> bool {
698 relative_contrast(a, b) >= 7.0
699}
700
701fn relative_contrast(a: Color, b: Color) -> f32 {
703 let lum_a = relative_luminance(a);
704 let lum_b = relative_luminance(b);
705 (lum_a.max(lum_b) + 0.05) / (lum_a.min(lum_b) + 0.05)
706}
707
708fn relative_luminance(color: Color) -> f32 {
710 let linear = color.into_linear();
711 0.2126 * linear[0] + 0.7152 * linear[1] + 0.0722 * linear[2]
712}
713
714fn to_hsl(color: Color) -> Hsl {
716 let x_max = color.r.max(color.g).max(color.b);
717 let x_min = color.r.min(color.g).min(color.b);
718 let c = x_max - x_min;
719 let l = x_max.midpoint(x_min);
720
721 let h = if c == 0.0 {
722 0.0
723 } else if x_max == color.r {
724 60.0 * ((color.g - color.b) / c).rem_euclid(6.0)
725 } else if x_max == color.g {
726 60.0 * (((color.b - color.r) / c) + 2.0)
727 } else {
728 60.0 * (((color.r - color.g) / c) + 4.0)
730 };
731
732 let s = if l == 0.0 || l == 1.0 {
733 0.0
734 } else {
735 (x_max - l) / l.min(1.0 - l)
736 };
737
738 Hsl {
739 h,
740 s,
741 l,
742 a: color.a,
743 }
744}
745
746fn from_hsl(hsl: Hsl) -> Color {
748 let c = (1.0 - (2.0 * hsl.l - 1.0).abs()) * hsl.s;
749 let h = hsl.h / 60.0;
750 let x = c * (1.0 - (h.rem_euclid(2.0) - 1.0).abs());
751
752 let (r1, g1, b1) = if h < 1.0 {
753 (c, x, 0.0)
754 } else if h < 2.0 {
755 (x, c, 0.0)
756 } else if h < 3.0 {
757 (0.0, c, x)
758 } else if h < 4.0 {
759 (0.0, x, c)
760 } else if h < 5.0 {
761 (x, 0.0, c)
762 } else {
763 (c, 0.0, x)
765 };
766
767 let m = hsl.l - (c / 2.0);
768
769 Color {
770 r: r1 + m,
771 g: g1 + m,
772 b: b1 + m,
773 a: hsl.a,
774 }
775}