use crate::{Color, color};
use std::sync::LazyLock;
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Palette {
pub background: Color,
pub text: Color,
pub primary: Color,
pub success: Color,
pub warning: Color,
pub danger: Color,
}
impl Palette {
pub const LIGHT: Self = Self {
background: Color::WHITE,
text: Color::BLACK,
primary: color!(0x5865F2),
success: color!(0x12664f),
warning: color!(0xb77e33),
danger: color!(0xc3423f),
};
pub const DARK: Self = Self {
background: color!(0x2B2D31),
text: Color::from_rgb(0.90, 0.90, 0.90),
primary: color!(0x5865F2),
success: color!(0x12664f),
warning: color!(0xffc14e),
danger: color!(0xc3423f),
};
pub const DRACULA: Self = Self {
background: color!(0x282A36), text: color!(0xf8f8f2), primary: color!(0xbd93f9), success: color!(0x50fa7b), warning: color!(0xf1fa8c), danger: color!(0xff5555), };
pub const NORD: Self = Self {
background: color!(0x2e3440), text: color!(0xeceff4), primary: color!(0x8fbcbb), success: color!(0xa3be8c), warning: color!(0xebcb8b), danger: color!(0xbf616a), };
pub const SOLARIZED_LIGHT: Self = Self {
background: color!(0xfdf6e3), text: color!(0x657b83), primary: color!(0x2aa198), success: color!(0x859900), warning: color!(0xb58900), danger: color!(0xdc322f), };
pub const SOLARIZED_DARK: Self = Self {
background: color!(0x002b36), text: color!(0x839496), primary: color!(0x2aa198), success: color!(0x859900), warning: color!(0xb58900), danger: color!(0xdc322f), };
pub const GRUVBOX_LIGHT: Self = Self {
background: color!(0xfbf1c7), text: color!(0x282828), primary: color!(0x458588), success: color!(0x98971a), warning: color!(0xd79921), danger: color!(0xcc241d), };
pub const GRUVBOX_DARK: Self = Self {
background: color!(0x282828), text: color!(0xfbf1c7), primary: color!(0x458588), success: color!(0x98971a), warning: color!(0xd79921), danger: color!(0xcc241d), };
pub const CATPPUCCIN_LATTE: Self = Self {
background: color!(0xeff1f5), text: color!(0x4c4f69), primary: color!(0x1e66f5), success: color!(0x40a02b), warning: color!(0xdf8e1d), danger: color!(0xd20f39), };
pub const CATPPUCCIN_FRAPPE: Self = Self {
background: color!(0x303446), text: color!(0xc6d0f5), primary: color!(0x8caaee), success: color!(0xa6d189), warning: color!(0xe5c890), danger: color!(0xe78284), };
pub const CATPPUCCIN_MACCHIATO: Self = Self {
background: color!(0x24273a), text: color!(0xcad3f5), primary: color!(0x8aadf4), success: color!(0xa6da95), warning: color!(0xeed49f), danger: color!(0xed8796), };
pub const CATPPUCCIN_MOCHA: Self = Self {
background: color!(0x1e1e2e), text: color!(0xcdd6f4), primary: color!(0x89b4fa), success: color!(0xa6e3a1), warning: color!(0xf9e2af), danger: color!(0xf38ba8), };
pub const TOKYO_NIGHT: Self = Self {
background: color!(0x1a1b26), text: color!(0x9aa5ce), primary: color!(0x2ac3de), success: color!(0x9ece6a), warning: color!(0xe0af68), danger: color!(0xf7768e), };
pub const TOKYO_NIGHT_STORM: Self = Self {
background: color!(0x24283b), text: color!(0x9aa5ce), primary: color!(0x2ac3de), success: color!(0x9ece6a), warning: color!(0xe0af68), danger: color!(0xf7768e), };
pub const TOKYO_NIGHT_LIGHT: Self = Self {
background: color!(0xd5d6db), text: color!(0x565a6e), primary: color!(0x166775), success: color!(0x485e30), warning: color!(0x8f5e15), danger: color!(0x8c4351), };
pub const KANAGAWA_WAVE: Self = Self {
background: color!(0x1f1f28), text: color!(0xDCD7BA), primary: color!(0x7FB4CA), success: color!(0x76946A), warning: color!(0xff9e3b), danger: color!(0xC34043), };
pub const KANAGAWA_DRAGON: Self = Self {
background: color!(0x181616), text: color!(0xc5c9c5), primary: color!(0x223249), success: color!(0x8a9a7b), warning: color!(0xff9e3b), danger: color!(0xc4746e), };
pub const KANAGAWA_LOTUS: Self = Self {
background: color!(0xf2ecbc), text: color!(0x545464), primary: color!(0x4d699b), success: color!(0x6f894e), warning: color!(0xe98a00), danger: color!(0xc84053), };
pub const MOONFLY: Self = Self {
background: color!(0x080808), text: color!(0xbdbdbd), primary: color!(0x80a0ff), success: color!(0x8cc85f), warning: color!(0xe3c78a), danger: color!(0xff5454), };
pub const NIGHTFLY: Self = Self {
background: color!(0x011627), text: color!(0xbdc1c6), primary: color!(0x82aaff), success: color!(0xa1cd5e), warning: color!(0xe3d18a), danger: color!(0xfc514e), };
pub const OXOCARBON: Self = Self {
background: color!(0x232323),
text: color!(0xd0d0d0),
primary: color!(0x00b4ff),
success: color!(0x00c15a),
warning: color!(0xbe95ff), danger: color!(0xf62d0f),
};
pub const FERRA: Self = Self {
background: color!(0x2b292d),
text: color!(0xfecdb2),
primary: color!(0xd1d1e0),
success: color!(0xb1b695),
warning: color!(0xf5d76e), danger: color!(0xe06b75),
};
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Extended {
pub background: Background,
pub primary: Primary,
pub secondary: Secondary,
pub success: Success,
pub warning: Warning,
pub danger: Danger,
pub is_dark: bool,
}
pub static EXTENDED_LIGHT: LazyLock<Extended> =
LazyLock::new(|| Extended::generate(Palette::LIGHT));
pub static EXTENDED_DARK: LazyLock<Extended> =
LazyLock::new(|| Extended::generate(Palette::DARK));
pub static EXTENDED_DRACULA: LazyLock<Extended> =
LazyLock::new(|| Extended::generate(Palette::DRACULA));
pub static EXTENDED_NORD: LazyLock<Extended> =
LazyLock::new(|| Extended::generate(Palette::NORD));
pub static EXTENDED_SOLARIZED_LIGHT: LazyLock<Extended> =
LazyLock::new(|| Extended::generate(Palette::SOLARIZED_LIGHT));
pub static EXTENDED_SOLARIZED_DARK: LazyLock<Extended> =
LazyLock::new(|| Extended::generate(Palette::SOLARIZED_DARK));
pub static EXTENDED_GRUVBOX_LIGHT: LazyLock<Extended> =
LazyLock::new(|| Extended::generate(Palette::GRUVBOX_LIGHT));
pub static EXTENDED_GRUVBOX_DARK: LazyLock<Extended> =
LazyLock::new(|| Extended::generate(Palette::GRUVBOX_DARK));
pub static EXTENDED_CATPPUCCIN_LATTE: LazyLock<Extended> =
LazyLock::new(|| Extended::generate(Palette::CATPPUCCIN_LATTE));
pub static EXTENDED_CATPPUCCIN_FRAPPE: LazyLock<Extended> =
LazyLock::new(|| Extended::generate(Palette::CATPPUCCIN_FRAPPE));
pub static EXTENDED_CATPPUCCIN_MACCHIATO: LazyLock<Extended> =
LazyLock::new(|| Extended::generate(Palette::CATPPUCCIN_MACCHIATO));
pub static EXTENDED_CATPPUCCIN_MOCHA: LazyLock<Extended> =
LazyLock::new(|| Extended::generate(Palette::CATPPUCCIN_MOCHA));
pub static EXTENDED_TOKYO_NIGHT: LazyLock<Extended> =
LazyLock::new(|| Extended::generate(Palette::TOKYO_NIGHT));
pub static EXTENDED_TOKYO_NIGHT_STORM: LazyLock<Extended> =
LazyLock::new(|| Extended::generate(Palette::TOKYO_NIGHT_STORM));
pub static EXTENDED_TOKYO_NIGHT_LIGHT: LazyLock<Extended> =
LazyLock::new(|| Extended::generate(Palette::TOKYO_NIGHT_LIGHT));
pub static EXTENDED_KANAGAWA_WAVE: LazyLock<Extended> =
LazyLock::new(|| Extended::generate(Palette::KANAGAWA_WAVE));
pub static EXTENDED_KANAGAWA_DRAGON: LazyLock<Extended> =
LazyLock::new(|| Extended::generate(Palette::KANAGAWA_DRAGON));
pub static EXTENDED_KANAGAWA_LOTUS: LazyLock<Extended> =
LazyLock::new(|| Extended::generate(Palette::KANAGAWA_LOTUS));
pub static EXTENDED_MOONFLY: LazyLock<Extended> =
LazyLock::new(|| Extended::generate(Palette::MOONFLY));
pub static EXTENDED_NIGHTFLY: LazyLock<Extended> =
LazyLock::new(|| Extended::generate(Palette::NIGHTFLY));
pub static EXTENDED_OXOCARBON: LazyLock<Extended> =
LazyLock::new(|| Extended::generate(Palette::OXOCARBON));
pub static EXTENDED_FERRA: LazyLock<Extended> =
LazyLock::new(|| Extended::generate(Palette::FERRA));
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,
),
warning: Warning::generate(
palette.warning,
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 weakest: Pair,
pub weaker: Pair,
pub weak: Pair,
pub neutral: Pair,
pub strong: Pair,
pub stronger: Pair,
pub strongest: Pair,
}
impl Background {
pub fn new(base: Color, text: Color) -> Self {
let weakest = deviate(base, 0.03);
let weaker = deviate(base, 0.07);
let weak = deviate(base, 0.1);
let neutral = deviate(base, 0.125);
let strong = deviate(base, 0.15);
let stronger = deviate(base, 0.175);
let strongest = deviate(base, 0.20);
Self {
base: Pair::new(base, text),
weakest: Pair::new(weakest, text),
weaker: Pair::new(weaker, text),
weak: Pair::new(weak, text),
neutral: Pair::new(neutral, text),
strong: Pair::new(strong, text),
stronger: Pair::new(stronger, text),
strongest: Pair::new(strongest, 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 factor = if is_dark(base) { 0.2 } else { 0.4 };
let weak = mix(deviate(base, 0.1), text, factor);
let strong = mix(deviate(base, 0.3), text, factor);
let base = mix(deviate(base, 0.2), text, factor);
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 Warning {
pub base: Pair,
pub weak: Pair,
pub strong: Pair,
}
impl Warning {
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),
}
}
}
struct Oklch {
l: f32,
c: f32,
h: f32,
a: f32,
}
pub fn darken(color: Color, amount: f32) -> Color {
let mut oklch = to_oklch(color);
if oklch.c > 0.0 && oklch.c < (1.0 - oklch.l) / 2.0 {
oklch.c *= 1.0 + (0.2 / oklch.c).min(100.0) * amount;
}
oklch.l = if oklch.l - amount < 0.0 {
0.0
} else {
oklch.l - amount
};
from_oklch(oklch)
}
pub fn lighten(color: Color, amount: f32) -> Color {
let mut oklch = to_oklch(color);
oklch.c *= 1.0 + 2.0 * amount / oklch.l.max(0.05);
oklch.l = if oklch.l + amount > 1.0 {
1.0
} else {
oklch.l + amount
};
from_oklch(oklch)
}
pub fn deviate(color: Color, amount: f32) -> Color {
if is_dark(color) {
lighten(color, amount)
} else {
darken(color, amount)
}
}
pub fn mix(a: Color, b: Color, factor: f32) -> Color {
let b_amount = factor.clamp(0.0, 1.0);
let a_amount = 1.0 - b_amount;
let a_linear = a.into_linear().map(|c| c * a_amount);
let b_linear = b.into_linear().map(|c| c * b_amount);
Color::from_linear_rgba(
a_linear[0] + b_linear[0],
a_linear[1] + b_linear[1],
a_linear[2] + b_linear[2],
a_linear[3] + b_linear[3],
)
}
pub fn readable(background: Color, text: Color) -> Color {
if text.is_readable_on(background) {
return text;
}
let improve = if is_dark(background) { lighten } else { darken };
let candidate = improve(text, 0.1);
if candidate.is_readable_on(background) {
return candidate;
}
let candidate = improve(text, 0.2);
if candidate.is_readable_on(background) {
return candidate;
}
let white_contrast = background.relative_contrast(Color::WHITE);
let black_contrast = background.relative_contrast(Color::BLACK);
if white_contrast >= black_contrast {
mix(Color::WHITE, background, 0.05)
} else {
mix(Color::BLACK, background, 0.05)
}
}
pub fn is_dark(color: Color) -> bool {
to_oklch(color).l < 0.6
}
fn to_oklch(color: Color) -> Oklch {
let [r, g, b, alpha] = color.into_linear();
let l = 0.41222146 * r + 0.53633255 * g + 0.051445995 * b;
let m = 0.2119035 * r + 0.6806995 * g + 0.10739696 * b;
let s = 0.08830246 * r + 0.28171885 * g + 0.6299787 * b;
let l_ = l.cbrt();
let m_ = m.cbrt();
let s_ = s.cbrt();
let l = 0.21045426 * l_ + 0.7936178 * m_ - 0.004072047 * s_;
let a = 1.9779985 * l_ - 2.4285922 * m_ + 0.4505937 * s_;
let b = 0.025904037 * l_ + 0.78277177 * m_ - 0.80867577 * s_;
let c = (a * a + b * b).sqrt();
let h = b.atan2(a);
Oklch { l, c, h, a: alpha }
}
fn from_oklch(oklch: Oklch) -> Color {
let Oklch { l, c, h, a: alpha } = oklch;
let a = c * h.cos();
let b = c * h.sin();
let l_ = l + 0.39633778 * a + 0.21580376 * b;
let m_ = l - 0.105561346 * a - 0.06385417 * b;
let s_ = l - 0.08948418 * a - 1.2914855 * b;
let l = l_ * l_ * l_;
let m = m_ * m_ * m_;
let s = s_ * s_ * s_;
let r = 4.0767417 * l - 3.3077116 * m + 0.23096994 * s;
let g = -1.268438 * l + 2.6097574 * m - 0.34131938 * s;
let b = -0.0041960863 * l - 0.7034186 * m + 1.7076147 * s;
Color::from_linear_rgba(
r.clamp(0.0, 1.0),
g.clamp(0.0, 1.0),
b.clamp(0.0, 1.0),
alpha,
)
}