use crate::core::{color, Color};
use once_cell::sync::Lazy;
use palette::color_difference::Wcag21RelativeContrast;
use palette::rgb::Rgb;
use palette::{FromColor, Hsl, Mix};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Palette {
pub background: Color,
pub text: Color,
pub primary: Color,
pub success: Color,
pub danger: Color,
}
impl Palette {
pub const LIGHT: Self = Self {
background: Color::WHITE,
text: Color::BLACK,
primary: Color::from_rgb(
0x5E as f32 / 255.0,
0x7C as f32 / 255.0,
0xE2 as f32 / 255.0,
),
success: Color::from_rgb(
0x12 as f32 / 255.0,
0x66 as f32 / 255.0,
0x4F as f32 / 255.0,
),
danger: Color::from_rgb(
0xC3 as f32 / 255.0,
0x42 as f32 / 255.0,
0x3F as f32 / 255.0,
),
};
pub const DARK: Self = Self {
background: Color::from_rgb(
0x20 as f32 / 255.0,
0x22 as f32 / 255.0,
0x25 as f32 / 255.0,
),
text: Color::from_rgb(0.90, 0.90, 0.90),
primary: Color::from_rgb(
0x5E as f32 / 255.0,
0x7C as f32 / 255.0,
0xE2 as f32 / 255.0,
),
success: Color::from_rgb(
0x12 as f32 / 255.0,
0x66 as f32 / 255.0,
0x4F as f32 / 255.0,
),
danger: Color::from_rgb(
0xC3 as f32 / 255.0,
0x42 as f32 / 255.0,
0x3F as f32 / 255.0,
),
};
pub const DRACULA: Self = Self {
background: color!(0x282A36), text: color!(0xf8f8f2), primary: color!(0xbd93f9), success: color!(0x50fa7b), danger: color!(0xff5555), };
pub const NORD: Self = Self {
background: color!(0x2e3440), text: color!(0xeceff4), primary: color!(0x8fbcbb), success: color!(0xa3be8c), danger: color!(0xbf616a), };
pub const SOLARIZED_LIGHT: Self = Self {
background: color!(0xfdf6e3), text: color!(0x657b83), primary: color!(0x2aa198), success: color!(0x859900), danger: color!(0xdc322f), };
pub const SOLARIZED_DARK: Self = Self {
background: color!(0x002b36), text: color!(0x839496), primary: color!(0x2aa198), success: color!(0x859900), danger: color!(0xdc322f), };
pub const GRUVBOX_LIGHT: Self = Self {
background: color!(0xfbf1c7), text: color!(0x282828), primary: color!(0x458588), success: color!(0x98971a), danger: color!(0xcc241d), };
pub const GRUVBOX_DARK: Self = Self {
background: color!(0x282828), text: color!(0xfbf1c7), primary: color!(0x458588), success: color!(0x98971a), danger: color!(0xcc241d), };
pub const CATPPUCCIN_LATTE: Self = Self {
background: color!(0xeff1f5), text: color!(0x4c4f69), primary: color!(0x1e66f5), success: color!(0x40a02b), danger: color!(0xd20f39), };
pub const CATPPUCCIN_FRAPPE: Self = Self {
background: color!(0x303446), text: color!(0xc6d0f5), primary: color!(0x8caaee), success: color!(0xa6d189), danger: color!(0xe78284), };
pub const CATPPUCCIN_MACCHIATO: Self = Self {
background: color!(0x24273a), text: color!(0xcad3f5), primary: color!(0x8aadf4), success: color!(0xa6da95), danger: color!(0xed8796), };
pub const CATPPUCCIN_MOCHA: Self = Self {
background: color!(0x1e1e2e), text: color!(0xcdd6f4), primary: color!(0x89b4fa), success: color!(0xa6e3a1), danger: color!(0xf38ba8), };
pub const TOKYO_NIGHT: Self = Self {
background: color!(0x1a1b26), text: color!(0x9aa5ce), primary: color!(0x2ac3de), success: color!(0x9ece6a), danger: color!(0xf7768e), };
pub const TOKYO_NIGHT_STORM: Self = Self {
background: color!(0x24283b), text: color!(0x9aa5ce), primary: color!(0x2ac3de), success: color!(0x9ece6a), danger: color!(0xf7768e), };
pub const TOKYO_NIGHT_LIGHT: Self = Self {
background: color!(0xd5d6db), text: color!(0x565a6e), primary: color!(0x166775), success: color!(0x485e30), danger: color!(0x8c4351), };
pub const KANAGAWA_WAVE: Self = Self {
background: color!(0x363646), text: color!(0xCD7BA), primary: color!(0x2D4F67), success: color!(0x76946A), danger: color!(0xC34043), };
pub const KANAGAWA_DRAGON: Self = Self {
background: color!(0x181616), text: color!(0xc5c9c5), primary: color!(0x223249), success: color!(0x8a9a7b), danger: color!(0xc4746e), };
pub const KANAGAWA_LOTUS: Self = Self {
background: color!(0xf2ecbc), text: color!(0x545464), primary: color!(0xc9cbd1), success: color!(0x6f894e), danger: color!(0xc84053), };
pub const MOONFLY: Self = Self {
background: color!(0x080808), text: color!(0xbdbdbd), primary: color!(0x80a0ff), success: color!(0x8cc85f), danger: color!(0xff5454), };
pub const NIGHTFLY: Self = Self {
background: color!(0x011627), text: color!(0xbdc1c6), primary: color!(0x82aaff), success: color!(0xa1cd5e), danger: color!(0xfc514e), };
pub const OXOCARBON: Self = Self {
background: color!(0x232323),
text: color!(0xd0d0d0),
primary: color!(0x00b4ff),
success: color!(0x00c15a),
danger: color!(0xf62d0f),
};
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Extended {
pub background: Background,
pub primary: Primary,
pub secondary: Secondary,
pub success: Success,
pub danger: Danger,
pub is_dark: bool,
}
pub static EXTENDED_LIGHT: Lazy<Extended> =
Lazy::new(|| Extended::generate(Palette::LIGHT));
pub static EXTENDED_DARK: Lazy<Extended> =
Lazy::new(|| Extended::generate(Palette::DARK));
pub static EXTENDED_DRACULA: Lazy<Extended> =
Lazy::new(|| Extended::generate(Palette::DRACULA));
pub static EXTENDED_NORD: Lazy<Extended> =
Lazy::new(|| Extended::generate(Palette::NORD));
pub static EXTENDED_SOLARIZED_LIGHT: Lazy<Extended> =
Lazy::new(|| Extended::generate(Palette::SOLARIZED_LIGHT));
pub static EXTENDED_SOLARIZED_DARK: Lazy<Extended> =
Lazy::new(|| Extended::generate(Palette::SOLARIZED_DARK));
pub static EXTENDED_GRUVBOX_LIGHT: Lazy<Extended> =
Lazy::new(|| Extended::generate(Palette::GRUVBOX_LIGHT));
pub static EXTENDED_GRUVBOX_DARK: Lazy<Extended> =
Lazy::new(|| Extended::generate(Palette::GRUVBOX_DARK));
pub static EXTENDED_CATPPUCCIN_LATTE: Lazy<Extended> =
Lazy::new(|| Extended::generate(Palette::CATPPUCCIN_LATTE));
pub static EXTENDED_CATPPUCCIN_FRAPPE: Lazy<Extended> =
Lazy::new(|| Extended::generate(Palette::CATPPUCCIN_FRAPPE));
pub static EXTENDED_CATPPUCCIN_MACCHIATO: Lazy<Extended> =
Lazy::new(|| Extended::generate(Palette::CATPPUCCIN_MACCHIATO));
pub static EXTENDED_CATPPUCCIN_MOCHA: Lazy<Extended> =
Lazy::new(|| Extended::generate(Palette::CATPPUCCIN_MOCHA));
pub static EXTENDED_TOKYO_NIGHT: Lazy<Extended> =
Lazy::new(|| Extended::generate(Palette::TOKYO_NIGHT));
pub static EXTENDED_TOKYO_NIGHT_STORM: Lazy<Extended> =
Lazy::new(|| Extended::generate(Palette::TOKYO_NIGHT_STORM));
pub static EXTENDED_TOKYO_NIGHT_LIGHT: Lazy<Extended> =
Lazy::new(|| Extended::generate(Palette::TOKYO_NIGHT_LIGHT));
pub static EXTENDED_KANAGAWA_WAVE: Lazy<Extended> =
Lazy::new(|| Extended::generate(Palette::KANAGAWA_WAVE));
pub static EXTENDED_KANAGAWA_DRAGON: Lazy<Extended> =
Lazy::new(|| Extended::generate(Palette::KANAGAWA_DRAGON));
pub static EXTENDED_KANAGAWA_LOTUS: Lazy<Extended> =
Lazy::new(|| Extended::generate(Palette::KANAGAWA_LOTUS));
pub static EXTENDED_MOONFLY: Lazy<Extended> =
Lazy::new(|| Extended::generate(Palette::MOONFLY));
pub static EXTENDED_NIGHTFLY: Lazy<Extended> =
Lazy::new(|| Extended::generate(Palette::NIGHTFLY));
pub static EXTENDED_OXOCARBON: Lazy<Extended> =
Lazy::new(|| Extended::generate(Palette::OXOCARBON));
impl Extended {
pub fn generate(palette: Palette) -> Self {
Self {
background: Background::new(palette.background, palette.text),
primary: Primary::generate(
palette.primary,
palette.background,
palette.text,
),
secondary: Secondary::generate(palette.background, palette.text),
success: Success::generate(
palette.success,
palette.background,
palette.text,
),
danger: Danger::generate(
palette.danger,
palette.background,
palette.text,
),
is_dark: is_dark(palette.background),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Pair {
pub color: Color,
pub text: Color,
}
impl Pair {
pub fn new(color: Color, text: Color) -> Self {
Self {
color,
text: readable(color, text),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Background {
pub base: Pair,
pub weak: Pair,
pub strong: Pair,
}
impl Background {
pub fn new(base: Color, text: Color) -> Self {
let weak = mix(base, text, 0.15);
let strong = mix(base, text, 0.40);
Self {
base: Pair::new(base, text),
weak: Pair::new(weak, text),
strong: Pair::new(strong, text),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Primary {
pub base: Pair,
pub weak: Pair,
pub strong: Pair,
}
impl Primary {
pub fn generate(base: Color, background: Color, text: Color) -> Self {
let weak = mix(base, background, 0.4);
let strong = deviate(base, 0.1);
Self {
base: Pair::new(base, text),
weak: Pair::new(weak, text),
strong: Pair::new(strong, text),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Secondary {
pub base: Pair,
pub weak: Pair,
pub strong: Pair,
}
impl Secondary {
pub fn generate(base: Color, text: Color) -> Self {
let base = mix(base, text, 0.2);
let weak = mix(base, text, 0.1);
let strong = mix(base, text, 0.3);
Self {
base: Pair::new(base, text),
weak: Pair::new(weak, text),
strong: Pair::new(strong, text),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Success {
pub base: Pair,
pub weak: Pair,
pub strong: Pair,
}
impl Success {
pub fn generate(base: Color, background: Color, text: Color) -> Self {
let weak = mix(base, background, 0.4);
let strong = deviate(base, 0.1);
Self {
base: Pair::new(base, text),
weak: Pair::new(weak, text),
strong: Pair::new(strong, text),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Danger {
pub base: Pair,
pub weak: Pair,
pub strong: Pair,
}
impl Danger {
pub fn generate(base: Color, background: Color, text: Color) -> Self {
let weak = mix(base, background, 0.4);
let strong = deviate(base, 0.1);
Self {
base: Pair::new(base, text),
weak: Pair::new(weak, text),
strong: Pair::new(strong, text),
}
}
}
fn darken(color: Color, amount: f32) -> Color {
let mut hsl = to_hsl(color);
hsl.lightness = if hsl.lightness - amount < 0.0 {
0.0
} else {
hsl.lightness - amount
};
from_hsl(hsl)
}
fn lighten(color: Color, amount: f32) -> Color {
let mut hsl = to_hsl(color);
hsl.lightness = if hsl.lightness + amount > 1.0 {
1.0
} else {
hsl.lightness + amount
};
from_hsl(hsl)
}
fn deviate(color: Color, amount: f32) -> Color {
if is_dark(color) {
lighten(color, amount)
} else {
darken(color, amount)
}
}
fn mix(a: Color, b: Color, factor: f32) -> Color {
let a_lin = Rgb::from(a).into_linear();
let b_lin = Rgb::from(b).into_linear();
let mixed = a_lin.mix(b_lin, factor);
Rgb::from_linear(mixed).into()
}
fn readable(background: Color, text: Color) -> Color {
if is_readable(background, text) {
text
} else if is_dark(background) {
Color::WHITE
} else {
Color::BLACK
}
}
fn is_dark(color: Color) -> bool {
to_hsl(color).lightness < 0.6
}
fn is_readable(a: Color, b: Color) -> bool {
let a_srgb = Rgb::from(a);
let b_srgb = Rgb::from(b);
a_srgb.has_enhanced_contrast_text(b_srgb)
}
fn to_hsl(color: Color) -> Hsl {
Hsl::from_color(Rgb::from(color))
}
fn from_hsl(hsl: Hsl) -> Color {
Rgb::from_color(hsl).into()
}