diff options
author | 2024-03-08 14:00:28 +0100 | |
---|---|---|
committer | 2024-03-08 14:00:28 +0100 | |
commit | edf7d7ca7593f660f4b15f154257471c26df87de (patch) | |
tree | 7cee3cbfbeb2ae5145f1bf6087b61fce4cbed8c9 /core/src | |
parent | 2074757cdc65ec16eeb1c7a12a5ff3bb5ed00859 (diff) | |
parent | 8919f2593e39f76b273513e959fa6d5ffb78fde2 (diff) | |
download | iced-edf7d7ca7593f660f4b15f154257471c26df87de.tar.gz iced-edf7d7ca7593f660f4b15f154257471c26df87de.tar.bz2 iced-edf7d7ca7593f660f4b15f154257471c26df87de.zip |
Merge pull request #2312 from iced-rs/theming-reloaded
Theming reloaded
Diffstat (limited to 'core/src')
-rw-r--r-- | core/src/background.rs | 13 | ||||
-rw-r--r-- | core/src/border.rs | 35 | ||||
-rw-r--r-- | core/src/color.rs | 14 | ||||
-rw-r--r-- | core/src/gradient.rs | 22 | ||||
-rw-r--r-- | core/src/lib.rs | 2 | ||||
-rw-r--r-- | core/src/theme.rs | 221 | ||||
-rw-r--r-- | core/src/theme/palette.rs | 625 | ||||
-rw-r--r-- | core/src/widget/text.rs | 80 |
8 files changed, 965 insertions, 47 deletions
diff --git a/core/src/background.rs b/core/src/background.rs index 347c52c0..eb4b5021 100644 --- a/core/src/background.rs +++ b/core/src/background.rs @@ -11,6 +11,19 @@ pub enum Background { // TODO: Add image variant } +impl Background { + /// Scales the the alpha channel of the [`Background`] by the given + /// factor. + pub fn scale_alpha(self, factor: f32) -> Self { + match self { + Self::Color(color) => Self::Color(color.scale_alpha(factor)), + Self::Gradient(gradient) => { + Self::Gradient(gradient.scale_alpha(factor)) + } + } + } +} + impl From<Color> for Background { fn from(color: Color) -> Self { Background::Color(color) diff --git a/core/src/border.rs b/core/src/border.rs index 64262471..2df24988 100644 --- a/core/src/border.rs +++ b/core/src/border.rs @@ -1,5 +1,5 @@ //! Draw lines around containers. -use crate::Color; +use crate::{Color, Pixels}; /// A border. #[derive(Debug, Clone, Copy, PartialEq, Default)] @@ -15,11 +15,38 @@ pub struct Border { } impl Border { - /// Creates a new default [`Border`] with the given [`Radius`]. - pub fn with_radius(radius: impl Into<Radius>) -> Self { + /// Creates a new default rounded [`Border`] with the given [`Radius`]. + /// + /// ``` + /// # use iced_core::Border; + /// # + /// assert_eq!(Border::rounded(10), Border::default().with_radius(10)); + /// ``` + pub fn rounded(radius: impl Into<Radius>) -> Self { + Self::default().with_radius(radius) + } + + /// Updates the [`Color`] of the [`Border`]. + pub fn with_color(self, color: impl Into<Color>) -> Self { + Self { + color: color.into(), + ..self + } + } + + /// Updates the [`Radius`] of the [`Border`]. + pub fn with_radius(self, radius: impl Into<Radius>) -> Self { Self { radius: radius.into(), - ..Self::default() + ..self + } + } + + /// Updates the width of the [`Border`]. + pub fn with_width(self, width: impl Into<Pixels>) -> Self { + Self { + width: width.into().0, + ..self } } } diff --git a/core/src/color.rs b/core/src/color.rs index b8db322f..4e79defb 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -1,4 +1,3 @@ -#[cfg(feature = "palette")] use palette::rgb::{Srgb, Srgba}; /// A color in the `sRGB` color space. @@ -151,6 +150,14 @@ impl Color { pub fn inverse(self) -> Color { Color::new(1.0f32 - self.r, 1.0f32 - self.g, 1.0f32 - self.b, self.a) } + + /// Scales the alpha channel of the [`Color`] by the given factor. + pub fn scale_alpha(self, factor: f32) -> Color { + Self { + a: self.a * factor, + ..self + } + } } impl From<[f32; 3]> for Color { @@ -202,7 +209,6 @@ macro_rules! color { }}; } -#[cfg(feature = "palette")] /// Converts from palette's `Rgba` type to a [`Color`]. impl From<Srgba> for Color { fn from(rgba: Srgba) -> Self { @@ -210,7 +216,6 @@ impl From<Srgba> for Color { } } -#[cfg(feature = "palette")] /// Converts from [`Color`] to palette's `Rgba` type. impl From<Color> for Srgba { fn from(c: Color) -> Self { @@ -218,7 +223,6 @@ impl From<Color> for Srgba { } } -#[cfg(feature = "palette")] /// Converts from palette's `Rgb` type to a [`Color`]. impl From<Srgb> for Color { fn from(rgb: Srgb) -> Self { @@ -226,7 +230,6 @@ impl From<Srgb> for Color { } } -#[cfg(feature = "palette")] /// Converts from [`Color`] to palette's `Rgb` type. impl From<Color> for Srgb { fn from(c: Color) -> Self { @@ -234,7 +237,6 @@ impl From<Color> for Srgb { } } -#[cfg(feature = "palette")] #[cfg(test)] mod tests { use super::*; diff --git a/core/src/gradient.rs b/core/src/gradient.rs index 4711b044..ccae0bce 100644 --- a/core/src/gradient.rs +++ b/core/src/gradient.rs @@ -12,17 +12,13 @@ pub enum Gradient { } impl Gradient { - /// Adjust the opacity of the gradient by a multiplier applied to each color stop. - pub fn mul_alpha(mut self, alpha_multiplier: f32) -> Self { - match &mut self { + /// Scales the alpha channel of the [`Gradient`] by the given factor. + pub fn scale_alpha(self, factor: f32) -> Self { + match self { Gradient::Linear(linear) => { - for stop in linear.stops.iter_mut().flatten() { - stop.color.a *= alpha_multiplier; - } + Gradient::Linear(linear.scale_alpha(factor)) } } - - self } } @@ -100,4 +96,14 @@ impl Linear { self } + + /// Scales the alpha channel of the [`Linear`] gradient by the given + /// factor. + pub fn scale_alpha(mut self, factor: f32) -> Self { + for stop in self.stops.iter_mut().flatten() { + stop.color.a *= factor; + } + + self + } } diff --git a/core/src/lib.rs b/core/src/lib.rs index 002336ee..d076413e 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -30,6 +30,7 @@ pub mod overlay; pub mod renderer; pub mod svg; pub mod text; +pub mod theme; pub mod time; pub mod touch; pub mod widget; @@ -76,6 +77,7 @@ pub use shadow::Shadow; pub use shell::Shell; pub use size::Size; pub use text::Text; +pub use theme::Theme; pub use transformation::Transformation; pub use vector::Vector; pub use widget::Widget; diff --git a/core/src/theme.rs b/core/src/theme.rs new file mode 100644 index 00000000..21ba2a37 --- /dev/null +++ b/core/src/theme.rs @@ -0,0 +1,221 @@ +//! Use the built-in theme and styles. +pub mod palette; + +pub use palette::Palette; + +use std::fmt; +use std::sync::Arc; + +/// A built-in theme. +#[derive(Debug, Clone, PartialEq, Default)] +pub enum Theme { + /// The built-in light variant. + #[default] + Light, + /// The built-in dark variant. + Dark, + /// The built-in Dracula variant. + Dracula, + /// The built-in Nord variant. + Nord, + /// The built-in Solarized Light variant. + SolarizedLight, + /// The built-in Solarized Dark variant. + SolarizedDark, + /// The built-in Gruvbox Light variant. + GruvboxLight, + /// The built-in Gruvbox Dark variant. + GruvboxDark, + /// The built-in Catppuccin Latte variant. + CatppuccinLatte, + /// The built-in Catppuccin Frappé variant. + CatppuccinFrappe, + /// The built-in Catppuccin Macchiato variant. + CatppuccinMacchiato, + /// The built-in Catppuccin Mocha variant. + CatppuccinMocha, + /// The built-in Tokyo Night variant. + TokyoNight, + /// The built-in Tokyo Night Storm variant. + TokyoNightStorm, + /// The built-in Tokyo Night Light variant. + TokyoNightLight, + /// The built-in Kanagawa Wave variant. + KanagawaWave, + /// The built-in Kanagawa Dragon variant. + KanagawaDragon, + /// The built-in Kanagawa Lotus variant. + KanagawaLotus, + /// The built-in Moonfly variant. + Moonfly, + /// The built-in Nightfly variant. + Nightfly, + /// The built-in Oxocarbon variant. + Oxocarbon, + /// A [`Theme`] that uses a [`Custom`] palette. + Custom(Arc<Custom>), +} + +impl Theme { + /// A list with all the defined themes. + pub const ALL: &'static [Self] = &[ + Self::Light, + Self::Dark, + Self::Dracula, + Self::Nord, + Self::SolarizedLight, + Self::SolarizedDark, + Self::GruvboxLight, + Self::GruvboxDark, + Self::CatppuccinLatte, + Self::CatppuccinFrappe, + Self::CatppuccinMacchiato, + Self::CatppuccinMocha, + Self::TokyoNight, + Self::TokyoNightStorm, + Self::TokyoNightLight, + Self::KanagawaWave, + Self::KanagawaDragon, + Self::KanagawaLotus, + Self::Moonfly, + Self::Nightfly, + Self::Oxocarbon, + ]; + + /// Creates a new custom [`Theme`] from the given [`Palette`]. + pub fn custom(name: String, palette: Palette) -> Self { + Self::custom_with_fn(name, palette, palette::Extended::generate) + } + + /// Creates a new custom [`Theme`] from the given [`Palette`], with + /// a custom generator of a [`palette::Extended`]. + pub fn custom_with_fn( + name: String, + palette: Palette, + generate: impl FnOnce(Palette) -> palette::Extended, + ) -> Self { + Self::Custom(Arc::new(Custom::with_fn(name, palette, generate))) + } + + /// Returns the [`Palette`] of the [`Theme`]. + pub fn palette(&self) -> Palette { + match self { + Self::Light => Palette::LIGHT, + Self::Dark => Palette::DARK, + Self::Dracula => Palette::DRACULA, + Self::Nord => Palette::NORD, + Self::SolarizedLight => Palette::SOLARIZED_LIGHT, + Self::SolarizedDark => Palette::SOLARIZED_DARK, + Self::GruvboxLight => Palette::GRUVBOX_LIGHT, + Self::GruvboxDark => Palette::GRUVBOX_DARK, + Self::CatppuccinLatte => Palette::CATPPUCCIN_LATTE, + Self::CatppuccinFrappe => Palette::CATPPUCCIN_FRAPPE, + Self::CatppuccinMacchiato => Palette::CATPPUCCIN_MACCHIATO, + Self::CatppuccinMocha => Palette::CATPPUCCIN_MOCHA, + Self::TokyoNight => Palette::TOKYO_NIGHT, + Self::TokyoNightStorm => Palette::TOKYO_NIGHT_STORM, + Self::TokyoNightLight => Palette::TOKYO_NIGHT_LIGHT, + Self::KanagawaWave => Palette::KANAGAWA_WAVE, + Self::KanagawaDragon => Palette::KANAGAWA_DRAGON, + Self::KanagawaLotus => Palette::KANAGAWA_LOTUS, + Self::Moonfly => Palette::MOONFLY, + Self::Nightfly => Palette::NIGHTFLY, + Self::Oxocarbon => Palette::OXOCARBON, + Self::Custom(custom) => custom.palette, + } + } + + /// Returns the [`palette::Extended`] of the [`Theme`]. + pub fn extended_palette(&self) -> &palette::Extended { + match self { + Self::Light => &palette::EXTENDED_LIGHT, + Self::Dark => &palette::EXTENDED_DARK, + Self::Dracula => &palette::EXTENDED_DRACULA, + Self::Nord => &palette::EXTENDED_NORD, + Self::SolarizedLight => &palette::EXTENDED_SOLARIZED_LIGHT, + Self::SolarizedDark => &palette::EXTENDED_SOLARIZED_DARK, + Self::GruvboxLight => &palette::EXTENDED_GRUVBOX_LIGHT, + Self::GruvboxDark => &palette::EXTENDED_GRUVBOX_DARK, + Self::CatppuccinLatte => &palette::EXTENDED_CATPPUCCIN_LATTE, + Self::CatppuccinFrappe => &palette::EXTENDED_CATPPUCCIN_FRAPPE, + Self::CatppuccinMacchiato => { + &palette::EXTENDED_CATPPUCCIN_MACCHIATO + } + Self::CatppuccinMocha => &palette::EXTENDED_CATPPUCCIN_MOCHA, + Self::TokyoNight => &palette::EXTENDED_TOKYO_NIGHT, + Self::TokyoNightStorm => &palette::EXTENDED_TOKYO_NIGHT_STORM, + Self::TokyoNightLight => &palette::EXTENDED_TOKYO_NIGHT_LIGHT, + Self::KanagawaWave => &palette::EXTENDED_KANAGAWA_WAVE, + Self::KanagawaDragon => &palette::EXTENDED_KANAGAWA_DRAGON, + Self::KanagawaLotus => &palette::EXTENDED_KANAGAWA_LOTUS, + Self::Moonfly => &palette::EXTENDED_MOONFLY, + Self::Nightfly => &palette::EXTENDED_NIGHTFLY, + Self::Oxocarbon => &palette::EXTENDED_OXOCARBON, + Self::Custom(custom) => &custom.extended, + } + } +} + +impl fmt::Display for Theme { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Light => write!(f, "Light"), + Self::Dark => write!(f, "Dark"), + Self::Dracula => write!(f, "Dracula"), + Self::Nord => write!(f, "Nord"), + Self::SolarizedLight => write!(f, "Solarized Light"), + Self::SolarizedDark => write!(f, "Solarized Dark"), + Self::GruvboxLight => write!(f, "Gruvbox Light"), + Self::GruvboxDark => write!(f, "Gruvbox Dark"), + Self::CatppuccinLatte => write!(f, "Catppuccin Latte"), + Self::CatppuccinFrappe => write!(f, "Catppuccin Frappé"), + Self::CatppuccinMacchiato => write!(f, "Catppuccin Macchiato"), + Self::CatppuccinMocha => write!(f, "Catppuccin Mocha"), + Self::TokyoNight => write!(f, "Tokyo Night"), + Self::TokyoNightStorm => write!(f, "Tokyo Night Storm"), + Self::TokyoNightLight => write!(f, "Tokyo Night Light"), + Self::KanagawaWave => write!(f, "Kanagawa Wave"), + Self::KanagawaDragon => write!(f, "Kanagawa Dragon"), + Self::KanagawaLotus => write!(f, "Kanagawa Lotus"), + Self::Moonfly => write!(f, "Moonfly"), + Self::Nightfly => write!(f, "Nightfly"), + Self::Oxocarbon => write!(f, "Oxocarbon"), + Self::Custom(custom) => custom.fmt(f), + } + } +} + +/// A [`Theme`] with a customized [`Palette`]. +#[derive(Debug, Clone, PartialEq)] +pub struct Custom { + name: String, + palette: Palette, + extended: palette::Extended, +} + +impl Custom { + /// Creates a [`Custom`] theme from the given [`Palette`]. + pub fn new(name: String, palette: Palette) -> Self { + Self::with_fn(name, palette, palette::Extended::generate) + } + + /// Creates a [`Custom`] theme from the given [`Palette`] with + /// a custom generator of a [`palette::Extended`]. + pub fn with_fn( + name: String, + palette: Palette, + generate: impl FnOnce(Palette) -> palette::Extended, + ) -> Self { + Self { + name, + palette, + extended: generate(palette), + } + } +} + +impl fmt::Display for Custom { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.name) + } +} diff --git a/core/src/theme/palette.rs b/core/src/theme/palette.rs new file mode 100644 index 00000000..985a54a8 --- /dev/null +++ b/core/src/theme/palette.rs @@ -0,0 +1,625 @@ +//! Define the colors of a theme. +use crate::{color, Color}; + +use once_cell::sync::Lazy; +use palette::color_difference::Wcag21RelativeContrast; +use palette::rgb::Rgb; +use palette::{FromColor, Hsl, Mix}; + +/// A color palette. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Palette { + /// The background [`Color`] of the [`Palette`]. + pub background: Color, + /// The text [`Color`] of the [`Palette`]. + pub text: Color, + /// The primary [`Color`] of the [`Palette`]. + pub primary: Color, + /// The success [`Color`] of the [`Palette`]. + pub success: Color, + /// The danger [`Color`] of the [`Palette`]. + pub danger: Color, +} + +impl Palette { + /// The built-in light variant of a [`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, + ), + }; + + /// The built-in dark variant of a [`Palette`]. + 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, + ), + }; + + /// The built-in [Dracula] variant of a [`Palette`]. + /// + /// [Dracula]: https://draculatheme.com + pub const DRACULA: Self = Self { + background: color!(0x282A36), // BACKGROUND + text: color!(0xf8f8f2), // FOREGROUND + primary: color!(0xbd93f9), // PURPLE + success: color!(0x50fa7b), // GREEN + danger: color!(0xff5555), // RED + }; + + /// The built-in [Nord] variant of a [`Palette`]. + /// + /// [Nord]: https://www.nordtheme.com/docs/colors-and-palettes + pub const NORD: Self = Self { + background: color!(0x2e3440), // nord0 + text: color!(0xeceff4), // nord6 + primary: color!(0x8fbcbb), // nord7 + success: color!(0xa3be8c), // nord14 + danger: color!(0xbf616a), // nord11 + }; + + /// The built-in [Solarized] Light variant of a [`Palette`]. + /// + /// [Solarized]: https://ethanschoonover.com/solarized + pub const SOLARIZED_LIGHT: Self = Self { + background: color!(0xfdf6e3), // base3 + text: color!(0x657b83), // base00 + primary: color!(0x2aa198), // cyan + success: color!(0x859900), // green + danger: color!(0xdc322f), // red + }; + + /// The built-in [Solarized] Dark variant of a [`Palette`]. + /// + /// [Solarized]: https://ethanschoonover.com/solarized + pub const SOLARIZED_DARK: Self = Self { + background: color!(0x002b36), // base03 + text: color!(0x839496), // base0 + primary: color!(0x2aa198), // cyan + success: color!(0x859900), // green + danger: color!(0xdc322f), // red + }; + + /// The built-in [Gruvbox] Light variant of a [`Palette`]. + /// + /// [Gruvbox]: https://github.com/morhetz/gruvbox + pub const GRUVBOX_LIGHT: Self = Self { + background: color!(0xfbf1c7), // light BG_0 + text: color!(0x282828), // light FG0_29 + primary: color!(0x458588), // light BLUE_4 + success: color!(0x98971a), // light GREEN_2 + danger: color!(0xcc241d), // light RED_1 + }; + + /// The built-in [Gruvbox] Dark variant of a [`Palette`]. + /// + /// [Gruvbox]: https://github.com/morhetz/gruvbox + pub const GRUVBOX_DARK: Self = Self { + background: color!(0x282828), // dark BG_0 + text: color!(0xfbf1c7), // dark FG0_29 + primary: color!(0x458588), // dark BLUE_4 + success: color!(0x98971a), // dark GREEN_2 + danger: color!(0xcc241d), // dark RED_1 + }; + + /// The built-in [Catppuccin] Latte variant of a [`Palette`]. + /// + /// [Catppuccin]: https://github.com/catppuccin/catppuccin + pub const CATPPUCCIN_LATTE: Self = Self { + background: color!(0xeff1f5), // Base + text: color!(0x4c4f69), // Text + primary: color!(0x1e66f5), // Blue + success: color!(0x40a02b), // Green + danger: color!(0xd20f39), // Red + }; + + /// The built-in [Catppuccin] Frappé variant of a [`Palette`]. + /// + /// [Catppuccin]: https://github.com/catppuccin/catppuccin + pub const CATPPUCCIN_FRAPPE: Self = Self { + background: color!(0x303446), // Base + text: color!(0xc6d0f5), // Text + primary: color!(0x8caaee), // Blue + success: color!(0xa6d189), // Green + danger: color!(0xe78284), // Red + }; + + /// The built-in [Catppuccin] Macchiato variant of a [`Palette`]. + /// + /// [Catppuccin]: https://github.com/catppuccin/catppuccin + pub const CATPPUCCIN_MACCHIATO: Self = Self { + background: color!(0x24273a), // Base + text: color!(0xcad3f5), // Text + primary: color!(0x8aadf4), // Blue + success: color!(0xa6da95), // Green + danger: color!(0xed8796), // Red + }; + + /// The built-in [Catppuccin] Mocha variant of a [`Palette`]. + /// + /// [Catppuccin]: https://github.com/catppuccin/catppuccin + pub const CATPPUCCIN_MOCHA: Self = Self { + background: color!(0x1e1e2e), // Base + text: color!(0xcdd6f4), // Text + primary: color!(0x89b4fa), // Blue + success: color!(0xa6e3a1), // Green + danger: color!(0xf38ba8), // Red + }; + + /// The built-in [Tokyo Night] variant of a [`Palette`]. + /// + /// [Tokyo Night]: https://github.com/enkia/tokyo-night-vscode-theme + pub const TOKYO_NIGHT: Self = Self { + background: color!(0x1a1b26), // Background (Night) + text: color!(0x9aa5ce), // Text + primary: color!(0x2ac3de), // Blue + success: color!(0x9ece6a), // Green + danger: color!(0xf7768e), // Red + }; + + /// The built-in [Tokyo Night] Storm variant of a [`Palette`]. + /// + /// [Tokyo Night]: https://github.com/enkia/tokyo-night-vscode-theme + pub const TOKYO_NIGHT_STORM: Self = Self { + background: color!(0x24283b), // Background (Storm) + text: color!(0x9aa5ce), // Text + primary: color!(0x2ac3de), // Blue + success: color!(0x9ece6a), // Green + danger: color!(0xf7768e), // Red + }; + + /// The built-in [Tokyo Night] Light variant of a [`Palette`]. + /// + /// [Tokyo Night]: https://github.com/enkia/tokyo-night-vscode-theme + pub const TOKYO_NIGHT_LIGHT: Self = Self { + background: color!(0xd5d6db), // Background + text: color!(0x565a6e), // Text + primary: color!(0x166775), // Blue + success: color!(0x485e30), // Green + danger: color!(0x8c4351), // Red + }; + + /// The built-in [Kanagawa] Wave variant of a [`Palette`]. + /// + /// [Kanagawa]: https://github.com/rebelot/kanagawa.nvim + pub const KANAGAWA_WAVE: Self = Self { + background: color!(0x363646), // Sumi Ink 3 + text: color!(0xCD7BA), // Fuji White + primary: color!(0x2D4F67), // Wave Blue 2 + success: color!(0x76946A), // Autumn Green + danger: color!(0xC34043), // Autumn Red + }; + + /// The built-in [Kanagawa] Dragon variant of a [`Palette`]. + /// + /// [Kanagawa]: https://github.com/rebelot/kanagawa.nvim + pub const KANAGAWA_DRAGON: Self = Self { + background: color!(0x181616), // Dragon Black 3 + text: color!(0xc5c9c5), // Dragon White + primary: color!(0x223249), // Wave Blue 1 + success: color!(0x8a9a7b), // Dragon Green 2 + danger: color!(0xc4746e), // Dragon Red + }; + + /// The built-in [Kanagawa] Lotus variant of a [`Palette`]. + /// + /// [Kanagawa]: https://github.com/rebelot/kanagawa.nvim + pub const KANAGAWA_LOTUS: Self = Self { + background: color!(0xf2ecbc), // Lotus White 3 + text: color!(0x545464), // Lotus Ink 1 + primary: color!(0xc9cbd1), // Lotus Violet 3 + success: color!(0x6f894e), // Lotus Green + danger: color!(0xc84053), // Lotus Red + }; + + /// The built-in [Moonfly] variant of a [`Palette`]. + /// + /// [Moonfly]: https://github.com/bluz71/vim-moonfly-colors + pub const MOONFLY: Self = Self { + background: color!(0x080808), // Background + text: color!(0xbdbdbd), // Foreground + primary: color!(0x80a0ff), // Blue (normal) + success: color!(0x8cc85f), // Green (normal) + danger: color!(0xff5454), // Red (normal) + }; + + /// The built-in [Nightfly] variant of a [`Palette`]. + /// + /// [Nightfly]: https://github.com/bluz71/vim-nightfly-colors + pub const NIGHTFLY: Self = Self { + background: color!(0x011627), // Background + text: color!(0xbdc1c6), // Foreground + primary: color!(0x82aaff), // Blue (normal) + success: color!(0xa1cd5e), // Green (normal) + danger: color!(0xfc514e), // Red (normal) + }; + + /// The built-in [Oxocarbon] variant of a [`Palette`]. + /// + /// [Oxocarbon]: https://github.com/nyoom-engineering/oxocarbon.nvim + pub const OXOCARBON: Self = Self { + background: color!(0x232323), + text: color!(0xd0d0d0), + primary: color!(0x00b4ff), + success: color!(0x00c15a), + danger: color!(0xf62d0f), + }; +} + +/// An extended set of colors generated from a [`Palette`]. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Extended { + /// The set of background colors. + pub background: Background, + /// The set of primary colors. + pub primary: Primary, + /// The set of secondary colors. + pub secondary: Secondary, + /// The set of success colors. + pub success: Success, + /// The set of danger colors. + pub danger: Danger, + /// Whether the palette is dark or not. + pub is_dark: bool, +} + +/// The built-in light variant of an [`Extended`] palette. +pub static EXTENDED_LIGHT: Lazy<Extended> = + Lazy::new(|| Extended::generate(Palette::LIGHT)); + +/// The built-in dark variant of an [`Extended`] palette. +pub static EXTENDED_DARK: Lazy<Extended> = + Lazy::new(|| Extended::generate(Palette::DARK)); + +/// The built-in Dracula variant of an [`Extended`] palette. +pub static EXTENDED_DRACULA: Lazy<Extended> = + Lazy::new(|| Extended::generate(Palette::DRACULA)); + +/// The built-in Nord variant of an [`Extended`] palette. +pub static EXTENDED_NORD: Lazy<Extended> = + Lazy::new(|| Extended::generate(Palette::NORD)); + +/// The built-in Solarized Light variant of an [`Extended`] palette. +pub static EXTENDED_SOLARIZED_LIGHT: Lazy<Extended> = + Lazy::new(|| Extended::generate(Palette::SOLARIZED_LIGHT)); + +/// The built-in Solarized Dark variant of an [`Extended`] palette. +pub static EXTENDED_SOLARIZED_DARK: Lazy<Extended> = + Lazy::new(|| Extended::generate(Palette::SOLARIZED_DARK)); + +/// The built-in Gruvbox Light variant of an [`Extended`] palette. +pub static EXTENDED_GRUVBOX_LIGHT: Lazy<Extended> = + Lazy::new(|| Extended::generate(Palette::GRUVBOX_LIGHT)); + +/// The built-in Gruvbox Dark variant of an [`Extended`] palette. +pub static EXTENDED_GRUVBOX_DARK: Lazy<Extended> = + Lazy::new(|| Extended::generate(Palette::GRUVBOX_DARK)); + +/// The built-in Catppuccin Latte variant of an [`Extended`] palette. +pub static EXTENDED_CATPPUCCIN_LATTE: Lazy<Extended> = + Lazy::new(|| Extended::generate(Palette::CATPPUCCIN_LATTE)); + +/// The built-in Catppuccin Frappé variant of an [`Extended`] palette. +pub static EXTENDED_CATPPUCCIN_FRAPPE: Lazy<Extended> = + Lazy::new(|| Extended::generate(Palette::CATPPUCCIN_FRAPPE)); + +/// The built-in Catppuccin Macchiato variant of an [`Extended`] palette. +pub static EXTENDED_CATPPUCCIN_MACCHIATO: Lazy<Extended> = + Lazy::new(|| Extended::generate(Palette::CATPPUCCIN_MACCHIATO)); + +/// The built-in Catppuccin Mocha variant of an [`Extended`] palette. +pub static EXTENDED_CATPPUCCIN_MOCHA: Lazy<Extended> = + Lazy::new(|| Extended::generate(Palette::CATPPUCCIN_MOCHA)); + +/// The built-in Tokyo Night variant of an [`Extended`] palette. +pub static EXTENDED_TOKYO_NIGHT: Lazy<Extended> = + Lazy::new(|| Extended::generate(Palette::TOKYO_NIGHT)); + +/// The built-in Tokyo Night Storm variant of an [`Extended`] palette. +pub static EXTENDED_TOKYO_NIGHT_STORM: Lazy<Extended> = + Lazy::new(|| Extended::generate(Palette::TOKYO_NIGHT_STORM)); + +/// The built-in Tokyo Night variant of an [`Extended`] palette. +pub static EXTENDED_TOKYO_NIGHT_LIGHT: Lazy<Extended> = + Lazy::new(|| Extended::generate(Palette::TOKYO_NIGHT_LIGHT)); + +/// The built-in Kanagawa Wave variant of an [`Extended`] palette. +pub static EXTENDED_KANAGAWA_WAVE: Lazy<Extended> = + Lazy::new(|| Extended::generate(Palette::KANAGAWA_WAVE)); + +/// The built-in Kanagawa Dragon variant of an [`Extended`] palette. +pub static EXTENDED_KANAGAWA_DRAGON: Lazy<Extended> = + Lazy::new(|| Extended::generate(Palette::KANAGAWA_DRAGON)); + +/// The built-in Kanagawa Lotus variant of an [`Extended`] palette. +pub static EXTENDED_KANAGAWA_LOTUS: Lazy<Extended> = + Lazy::new(|| Extended::generate(Palette::KANAGAWA_LOTUS)); + +/// The built-in Moonfly variant of an [`Extended`] palette. +pub static EXTENDED_MOONFLY: Lazy<Extended> = + Lazy::new(|| Extended::generate(Palette::MOONFLY)); + +/// The built-in Nightfly variant of an [`Extended`] palette. +pub static EXTENDED_NIGHTFLY: Lazy<Extended> = + Lazy::new(|| Extended::generate(Palette::NIGHTFLY)); + +/// The built-in Oxocarbon variant of an [`Extended`] palette. +pub static EXTENDED_OXOCARBON: Lazy<Extended> = + Lazy::new(|| Extended::generate(Palette::OXOCARBON)); + +impl Extended { + /// Generates an [`Extended`] palette from a simple [`Palette`]. + 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), + } + } +} + +/// A pair of background and text colors. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Pair { + /// The background color. + pub color: Color, + + /// The text color. + /// + /// It's guaranteed to be readable on top of the background [`color`]. + /// + /// [`color`]: Self::color + pub text: Color, +} + +impl Pair { + /// Creates a new [`Pair`] from a background [`Color`] and some text [`Color`]. + pub fn new(color: Color, text: Color) -> Self { + Self { + color, + text: readable(color, text), + } + } +} + +/// A set of background colors. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Background { + /// The base background color. + pub base: Pair, + /// A weaker version of the base background color. + pub weak: Pair, + /// A stronger version of the base background color. + pub strong: Pair, +} + +impl Background { + /// Generates a set of [`Background`] colors from the base and text colors. + 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), + } + } +} + +/// A set of primary colors. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Primary { + /// The base primary color. + pub base: Pair, + /// A weaker version of the base primary color. + pub weak: Pair, + /// A stronger version of the base primary color. + pub strong: Pair, +} + +impl Primary { + /// Generates a set of [`Primary`] colors from the base, background, and text colors. + 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), + } + } +} + +/// A set of secondary colors. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Secondary { + /// The base secondary color. + pub base: Pair, + /// A weaker version of the base secondary color. + pub weak: Pair, + /// A stronger version of the base secondary color. + pub strong: Pair, +} + +impl Secondary { + /// Generates a set of [`Secondary`] colors from the base and text colors. + 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), + } + } +} + +/// A set of success colors. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Success { + /// The base success color. + pub base: Pair, + /// A weaker version of the base success color. + pub weak: Pair, + /// A stronger version of the base success color. + pub strong: Pair, +} + +impl Success { + /// Generates a set of [`Success`] colors from the base, background, and text colors. + 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), + } + } +} + +/// A set of danger colors. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Danger { + /// The base danger color. + pub base: Pair, + /// A weaker version of the base danger color. + pub weak: Pair, + /// A stronger version of the base danger color. + pub strong: Pair, +} + +impl Danger { + /// Generates a set of [`Danger`] colors from the base, background, and text colors. + 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() +} diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs index 0796c4e4..a220127c 100644 --- a/core/src/widget/text.rs +++ b/core/src/widget/text.rs @@ -17,7 +17,6 @@ pub use text::{LineHeight, Shaping}; #[allow(missing_debug_implementations)] pub struct Text<'a, Theme, Renderer> where - Theme: StyleSheet, Renderer: text::Renderer, { content: Cow<'a, str>, @@ -29,12 +28,11 @@ where vertical_alignment: alignment::Vertical, font: Option<Renderer::Font>, shaping: Shaping, - style: Theme::Style, + style: Style<Theme>, } impl<'a, Theme, Renderer> Text<'a, Theme, Renderer> where - Theme: StyleSheet, Renderer: text::Renderer, { /// Create a new fragment of [`Text`] with the given contents. @@ -49,7 +47,7 @@ where horizontal_alignment: alignment::Horizontal::Left, vertical_alignment: alignment::Vertical::Top, shaping: Shaping::Basic, - style: Default::default(), + style: Style::default(), } } @@ -74,8 +72,20 @@ where } /// Sets the style of the [`Text`]. - pub fn style(mut self, style: impl Into<Theme::Style>) -> Self { - self.style = style.into(); + pub fn style(mut self, style: fn(&Theme) -> Appearance) -> Self { + self.style = Style::Themed(style); + self + } + + /// Sets the [`Color`] of the [`Text`]. + pub fn color(mut self, color: impl Into<Color>) -> Self { + self.style = Style::Colored(Some(color.into())); + self + } + + /// Sets the [`Color`] of the [`Text`], if `Some`. + pub fn color_maybe(mut self, color: Option<impl Into<Color>>) -> Self { + self.style = Style::Colored(color.map(Into::into)); self } @@ -123,7 +133,6 @@ pub struct State<P: Paragraph>(P); impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer> for Text<'a, Theme, Renderer> where - Theme: StyleSheet, Renderer: text::Renderer, { fn tag(&self) -> tree::Tag { @@ -175,14 +184,12 @@ where ) { let state = tree.state.downcast_ref::<State<Renderer::Paragraph>>(); - draw( - renderer, - style, - layout, - state, - theme.appearance(self.style.clone()), - viewport, - ); + let appearance = match self.style { + Style::Themed(f) => f(theme), + Style::Colored(color) => Appearance { color }, + }; + + draw(renderer, style, layout, state, appearance, viewport); } } @@ -273,7 +280,7 @@ pub fn draw<Renderer>( impl<'a, Message, Theme, Renderer> From<Text<'a, Theme, Renderer>> for Element<'a, Message, Theme, Renderer> where - Theme: StyleSheet + 'a, + Theme: 'a, Renderer: text::Renderer + 'a, { fn from( @@ -285,7 +292,6 @@ where impl<'a, Theme, Renderer> Clone for Text<'a, Theme, Renderer> where - Theme: StyleSheet, Renderer: text::Renderer, { fn clone(&self) -> Self { @@ -298,7 +304,7 @@ where horizontal_alignment: self.horizontal_alignment, vertical_alignment: self.vertical_alignment, font: self.font, - style: self.style.clone(), + style: self.style, shaping: self.shaping, } } @@ -306,7 +312,6 @@ where impl<'a, Theme, Renderer> From<&'a str> for Text<'a, Theme, Renderer> where - Theme: StyleSheet, Renderer: text::Renderer, { fn from(content: &'a str) -> Self { @@ -317,7 +322,7 @@ where impl<'a, Message, Theme, Renderer> From<&'a str> for Element<'a, Message, Theme, Renderer> where - Theme: StyleSheet + 'a, + Theme: 'a, Renderer: text::Renderer + 'a, { fn from(content: &'a str) -> Self { @@ -325,15 +330,6 @@ where } } -/// The style sheet of some text. -pub trait StyleSheet { - /// The supported style of the [`StyleSheet`]. - type Style: Default + Clone; - - /// Produces the [`Appearance`] of some text. - fn appearance(&self, style: Self::Style) -> Appearance; -} - /// The apperance of some text. #[derive(Debug, Clone, Copy, Default)] pub struct Appearance { @@ -342,3 +338,29 @@ pub struct Appearance { /// The default, `None`, means using the inherited color. pub color: Option<Color>, } + +#[derive(Debug)] +enum Style<Theme> { + Themed(fn(&Theme) -> Appearance), + Colored(Option<Color>), +} + +impl<Theme> Clone for Style<Theme> { + fn clone(&self) -> Self { + *self + } +} + +impl<Theme> Copy for Style<Theme> {} + +impl<Theme> Default for Style<Theme> { + fn default() -> Self { + Style::Colored(None) + } +} + +impl<Theme> From<fn(&Theme) -> Appearance> for Style<Theme> { + fn from(f: fn(&Theme) -> Appearance) -> Self { + Style::Themed(f) + } +} |