1use crate::core::{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
281#[derive(Debug, Clone, Copy, PartialEq)]
283pub struct Extended {
284 pub background: Background,
286 pub primary: Primary,
288 pub secondary: Secondary,
290 pub success: Success,
292 pub danger: Danger,
294 pub is_dark: bool,
296}
297
298pub static EXTENDED_LIGHT: Lazy<Extended> =
300 Lazy::new(|| Extended::generate(Palette::LIGHT));
301
302pub static EXTENDED_DARK: Lazy<Extended> =
304 Lazy::new(|| Extended::generate(Palette::DARK));
305
306pub static EXTENDED_DRACULA: Lazy<Extended> =
308 Lazy::new(|| Extended::generate(Palette::DRACULA));
309
310pub static EXTENDED_NORD: Lazy<Extended> =
312 Lazy::new(|| Extended::generate(Palette::NORD));
313
314pub static EXTENDED_SOLARIZED_LIGHT: Lazy<Extended> =
316 Lazy::new(|| Extended::generate(Palette::SOLARIZED_LIGHT));
317
318pub static EXTENDED_SOLARIZED_DARK: Lazy<Extended> =
320 Lazy::new(|| Extended::generate(Palette::SOLARIZED_DARK));
321
322pub static EXTENDED_GRUVBOX_LIGHT: Lazy<Extended> =
324 Lazy::new(|| Extended::generate(Palette::GRUVBOX_LIGHT));
325
326pub static EXTENDED_GRUVBOX_DARK: Lazy<Extended> =
328 Lazy::new(|| Extended::generate(Palette::GRUVBOX_DARK));
329
330pub static EXTENDED_CATPPUCCIN_LATTE: Lazy<Extended> =
332 Lazy::new(|| Extended::generate(Palette::CATPPUCCIN_LATTE));
333
334pub static EXTENDED_CATPPUCCIN_FRAPPE: Lazy<Extended> =
336 Lazy::new(|| Extended::generate(Palette::CATPPUCCIN_FRAPPE));
337
338pub static EXTENDED_CATPPUCCIN_MACCHIATO: Lazy<Extended> =
340 Lazy::new(|| Extended::generate(Palette::CATPPUCCIN_MACCHIATO));
341
342pub static EXTENDED_CATPPUCCIN_MOCHA: Lazy<Extended> =
344 Lazy::new(|| Extended::generate(Palette::CATPPUCCIN_MOCHA));
345
346pub static EXTENDED_TOKYO_NIGHT: Lazy<Extended> =
348 Lazy::new(|| Extended::generate(Palette::TOKYO_NIGHT));
349
350pub static EXTENDED_TOKYO_NIGHT_STORM: Lazy<Extended> =
352 Lazy::new(|| Extended::generate(Palette::TOKYO_NIGHT_STORM));
353
354pub static EXTENDED_TOKYO_NIGHT_LIGHT: Lazy<Extended> =
356 Lazy::new(|| Extended::generate(Palette::TOKYO_NIGHT_LIGHT));
357
358pub static EXTENDED_KANAGAWA_WAVE: Lazy<Extended> =
360 Lazy::new(|| Extended::generate(Palette::KANAGAWA_WAVE));
361
362pub static EXTENDED_KANAGAWA_DRAGON: Lazy<Extended> =
364 Lazy::new(|| Extended::generate(Palette::KANAGAWA_DRAGON));
365
366pub static EXTENDED_KANAGAWA_LOTUS: Lazy<Extended> =
368 Lazy::new(|| Extended::generate(Palette::KANAGAWA_LOTUS));
369
370pub static EXTENDED_MOONFLY: Lazy<Extended> =
372 Lazy::new(|| Extended::generate(Palette::MOONFLY));
373
374pub static EXTENDED_NIGHTFLY: Lazy<Extended> =
376 Lazy::new(|| Extended::generate(Palette::NIGHTFLY));
377
378pub static EXTENDED_OXOCARBON: Lazy<Extended> =
380 Lazy::new(|| Extended::generate(Palette::OXOCARBON));
381
382impl Extended {
383 pub fn generate(palette: Palette) -> Self {
385 Self {
386 background: Background::new(palette.background, palette.text),
387 primary: Primary::generate(
388 palette.primary,
389 palette.background,
390 palette.text,
391 ),
392 secondary: Secondary::generate(palette.background, palette.text),
393 success: Success::generate(
394 palette.success,
395 palette.background,
396 palette.text,
397 ),
398 danger: Danger::generate(
399 palette.danger,
400 palette.background,
401 palette.text,
402 ),
403 is_dark: is_dark(palette.background),
404 }
405 }
406}
407
408#[derive(Debug, Clone, Copy, PartialEq)]
410pub struct Pair {
411 pub color: Color,
413
414 pub text: Color,
420}
421
422impl Pair {
423 pub fn new(color: Color, text: Color) -> Self {
425 Self {
426 color,
427 text: readable(color, text),
428 }
429 }
430}
431
432#[derive(Debug, Clone, Copy, PartialEq)]
434pub struct Background {
435 pub base: Pair,
437 pub weak: Pair,
439 pub strong: Pair,
441}
442
443impl Background {
444 pub fn new(base: Color, text: Color) -> Self {
446 let weak = mix(base, text, 0.15);
447 let strong = mix(base, text, 0.40);
448
449 Self {
450 base: Pair::new(base, text),
451 weak: Pair::new(weak, text),
452 strong: Pair::new(strong, text),
453 }
454 }
455}
456
457#[derive(Debug, Clone, Copy, PartialEq)]
459pub struct Primary {
460 pub base: Pair,
462 pub weak: Pair,
464 pub strong: Pair,
466}
467
468impl Primary {
469 pub fn generate(base: Color, background: Color, text: Color) -> Self {
471 let weak = mix(base, background, 0.4);
472 let strong = deviate(base, 0.1);
473
474 Self {
475 base: Pair::new(base, text),
476 weak: Pair::new(weak, text),
477 strong: Pair::new(strong, text),
478 }
479 }
480}
481
482#[derive(Debug, Clone, Copy, PartialEq)]
484pub struct Secondary {
485 pub base: Pair,
487 pub weak: Pair,
489 pub strong: Pair,
491}
492
493impl Secondary {
494 pub fn generate(base: Color, text: Color) -> Self {
496 let base = mix(base, text, 0.2);
497 let weak = mix(base, text, 0.1);
498 let strong = mix(base, text, 0.3);
499
500 Self {
501 base: Pair::new(base, text),
502 weak: Pair::new(weak, text),
503 strong: Pair::new(strong, text),
504 }
505 }
506}
507
508#[derive(Debug, Clone, Copy, PartialEq)]
510pub struct Success {
511 pub base: Pair,
513 pub weak: Pair,
515 pub strong: Pair,
517}
518
519impl Success {
520 pub fn generate(base: Color, background: Color, text: Color) -> Self {
522 let weak = mix(base, background, 0.4);
523 let strong = deviate(base, 0.1);
524
525 Self {
526 base: Pair::new(base, text),
527 weak: Pair::new(weak, text),
528 strong: Pair::new(strong, text),
529 }
530 }
531}
532
533#[derive(Debug, Clone, Copy, PartialEq)]
535pub struct Danger {
536 pub base: Pair,
538 pub weak: Pair,
540 pub strong: Pair,
542}
543
544impl Danger {
545 pub fn generate(base: Color, background: Color, text: Color) -> Self {
547 let weak = mix(base, background, 0.4);
548 let strong = deviate(base, 0.1);
549
550 Self {
551 base: Pair::new(base, text),
552 weak: Pair::new(weak, text),
553 strong: Pair::new(strong, text),
554 }
555 }
556}
557
558fn darken(color: Color, amount: f32) -> Color {
559 let mut hsl = to_hsl(color);
560
561 hsl.lightness = if hsl.lightness - amount < 0.0 {
562 0.0
563 } else {
564 hsl.lightness - amount
565 };
566
567 from_hsl(hsl)
568}
569
570fn lighten(color: Color, amount: f32) -> Color {
571 let mut hsl = to_hsl(color);
572
573 hsl.lightness = if hsl.lightness + amount > 1.0 {
574 1.0
575 } else {
576 hsl.lightness + amount
577 };
578
579 from_hsl(hsl)
580}
581
582fn deviate(color: Color, amount: f32) -> Color {
583 if is_dark(color) {
584 lighten(color, amount)
585 } else {
586 darken(color, amount)
587 }
588}
589
590fn mix(a: Color, b: Color, factor: f32) -> Color {
591 let a_lin = Rgb::from(a).into_linear();
592 let b_lin = Rgb::from(b).into_linear();
593
594 let mixed = a_lin.mix(b_lin, factor);
595 Rgb::from_linear(mixed).into()
596}
597
598fn readable(background: Color, text: Color) -> Color {
599 if is_readable(background, text) {
600 text
601 } else if is_dark(background) {
602 Color::WHITE
603 } else {
604 Color::BLACK
605 }
606}
607
608fn is_dark(color: Color) -> bool {
609 to_hsl(color).lightness < 0.6
610}
611
612fn is_readable(a: Color, b: Color) -> bool {
613 let a_srgb = Rgb::from(a);
614 let b_srgb = Rgb::from(b);
615
616 a_srgb.has_enhanced_contrast_text(b_srgb)
617}
618
619fn to_hsl(color: Color) -> Hsl {
620 Hsl::from_color(Rgb::from(color))
621}
622
623fn from_hsl(hsl: Hsl) -> Color {
624 Rgb::from_color(hsl).into()
625}