1use crate::{color, Color};
3
4use once_cell::sync::Lazy;
5use palette::color_difference::Wcag21RelativeContrast;
6use palette::rgb::Rgb;
7use palette::{FromColor, Hsl, Mix};
8
9#[derive(Debug, Clone, Copy, PartialEq)]
11pub struct Palette {
12 pub background: Color,
14 pub text: Color,
16 pub primary: Color,
18 pub success: 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::from_rgb(
30 0x5E as f32 / 255.0,
31 0x7C as f32 / 255.0,
32 0xE2 as f32 / 255.0,
33 ),
34 success: Color::from_rgb(
35 0x12 as f32 / 255.0,
36 0x66 as f32 / 255.0,
37 0x4F as f32 / 255.0,
38 ),
39 danger: Color::from_rgb(
40 0xC3 as f32 / 255.0,
41 0x42 as f32 / 255.0,
42 0x3F as f32 / 255.0,
43 ),
44 };
45
46 pub const DARK: Self = Self {
48 background: Color::from_rgb(
49 0x20 as f32 / 255.0,
50 0x22 as f32 / 255.0,
51 0x25 as f32 / 255.0,
52 ),
53 text: Color::from_rgb(0.90, 0.90, 0.90),
54 primary: Color::from_rgb(
55 0x5E as f32 / 255.0,
56 0x7C as f32 / 255.0,
57 0xE2 as f32 / 255.0,
58 ),
59 success: Color::from_rgb(
60 0x12 as f32 / 255.0,
61 0x66 as f32 / 255.0,
62 0x4F as f32 / 255.0,
63 ),
64 danger: Color::from_rgb(
65 0xC3 as f32 / 255.0,
66 0x42 as f32 / 255.0,
67 0x3F as f32 / 255.0,
68 ),
69 };
70
71 pub const DRACULA: Self = Self {
75 background: color!(0x282A36), text: color!(0xf8f8f2), primary: color!(0xbd93f9), success: color!(0x50fa7b), danger: color!(0xff5555), };
81
82 pub const NORD: Self = Self {
86 background: color!(0x2e3440), text: color!(0xeceff4), primary: color!(0x8fbcbb), success: color!(0xa3be8c), danger: color!(0xbf616a), };
92
93 pub const SOLARIZED_LIGHT: Self = Self {
97 background: color!(0xfdf6e3), text: color!(0x657b83), primary: color!(0x2aa198), success: color!(0x859900), danger: color!(0xdc322f), };
103
104 pub const SOLARIZED_DARK: Self = Self {
108 background: color!(0x002b36), text: color!(0x839496), primary: color!(0x2aa198), success: color!(0x859900), danger: color!(0xdc322f), };
114
115 pub const GRUVBOX_LIGHT: Self = Self {
119 background: color!(0xfbf1c7), text: color!(0x282828), primary: color!(0x458588), success: color!(0x98971a), danger: color!(0xcc241d), };
125
126 pub const GRUVBOX_DARK: Self = Self {
130 background: color!(0x282828), text: color!(0xfbf1c7), primary: color!(0x458588), success: color!(0x98971a), danger: color!(0xcc241d), };
136
137 pub const CATPPUCCIN_LATTE: Self = Self {
141 background: color!(0xeff1f5), text: color!(0x4c4f69), primary: color!(0x1e66f5), success: color!(0x40a02b), danger: color!(0xd20f39), };
147
148 pub const CATPPUCCIN_FRAPPE: Self = Self {
152 background: color!(0x303446), text: color!(0xc6d0f5), primary: color!(0x8caaee), success: color!(0xa6d189), danger: color!(0xe78284), };
158
159 pub const CATPPUCCIN_MACCHIATO: Self = Self {
163 background: color!(0x24273a), text: color!(0xcad3f5), primary: color!(0x8aadf4), success: color!(0xa6da95), danger: color!(0xed8796), };
169
170 pub const CATPPUCCIN_MOCHA: Self = Self {
174 background: color!(0x1e1e2e), text: color!(0xcdd6f4), primary: color!(0x89b4fa), success: color!(0xa6e3a1), danger: color!(0xf38ba8), };
180
181 pub const TOKYO_NIGHT: Self = Self {
185 background: color!(0x1a1b26), text: color!(0x9aa5ce), primary: color!(0x2ac3de), success: color!(0x9ece6a), danger: color!(0xf7768e), };
191
192 pub const TOKYO_NIGHT_STORM: Self = Self {
196 background: color!(0x24283b), text: color!(0x9aa5ce), primary: color!(0x2ac3de), success: color!(0x9ece6a), danger: color!(0xf7768e), };
202
203 pub const TOKYO_NIGHT_LIGHT: Self = Self {
207 background: color!(0xd5d6db), text: color!(0x565a6e), primary: color!(0x166775), success: color!(0x485e30), danger: color!(0x8c4351), };
213
214 pub const KANAGAWA_WAVE: Self = Self {
218 background: color!(0x363646), text: color!(0xCD7BA), primary: color!(0x2D4F67), success: color!(0x76946A), danger: color!(0xC34043), };
224
225 pub const KANAGAWA_DRAGON: Self = Self {
229 background: color!(0x181616), text: color!(0xc5c9c5), primary: color!(0x223249), success: color!(0x8a9a7b), danger: color!(0xc4746e), };
235
236 pub const KANAGAWA_LOTUS: Self = Self {
240 background: color!(0xf2ecbc), text: color!(0x545464), primary: color!(0xc9cbd1), success: color!(0x6f894e), danger: color!(0xc84053), };
246
247 pub const MOONFLY: Self = Self {
251 background: color!(0x080808), text: color!(0xbdbdbd), primary: color!(0x80a0ff), success: color!(0x8cc85f), danger: color!(0xff5454), };
257
258 pub const NIGHTFLY: Self = Self {
262 background: color!(0x011627), text: color!(0xbdc1c6), primary: color!(0x82aaff), success: color!(0xa1cd5e), danger: color!(0xfc514e), };
268
269 pub const OXOCARBON: Self = Self {
273 background: color!(0x232323),
274 text: color!(0xd0d0d0),
275 primary: color!(0x00b4ff),
276 success: color!(0x00c15a),
277 danger: color!(0xf62d0f),
278 };
279
280 pub const FERRA: Self = Self {
284 background: color!(0x2b292d),
285 text: color!(0xfecdb2),
286 primary: color!(0xd1d1e0),
287 success: color!(0xb1b695),
288 danger: color!(0xe06b75),
289 };
290}
291
292#[derive(Debug, Clone, Copy, PartialEq)]
294pub struct Extended {
295 pub background: Background,
297 pub primary: Primary,
299 pub secondary: Secondary,
301 pub success: Success,
303 pub danger: Danger,
305 pub is_dark: bool,
307}
308
309pub static EXTENDED_LIGHT: Lazy<Extended> =
311 Lazy::new(|| Extended::generate(Palette::LIGHT));
312
313pub static EXTENDED_DARK: Lazy<Extended> =
315 Lazy::new(|| Extended::generate(Palette::DARK));
316
317pub static EXTENDED_DRACULA: Lazy<Extended> =
319 Lazy::new(|| Extended::generate(Palette::DRACULA));
320
321pub static EXTENDED_NORD: Lazy<Extended> =
323 Lazy::new(|| Extended::generate(Palette::NORD));
324
325pub static EXTENDED_SOLARIZED_LIGHT: Lazy<Extended> =
327 Lazy::new(|| Extended::generate(Palette::SOLARIZED_LIGHT));
328
329pub static EXTENDED_SOLARIZED_DARK: Lazy<Extended> =
331 Lazy::new(|| Extended::generate(Palette::SOLARIZED_DARK));
332
333pub static EXTENDED_GRUVBOX_LIGHT: Lazy<Extended> =
335 Lazy::new(|| Extended::generate(Palette::GRUVBOX_LIGHT));
336
337pub static EXTENDED_GRUVBOX_DARK: Lazy<Extended> =
339 Lazy::new(|| Extended::generate(Palette::GRUVBOX_DARK));
340
341pub static EXTENDED_CATPPUCCIN_LATTE: Lazy<Extended> =
343 Lazy::new(|| Extended::generate(Palette::CATPPUCCIN_LATTE));
344
345pub static EXTENDED_CATPPUCCIN_FRAPPE: Lazy<Extended> =
347 Lazy::new(|| Extended::generate(Palette::CATPPUCCIN_FRAPPE));
348
349pub static EXTENDED_CATPPUCCIN_MACCHIATO: Lazy<Extended> =
351 Lazy::new(|| Extended::generate(Palette::CATPPUCCIN_MACCHIATO));
352
353pub static EXTENDED_CATPPUCCIN_MOCHA: Lazy<Extended> =
355 Lazy::new(|| Extended::generate(Palette::CATPPUCCIN_MOCHA));
356
357pub static EXTENDED_TOKYO_NIGHT: Lazy<Extended> =
359 Lazy::new(|| Extended::generate(Palette::TOKYO_NIGHT));
360
361pub static EXTENDED_TOKYO_NIGHT_STORM: Lazy<Extended> =
363 Lazy::new(|| Extended::generate(Palette::TOKYO_NIGHT_STORM));
364
365pub static EXTENDED_TOKYO_NIGHT_LIGHT: Lazy<Extended> =
367 Lazy::new(|| Extended::generate(Palette::TOKYO_NIGHT_LIGHT));
368
369pub static EXTENDED_KANAGAWA_WAVE: Lazy<Extended> =
371 Lazy::new(|| Extended::generate(Palette::KANAGAWA_WAVE));
372
373pub static EXTENDED_KANAGAWA_DRAGON: Lazy<Extended> =
375 Lazy::new(|| Extended::generate(Palette::KANAGAWA_DRAGON));
376
377pub static EXTENDED_KANAGAWA_LOTUS: Lazy<Extended> =
379 Lazy::new(|| Extended::generate(Palette::KANAGAWA_LOTUS));
380
381pub static EXTENDED_MOONFLY: Lazy<Extended> =
383 Lazy::new(|| Extended::generate(Palette::MOONFLY));
384
385pub static EXTENDED_NIGHTFLY: Lazy<Extended> =
387 Lazy::new(|| Extended::generate(Palette::NIGHTFLY));
388
389pub static EXTENDED_OXOCARBON: Lazy<Extended> =
391 Lazy::new(|| Extended::generate(Palette::OXOCARBON));
392
393pub static EXTENDED_FERRA: Lazy<Extended> =
395 Lazy::new(|| Extended::generate(Palette::FERRA));
396
397impl Extended {
398 pub fn generate(palette: Palette) -> Self {
400 Self {
401 background: Background::new(palette.background, palette.text),
402 primary: Primary::generate(
403 palette.primary,
404 palette.background,
405 palette.text,
406 ),
407 secondary: Secondary::generate(palette.background, palette.text),
408 success: Success::generate(
409 palette.success,
410 palette.background,
411 palette.text,
412 ),
413 danger: Danger::generate(
414 palette.danger,
415 palette.background,
416 palette.text,
417 ),
418 is_dark: is_dark(palette.background),
419 }
420 }
421}
422
423#[derive(Debug, Clone, Copy, PartialEq)]
425pub struct Pair {
426 pub color: Color,
428
429 pub icon: Color,
431
432 pub text: Color,
438}
439
440impl Pair {
441 pub fn new(color: Color, text: Color) -> Self {
443 let text = readable(color, text);
444
445 Self {
446 color,
447 icon: text,
448 text,
449 }
450 }
451}
452
453#[derive(Debug, Clone, Copy, PartialEq)]
455pub struct Background {
456 pub base: Pair,
458 pub weak: Pair,
460 pub strong: Pair,
462}
463
464impl Background {
465 pub fn new(base: Color, text: Color) -> Self {
467 let weak = mix(base, text, 0.15);
468 let strong = mix(base, text, 0.40);
469
470 Self {
471 base: Pair::new(base, text),
472 weak: Pair::new(weak, text),
473 strong: Pair::new(strong, text),
474 }
475 }
476}
477
478#[derive(Debug, Clone, Copy, PartialEq)]
480pub struct Primary {
481 pub base: Pair,
483 pub weak: Pair,
485 pub strong: Pair,
487}
488
489impl Primary {
490 pub fn generate(base: Color, background: Color, text: Color) -> Self {
492 let weak = mix(base, background, 0.4);
493 let strong = deviate(base, 0.1);
494
495 Self {
496 base: Pair::new(base, text),
497 weak: Pair::new(weak, text),
498 strong: Pair::new(strong, text),
499 }
500 }
501}
502
503#[derive(Debug, Clone, Copy, PartialEq)]
505pub struct Secondary {
506 pub base: Pair,
508 pub weak: Pair,
510 pub strong: Pair,
512}
513
514impl Secondary {
515 pub fn generate(base: Color, text: Color) -> Self {
517 let base = mix(base, text, 0.2);
518 let weak = mix(base, text, 0.1);
519 let strong = mix(base, text, 0.3);
520
521 Self {
522 base: Pair::new(base, text),
523 weak: Pair::new(weak, text),
524 strong: Pair::new(strong, text),
525 }
526 }
527}
528
529#[derive(Debug, Clone, Copy, PartialEq)]
531pub struct Success {
532 pub base: Pair,
534 pub weak: Pair,
536 pub strong: Pair,
538}
539
540impl Success {
541 pub fn generate(base: Color, background: Color, text: Color) -> Self {
543 let weak = mix(base, background, 0.4);
544 let strong = deviate(base, 0.1);
545
546 Self {
547 base: Pair::new(base, text),
548 weak: Pair::new(weak, text),
549 strong: Pair::new(strong, text),
550 }
551 }
552}
553
554#[derive(Debug, Clone, Copy, PartialEq)]
556pub struct Danger {
557 pub base: Pair,
559 pub weak: Pair,
561 pub strong: Pair,
563}
564
565impl Danger {
566 pub fn generate(base: Color, background: Color, text: Color) -> Self {
568 let weak = mix(base, background, 0.4);
569 let strong = deviate(base, 0.1);
570
571 Self {
572 base: Pair::new(base, text),
573 weak: Pair::new(weak, text),
574 strong: Pair::new(strong, text),
575 }
576 }
577}
578
579fn darken(color: Color, amount: f32) -> Color {
580 let mut hsl = to_hsl(color);
581
582 hsl.lightness = if hsl.lightness - amount < 0.0 {
583 0.0
584 } else {
585 hsl.lightness - amount
586 };
587
588 from_hsl(hsl)
589}
590
591fn lighten(color: Color, amount: f32) -> Color {
592 let mut hsl = to_hsl(color);
593
594 hsl.lightness = if hsl.lightness + amount > 1.0 {
595 1.0
596 } else {
597 hsl.lightness + amount
598 };
599
600 from_hsl(hsl)
601}
602
603fn deviate(color: Color, amount: f32) -> Color {
604 if is_dark(color) {
605 lighten(color, amount)
606 } else {
607 darken(color, amount)
608 }
609}
610
611fn mix(a: Color, b: Color, factor: f32) -> Color {
612 let a_lin = Rgb::from(a).into_linear();
613 let b_lin = Rgb::from(b).into_linear();
614
615 let mixed = a_lin.mix(b_lin, factor);
616 Rgb::from_linear(mixed).into()
617}
618
619fn readable(background: Color, text: Color) -> Color {
620 if is_readable(background, text) {
621 text
622 } else {
623 let white_contrast = relative_contrast(background, Color::WHITE);
624 let black_contrast = relative_contrast(background, Color::BLACK);
625
626 if white_contrast >= black_contrast {
627 Color::WHITE
628 } else {
629 Color::BLACK
630 }
631 }
632}
633
634fn is_dark(color: Color) -> bool {
635 to_hsl(color).lightness < 0.6
636}
637
638fn is_readable(a: Color, b: Color) -> bool {
639 let a_srgb = Rgb::from(a);
640 let b_srgb = Rgb::from(b);
641
642 a_srgb.has_enhanced_contrast_text(b_srgb)
643}
644
645fn relative_contrast(a: Color, b: Color) -> f32 {
646 let a_srgb = Rgb::from(a);
647 let b_srgb = Rgb::from(b);
648
649 a_srgb.relative_contrast(b_srgb)
650}
651
652fn to_hsl(color: Color) -> Hsl {
653 Hsl::from_color(Rgb::from(color))
654}
655
656fn from_hsl(hsl: Hsl) -> Color {
657 Rgb::from_color(hsl).into()
658}