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!(0xb77e33),
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 weaker: Pair,
457 pub weak: Pair,
459 pub neutral: Pair,
461 pub strong: Pair,
463 pub stronger: Pair,
465 pub strongest: Pair,
467}
468
469impl Background {
470 pub fn new(base: Color, text: Color) -> Self {
472 let weakest = deviate(base, 0.03);
473 let weaker = deviate(base, 0.07);
474 let weak = deviate(base, 0.1);
475 let neutral = deviate(base, 0.125);
476 let strong = deviate(base, 0.15);
477 let stronger = deviate(base, 0.175);
478 let strongest = deviate(base, 0.20);
479
480 Self {
481 base: Pair::new(base, text),
482 weakest: Pair::new(weakest, text),
483 weaker: Pair::new(weaker, text),
484 weak: Pair::new(weak, text),
485 neutral: Pair::new(neutral, text),
486 strong: Pair::new(strong, text),
487 stronger: Pair::new(stronger, text),
488 strongest: Pair::new(strongest, text),
489 }
490 }
491}
492
493#[derive(Debug, Clone, Copy, PartialEq)]
495pub struct Primary {
496 pub base: Pair,
498 pub weak: Pair,
500 pub strong: Pair,
502}
503
504impl Primary {
505 pub fn generate(base: Color, background: Color, text: Color) -> Self {
507 let weak = mix(base, background, 0.4);
508 let strong = deviate(base, 0.1);
509
510 Self {
511 base: Pair::new(base, text),
512 weak: Pair::new(weak, text),
513 strong: Pair::new(strong, text),
514 }
515 }
516}
517
518#[derive(Debug, Clone, Copy, PartialEq)]
520pub struct Secondary {
521 pub base: Pair,
523 pub weak: Pair,
525 pub strong: Pair,
527}
528
529impl Secondary {
530 pub fn generate(base: Color, text: Color) -> Self {
532 let factor = if is_dark(base) { 0.2 } else { 0.4 };
533
534 let weak = mix(deviate(base, 0.1), text, factor);
535 let strong = mix(deviate(base, 0.3), text, factor);
536 let base = mix(deviate(base, 0.2), text, factor);
537
538 Self {
539 base: Pair::new(base, text),
540 weak: Pair::new(weak, text),
541 strong: Pair::new(strong, text),
542 }
543 }
544}
545
546#[derive(Debug, Clone, Copy, PartialEq)]
548pub struct Success {
549 pub base: Pair,
551 pub weak: Pair,
553 pub strong: Pair,
555}
556
557impl Success {
558 pub fn generate(base: Color, background: Color, text: Color) -> Self {
560 let weak = mix(base, background, 0.4);
561 let strong = deviate(base, 0.1);
562
563 Self {
564 base: Pair::new(base, text),
565 weak: Pair::new(weak, text),
566 strong: Pair::new(strong, text),
567 }
568 }
569}
570
571#[derive(Debug, Clone, Copy, PartialEq)]
573pub struct Warning {
574 pub base: Pair,
576 pub weak: Pair,
578 pub strong: Pair,
580}
581
582impl Warning {
583 pub fn generate(base: Color, background: Color, text: Color) -> Self {
585 let weak = mix(base, background, 0.4);
586 let strong = deviate(base, 0.1);
587
588 Self {
589 base: Pair::new(base, text),
590 weak: Pair::new(weak, text),
591 strong: Pair::new(strong, text),
592 }
593 }
594}
595
596#[derive(Debug, Clone, Copy, PartialEq)]
598pub struct Danger {
599 pub base: Pair,
601 pub weak: Pair,
603 pub strong: Pair,
605}
606
607impl Danger {
608 pub fn generate(base: Color, background: Color, text: Color) -> Self {
610 let weak = mix(base, background, 0.4);
611 let strong = deviate(base, 0.1);
612
613 Self {
614 base: Pair::new(base, text),
615 weak: Pair::new(weak, text),
616 strong: Pair::new(strong, text),
617 }
618 }
619}
620
621struct Oklch {
622 l: f32,
623 c: f32,
624 h: f32,
625 a: f32,
626}
627
628fn darken(color: Color, amount: f32) -> Color {
629 let mut oklch = to_oklch(color);
630
631 if oklch.c > 0.0 && oklch.c < (1.0 - oklch.l) / 2.0 {
633 oklch.c *= 1.0 + (0.2 / oklch.c).min(100.0) * amount;
635 }
636
637 oklch.l = if oklch.l - amount < 0.0 {
638 0.0
639 } else {
640 oklch.l - amount
641 };
642
643 from_oklch(oklch)
644}
645
646fn lighten(color: Color, amount: f32) -> Color {
647 let mut oklch = to_oklch(color);
648
649 oklch.c *= 1.0 + 2.0 * amount / oklch.l.max(0.05);
652
653 oklch.l = if oklch.l + amount > 1.0 {
654 1.0
655 } else {
656 oklch.l + amount
657 };
658
659 from_oklch(oklch)
660}
661
662fn deviate(color: Color, amount: f32) -> Color {
663 if is_dark(color) {
664 lighten(color, amount)
665 } else {
666 darken(color, amount)
667 }
668}
669
670fn mix(a: Color, b: Color, factor: f32) -> Color {
671 let b_amount = factor.clamp(0.0, 1.0);
672 let a_amount = 1.0 - b_amount;
673
674 let a_linear = a.into_linear().map(|c| c * a_amount);
675 let b_linear = b.into_linear().map(|c| c * b_amount);
676
677 Color::from_linear_rgba(
678 a_linear[0] + b_linear[0],
679 a_linear[1] + b_linear[1],
680 a_linear[2] + b_linear[2],
681 a_linear[3] + b_linear[3],
682 )
683}
684
685fn readable(background: Color, text: Color) -> Color {
686 if is_readable(background, text) {
687 return text;
688 }
689
690 let improve = if is_dark(background) { lighten } else { darken };
691
692 let candidate = improve(text, 0.1);
694
695 if is_readable(background, candidate) {
696 return candidate;
697 }
698
699 let candidate = improve(text, 0.2);
700
701 if is_readable(background, candidate) {
702 return candidate;
703 }
704
705 let white_contrast = relative_contrast(background, Color::WHITE);
706 let black_contrast = relative_contrast(background, Color::BLACK);
707
708 if white_contrast >= black_contrast {
709 mix(Color::WHITE, background, 0.05)
710 } else {
711 mix(Color::BLACK, background, 0.05)
712 }
713}
714
715fn is_dark(color: Color) -> bool {
716 to_oklch(color).l < 0.6
717}
718
719fn is_readable(a: Color, b: Color) -> bool {
720 relative_contrast(a, b) >= 6.0
721}
722
723fn relative_contrast(a: Color, b: Color) -> f32 {
725 let lum_a = a.relative_luminance();
726 let lum_b = b.relative_luminance();
727 (lum_a.max(lum_b) + 0.05) / (lum_a.min(lum_b) + 0.05)
728}
729
730fn to_oklch(color: Color) -> Oklch {
732 let [r, g, b, alpha] = color.into_linear();
733
734 let l = 0.41222146 * r + 0.53633255 * g + 0.051445995 * b;
736 let m = 0.2119035 * r + 0.6806995 * g + 0.10739696 * b;
737 let s = 0.08830246 * r + 0.28171885 * g + 0.6299787 * b;
738
739 let l_ = l.cbrt();
741 let m_ = m.cbrt();
742 let s_ = s.cbrt();
743
744 let l = 0.21045426 * l_ + 0.7936178 * m_ - 0.004072047 * s_;
746 let a = 1.9779985 * l_ - 2.4285922 * m_ + 0.4505937 * s_;
747 let b = 0.025904037 * l_ + 0.78277177 * m_ - 0.80867577 * s_;
748
749 let c = (a * a + b * b).sqrt();
751 let h = b.atan2(a); Oklch { l, c, h, a: alpha }
754}
755
756fn from_oklch(oklch: Oklch) -> Color {
758 let Oklch { l, c, h, a: alpha } = oklch;
759
760 let a = c * h.cos();
761 let b = c * h.sin();
762
763 let l_ = l + 0.39633778 * a + 0.21580376 * b;
765 let m_ = l - 0.105561346 * a - 0.06385417 * b;
766 let s_ = l - 0.08948418 * a - 1.2914855 * b;
767
768 let l = l_ * l_ * l_;
770 let m = m_ * m_ * m_;
771 let s = s_ * s_ * s_;
772
773 let r = 4.0767417 * l - 3.3077116 * m + 0.23096994 * s;
774 let g = -1.268438 * l + 2.6097574 * m - 0.34131938 * s;
775 let b = -0.0041960863 * l - 0.7034186 * m + 1.7076147 * s;
776
777 Color::from_linear_rgba(
778 r.clamp(0.0, 1.0),
779 g.clamp(0.0, 1.0),
780 b.clamp(0.0, 1.0),
781 alpha,
782 )
783}