Color Schemes (#142)

* WIP: Color Schemes

* Import/export using color_schemes config

* Finish color scheme implementation

* Add color scheme rename
This commit is contained in:
Jeremy Soller 2024-02-22 09:55:46 -07:00 committed by GitHub
parent 01052fae0b
commit 89e1dcb83a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
28 changed files with 2630 additions and 861 deletions

View file

@ -5,6 +5,7 @@ use cosmic::{
theme,
};
use cosmic_text::{Metrics, Stretch, Weight};
use hex_color::HexColor;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
@ -31,6 +32,139 @@ impl AppTheme {
}
}
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(transparent)]
pub struct ColorSchemeId(pub u64);
//TODO: there is a lot of extra code to keep the exported color scheme clean,
//consider how to reduce this
fn de_color_opt<'de, D>(deserializer: D) -> Result<Option<HexColor>, D::Error>
where
D: serde::Deserializer<'de>,
{
let hex_color: HexColor = Deserialize::deserialize(deserializer)?;
Ok(Some(hex_color))
}
fn ser_color_opt<S>(hex_color_opt: &Option<HexColor>, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
use serde::ser::Error as _;
match hex_color_opt {
Some(hex_color) => Serialize::serialize(hex_color, serializer),
None => Err(S::Error::custom("ser_color_opt called with None")),
}
}
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(default, deny_unknown_fields)]
pub struct ColorSchemeAnsi {
#[serde(
deserialize_with = "de_color_opt",
serialize_with = "ser_color_opt",
skip_serializing_if = "Option::is_none"
)]
pub black: Option<HexColor>,
#[serde(
deserialize_with = "de_color_opt",
serialize_with = "ser_color_opt",
skip_serializing_if = "Option::is_none"
)]
pub red: Option<HexColor>,
#[serde(
deserialize_with = "de_color_opt",
serialize_with = "ser_color_opt",
skip_serializing_if = "Option::is_none"
)]
pub green: Option<HexColor>,
#[serde(
deserialize_with = "de_color_opt",
serialize_with = "ser_color_opt",
skip_serializing_if = "Option::is_none"
)]
pub yellow: Option<HexColor>,
#[serde(
deserialize_with = "de_color_opt",
serialize_with = "ser_color_opt",
skip_serializing_if = "Option::is_none"
)]
pub blue: Option<HexColor>,
#[serde(
deserialize_with = "de_color_opt",
serialize_with = "ser_color_opt",
skip_serializing_if = "Option::is_none"
)]
pub magenta: Option<HexColor>,
#[serde(
deserialize_with = "de_color_opt",
serialize_with = "ser_color_opt",
skip_serializing_if = "Option::is_none"
)]
pub cyan: Option<HexColor>,
#[serde(
deserialize_with = "de_color_opt",
serialize_with = "ser_color_opt",
skip_serializing_if = "Option::is_none"
)]
pub white: Option<HexColor>,
}
impl ColorSchemeAnsi {
pub fn is_empty(&self) -> bool {
self.black.is_none()
&& self.red.is_none()
&& self.green.is_none()
&& self.yellow.is_none()
&& self.blue.is_none()
&& self.magenta.is_none()
&& self.cyan.is_none()
&& self.white.is_none()
}
}
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(default, deny_unknown_fields)]
pub struct ColorScheme {
pub name: String,
#[serde(
deserialize_with = "de_color_opt",
serialize_with = "ser_color_opt",
skip_serializing_if = "Option::is_none"
)]
pub foreground: Option<HexColor>,
#[serde(
deserialize_with = "de_color_opt",
serialize_with = "ser_color_opt",
skip_serializing_if = "Option::is_none"
)]
pub background: Option<HexColor>,
#[serde(
deserialize_with = "de_color_opt",
serialize_with = "ser_color_opt",
skip_serializing_if = "Option::is_none"
)]
pub cursor: Option<HexColor>,
#[serde(
deserialize_with = "de_color_opt",
serialize_with = "ser_color_opt",
skip_serializing_if = "Option::is_none"
)]
pub bright_foreground: Option<HexColor>,
#[serde(
deserialize_with = "de_color_opt",
serialize_with = "ser_color_opt",
skip_serializing_if = "Option::is_none"
)]
pub dim_foreground: Option<HexColor>,
#[serde(skip_serializing_if = "ColorSchemeAnsi::is_empty")]
pub normal: ColorSchemeAnsi,
#[serde(skip_serializing_if = "ColorSchemeAnsi::is_empty")]
pub bright: ColorSchemeAnsi,
#[serde(skip_serializing_if = "ColorSchemeAnsi::is_empty")]
pub dim: ColorSchemeAnsi,
}
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(transparent)]
pub struct ProfileId(pub u64);
@ -63,6 +197,7 @@ impl Default for Profile {
#[derive(Clone, CosmicConfigEntry, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct Config {
pub app_theme: AppTheme,
pub color_schemes: BTreeMap<ColorSchemeId, ColorScheme>,
pub font_name: String,
pub font_size: u16,
pub font_weight: u16,
@ -84,6 +219,7 @@ impl Default for Config {
Self {
app_theme: AppTheme::System,
bold_font_weight: Weight::BOLD.0,
color_schemes: BTreeMap::new(),
dim_font_weight: Weight::NORMAL.0,
focus_follow_mouse: false,
font_name: "Fira Mono".to_string(),
@ -102,6 +238,25 @@ impl Default for Config {
}
impl Config {
// Get a sorted and adjusted for duplicates list of color scheme names and ids
pub fn color_scheme_names(&self) -> Vec<(String, ColorSchemeId)> {
let mut color_scheme_names =
Vec::<(String, ColorSchemeId)>::with_capacity(self.color_schemes.len());
for (color_scheme_id, color_scheme) in self.color_schemes.iter() {
let mut name = color_scheme.name.clone();
let mut copies = 1;
while color_scheme_names.iter().find(|x| x.0 == name).is_some() {
copies += 1;
name = format!("{} ({})", color_scheme.name, copies);
}
color_scheme_names.push((name, *color_scheme_id));
}
color_scheme_names.sort_by(|a, b| lexical_sort::natural_lexical_cmp(&a.0, &b.0));
color_scheme_names
}
fn font_size_adjusted(&self, zoom_adj: i8) -> f32 {
let font_size = f32::from(self.font_size).max(1.0);
let adj = f32::from(zoom_adj);
@ -120,7 +275,7 @@ impl Config {
(self.opacity as f32) / 100.0
}
// Get a sorted and adjusted for duplicates list of profiles names and ids
// Get a sorted and adjusted for duplicates list of profile names and ids
pub fn profile_names(&self) -> Vec<(String, ProfileId)> {
let mut profile_names = Vec::<(String, ProfileId)>::with_capacity(self.profiles.len());
for (profile_id, profile) in self.profiles.iter() {

View file

@ -30,11 +30,13 @@ impl IconCache {
};
}
bundle!("dialog-error-symbolic", 16);
bundle!("edit-clear-symbolic", 16);
bundle!("edit-delete-symbolic", 16);
bundle!("list-add-symbolic", 16);
bundle!("go-down-symbolic", 16);
bundle!("go-up-symbolic", 16);
bundle!("view-more-symbolic", 16);
bundle!("window-close-symbolic", 16);
Self { cache }

View file

@ -9,6 +9,7 @@ use cosmic::{
cosmic_config::{self, ConfigSet, CosmicConfigEntry},
cosmic_theme, executor,
iced::{
self,
advanced::graphics::text::font_system,
clipboard, event,
futures::SinkExt,
@ -20,17 +21,18 @@ use cosmic::{
widget::{self, button, pane_grid, segmented_button, PaneGrid},
Application, ApplicationExt, Element,
};
use cosmic_files::dialog::{Dialog, DialogKind, DialogMessage, DialogResult};
use cosmic_text::{fontdb::FaceInfo, Family, Stretch, Weight};
use std::{
any::TypeId,
cmp,
collections::{BTreeMap, BTreeSet, HashMap},
env, process,
env, fs, process,
sync::{atomic::Ordering, Mutex},
};
use tokio::sync::mpsc;
use config::{AppTheme, Config, Profile, ProfileId, CONFIG_VERSION};
use config::{AppTheme, ColorScheme, ColorSchemeId, Config, Profile, ProfileId, CONFIG_VERSION};
mod config;
mod mouse_reporter;
@ -166,6 +168,7 @@ pub struct Flags {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Action {
ColorSchemes,
Copy,
Find,
PaneFocusDown,
@ -204,6 +207,7 @@ pub enum Action {
impl Action {
pub fn message(self, entity_opt: Option<segmented_button::Entity>) -> Message {
match self {
Action::ColorSchemes => Message::ToggleContextPage(ContextPage::ColorSchemes),
Action::Copy => Message::Copy(entity_opt),
Action::Find => Message::Find(true),
Action::PaneFocusDown => Message::PaneFocusAdjacent(pane_grid::Direction::Down),
@ -245,6 +249,15 @@ impl Action {
#[derive(Clone, Debug)]
pub enum Message {
AppTheme(AppTheme),
ColorSchemeCollapse(ColorSchemeId),
ColorSchemeDelete(ColorSchemeId),
ColorSchemeExport(ColorSchemeId),
ColorSchemeExportResult(ColorSchemeId, DialogResult),
ColorSchemeExpand(ColorSchemeId),
ColorSchemeRename(ColorSchemeId, String),
ColorSchemeRenameSubmit,
ColorSchemeImport,
ColorSchemeImportResult(DialogResult),
Config(Config),
Copy(Option<segmented_button::Entity>),
DefaultFont(usize),
@ -254,6 +267,7 @@ pub enum Message {
DefaultDimFontWeight(usize),
DefaultBoldFontWeight(usize),
DefaultZoomStep(usize),
DialogMessage(DialogMessage),
Key(Modifiers, Key),
Find(bool),
FindNext,
@ -306,6 +320,7 @@ pub enum Message {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ContextPage {
ColorSchemes,
Profiles,
Settings,
}
@ -313,6 +328,7 @@ pub enum ContextPage {
impl ContextPage {
fn title(&self) -> String {
match self {
Self::ColorSchemes => fl!("color-schemes"),
Self::Profiles => fl!("profiles"),
Self::Settings => fl!("settings"),
}
@ -343,6 +359,7 @@ pub struct App {
theme_names: Vec<String>,
themes: HashMap<String, TermColors>,
context_page: ContextPage,
dialog_opt: Option<Dialog<Message>>,
terminal_ids: HashMap<pane_grid::Pane, widget::Id>,
find: bool,
find_search_id: widget::Id,
@ -350,15 +367,47 @@ pub struct App {
term_event_tx_opt: Option<mpsc::Sender<(pane_grid::Pane, segmented_button::Entity, TermEvent)>>,
startup_options: Option<tty::Options>,
term_config: TermConfig,
color_scheme_errors: Vec<String>,
color_scheme_expanded: Option<ColorSchemeId>,
color_scheme_renaming: Option<(ColorSchemeId, String)>,
color_scheme_rename_id: widget::Id,
profile_expanded: Option<ProfileId>,
show_advanced_font_settings: bool,
modifiers: Modifiers,
}
impl App {
fn update_color_schemes(&mut self) {
self.themes = terminal_theme::terminal_themes();
for (color_scheme_name, color_scheme_id) in self.config.color_scheme_names() {
if let Some(color_scheme) = self.config.color_schemes.get(&color_scheme_id) {
if self
.themes
.insert(color_scheme_name.clone(), color_scheme.into())
.is_some()
{
log::warn!(
"custom color scheme {:?} replaces builtin one",
color_scheme_name
);
}
}
}
self.theme_names.clear();
for theme_name in self.themes.keys() {
self.theme_names.push(theme_name.clone());
}
self.theme_names
.sort_by(|a, b| lexical_sort::natural_lexical_cmp(a, b));
}
fn update_config(&mut self) -> Command<Message> {
let theme = self.config.app_theme.theme();
// Update color schemes
self.update_color_schemes();
// Update terminal window background color
{
let color = Color::from(theme.cosmic().background.base);
@ -399,6 +448,20 @@ impl App {
self.update_config()
}
fn save_color_schemes(&mut self) -> Command<Message> {
// Optimized for just saving color_schemes
if let Some(ref config_handler) = self.config_handler {
match config_handler.set("color_schemes", &self.config.color_schemes) {
Ok(()) => {}
Err(err) => {
log::error!("failed to save config: {}", err);
}
}
}
self.update_color_schemes();
Command::none()
}
fn save_profiles(&mut self) -> Command<Message> {
// Optimized for just saving profiles
if let Some(ref config_handler) = self.config_handler {
@ -434,11 +497,14 @@ impl App {
None => (String::new(), "COSMIC Terminal".to_string()),
};
self.set_header_title(header_title);
Command::batch([self.set_window_title(window_title), self.update_focus()])
Command::batch([
self.set_window_title(window_title, window::Id::MAIN),
self.update_focus(),
])
} else {
log::error!("Failed to get the specific pane");
Command::batch([
self.set_window_title("COSMIC Terminal".to_string()),
self.set_window_title("COSMIC Terminal".to_string(), window::Id::MAIN),
self.update_focus(),
])
}
@ -521,6 +587,90 @@ impl App {
}
}
fn color_schemes(&self) -> Element<Message> {
let cosmic_theme::Spacing { space_xxxs, .. } = self.core().system_theme().cosmic().spacing;
let mut sections = Vec::with_capacity(2 + self.color_scheme_errors.len());
if !self.config.color_schemes.is_empty() {
let mut section = widget::settings::view_section("");
for (color_scheme_name, color_scheme_id) in self.config.color_scheme_names() {
let expanded = self.color_scheme_expanded == Some(color_scheme_id);
let renaming = match &self.color_scheme_renaming {
Some((id, value)) if id == &color_scheme_id => Some(value),
_ => None,
};
let button = if expanded {
widget::button(icon_cache_get("view-more-symbolic", 16))
.on_press(Message::ColorSchemeCollapse(color_scheme_id))
} else {
widget::button(icon_cache_get("view-more-symbolic", 16))
.on_press(Message::ColorSchemeExpand(color_scheme_id))
}
.style(style::Button::Icon);
let menu = menu::color_scheme_menu(color_scheme_id, &color_scheme_name);
let popover = widget::popover(button, menu).show_popup(expanded);
let item = match renaming {
Some(value) => widget::settings::item_row(vec![
widget::text_input("", value)
.id(self.color_scheme_rename_id.clone())
.on_input(move |value| {
Message::ColorSchemeRename(color_scheme_id, value)
})
.on_submit(Message::ColorSchemeRenameSubmit)
.into(),
popover.into(),
]),
None => widget::settings::item::builder(color_scheme_name).control(popover),
};
section = section.add(item);
}
sections.push(section.into());
}
sections.push(
widget::row::with_children(vec![
widget::horizontal_space(Length::Fill).into(),
widget::button::standard(fl!("import"))
.on_press(Message::ColorSchemeImport)
.into(),
])
.into(),
);
for error in self.color_scheme_errors.iter() {
sections.push(
widget::row::with_children(vec![
icon_cache_get("dialog-error-symbolic", 16)
.style(style::Svg::custom(|theme| {
let cosmic = theme.cosmic();
widget::svg::Appearance {
color: Some(cosmic.destructive_text_color().into()),
}
}))
.into(),
widget::text(error)
.style(style::Text::Custom(|theme| {
let cosmic = theme.cosmic();
//TODO: re-export in libcosmic
iced::widget::text::Appearance {
color: Some(cosmic.destructive_text_color().into()),
}
}))
.into(),
])
.spacing(space_xxxs)
.into(),
);
}
widget::settings::view_column(sections).into()
}
fn profiles(&self) -> Element<Message> {
let cosmic_theme::Spacing {
space_s,
@ -652,7 +802,7 @@ impl App {
let add_profile = widget::row::with_children(vec![
widget::horizontal_space(Length::Fill).into(),
widget::button(widget::text(fl!("add-profile")))
widget::button::standard(fl!("add-profile"))
.on_press(Message::ProfileNew)
.into(),
]);
@ -1067,9 +1217,6 @@ impl Application for App {
zoom_steps.push(zoom_step);
}
let themes = terminal_theme::terminal_themes();
let mut theme_names: Vec<_> = themes.keys().cloned().collect();
theme_names.sort();
let pane_model = TerminalPaneGrid::new(segmented_button::ModelBuilder::default().build());
let mut terminal_ids = HashMap::new();
terminal_ids.insert(pane_model.focus, widget::Id::unique());
@ -1094,9 +1241,10 @@ impl Application for App {
zoom_adj: 0,
zoom_step_names,
zoom_steps,
theme_names,
themes,
theme_names: Vec::new(),
themes: HashMap::new(),
context_page: ContextPage::Settings,
dialog_opt: None,
terminal_ids,
find: false,
find_search_id: widget::Id::unique(),
@ -1104,6 +1252,10 @@ impl Application for App {
startup_options: flags.startup_options,
term_config: flags.term_config,
term_event_tx_opt: None,
color_scheme_errors: Vec::new(),
color_scheme_expanded: None,
color_scheme_renaming: None,
color_scheme_rename_id: widget::Id::unique(),
profile_expanded: None,
show_advanced_font_settings: false,
modifiers: Modifiers::empty(),
@ -1172,6 +1324,137 @@ impl Application for App {
self.config.app_theme = app_theme;
return self.save_config();
}
Message::ColorSchemeCollapse(_color_scheme_id) => {
self.color_scheme_expanded = None;
}
Message::ColorSchemeDelete(color_scheme_id) => {
self.color_scheme_expanded = None;
self.config.color_schemes.remove(&color_scheme_id);
return self.save_color_schemes();
}
Message::ColorSchemeExport(color_scheme_id) => {
self.color_scheme_expanded = None;
if let Some(color_scheme) = self.config.color_schemes.get(&color_scheme_id) {
if self.dialog_opt.is_none() {
let (dialog, command) = Dialog::new(
DialogKind::SaveFile {
filename: format!("{}.ron", color_scheme.name),
},
None,
Message::DialogMessage,
move |result| {
Message::ColorSchemeExportResult(color_scheme_id.clone(), result)
},
);
self.dialog_opt = Some(dialog);
return command;
}
}
}
Message::ColorSchemeExportResult(color_scheme_id, result) => {
//TODO: show errors in UI
self.dialog_opt = None;
if let DialogResult::Open(paths) = result {
let path = &paths[0];
if let Some(color_scheme) = self.config.color_schemes.get(&color_scheme_id) {
match ron::ser::to_string_pretty(
&color_scheme,
ron::ser::PrettyConfig::new(),
) {
Ok(ron) => match fs::write(path, &ron) {
Ok(()) => {}
Err(err) => {
log::error!(
"failed to export {:?} to {:?}: {}",
color_scheme_id,
path,
err
);
}
},
Err(err) => {
log::error!(
"failed to serialize color scheme {:?}: {}",
color_scheme_id,
err
);
}
}
} else {
log::error!("failed to find color scheme {:?}", color_scheme_id);
}
}
}
Message::ColorSchemeExpand(color_scheme_id) => {
self.color_scheme_expanded = Some(color_scheme_id);
}
Message::ColorSchemeRename(color_scheme_id, color_scheme_name) => {
self.color_scheme_expanded = None;
let focus = self.color_scheme_renaming.is_none();
self.color_scheme_renaming = Some((color_scheme_id, color_scheme_name));
if focus {
return widget::text_input::focus(self.color_scheme_rename_id.clone());
}
}
Message::ColorSchemeRenameSubmit => {
if let Some((color_scheme_id, color_scheme_name)) =
self.color_scheme_renaming.take()
{
if let Some(color_scheme) = self.config.color_schemes.get_mut(&color_scheme_id)
{
color_scheme.name = color_scheme_name;
return self.save_color_schemes();
}
}
}
Message::ColorSchemeImport => {
if self.dialog_opt.is_none() {
self.color_scheme_errors.clear();
let (dialog, command) = Dialog::new(
DialogKind::OpenMultipleFiles,
None,
Message::DialogMessage,
move |result| Message::ColorSchemeImportResult(result),
);
self.dialog_opt = Some(dialog);
return command;
}
}
Message::ColorSchemeImportResult(result) => {
self.dialog_opt = None;
if let DialogResult::Open(paths) = result {
self.color_scheme_errors.clear();
for path in paths.iter() {
let mut file = match fs::File::open(path) {
Ok(ok) => ok,
Err(err) => {
self.color_scheme_errors
.push(format!("Failed to open {:?}: {}", path, err));
continue;
}
};
match ron::de::from_reader::<_, ColorScheme>(&mut file) {
Ok(color_scheme) => {
// Get next color_scheme ID
let color_scheme_id = self
.config
.color_schemes
.last_key_value()
.map(|(id, _)| ColorSchemeId(id.0 + 1))
.unwrap_or_default();
self.config
.color_schemes
.insert(color_scheme_id, color_scheme);
}
Err(err) => {
self.color_scheme_errors
.push(format!("Failed to parse {:?}: {}", path, err));
}
}
}
return self.save_color_schemes();
}
}
Message::Config(config) => {
if config != self.config {
log::info!("update config");
@ -1285,6 +1568,11 @@ impl Application for App {
log::warn!("failed to find zoom step with index {}", index);
}
},
Message::DialogMessage(dialog_message) => {
if let Some(dialog) = &mut self.dialog_opt {
return dialog.update(dialog_message);
}
}
Message::Key(modifiers, key) => {
for (key_bind, action) in self.key_binds.iter() {
if key_bind.matches(modifiers, &key) {
@ -1767,6 +2055,17 @@ impl Application for App {
self.context_page = context_page;
self.core.window.show_context = true;
}
// Extra work to do to prepare context pages
match self.context_page {
ContextPage::ColorSchemes => {
self.color_scheme_errors.clear();
self.color_scheme_expanded = None;
self.color_scheme_renaming = None;
}
_ => {}
}
self.set_context_title(context_page.title());
}
Message::ShowAdvancedFontSettings(show) => {
@ -1809,6 +2108,7 @@ impl Application for App {
}
Some(match self.context_page {
ContextPage::ColorSchemes => self.color_schemes(),
ContextPage::Profiles => self.profiles(),
ContextPage::Settings => self.settings(),
})
@ -1827,6 +2127,13 @@ impl Application for App {
.into()]
}
fn view_window(&self, window_id: window::Id) -> Element<Message> {
match &self.dialog_opt {
Some(dialog) => dialog.view(window_id),
None => widget::text("Unknown window ID").into(),
}
}
/// Creates a view after each update.
fn view(&self) -> Element<Self::Message> {
let cosmic_theme::Spacing { space_xxs, .. } = self.core().system_theme().cosmic().spacing;
@ -2026,6 +2333,10 @@ impl Application for App {
cosmic_theme::ThemeMode::version(),
)
.map(|_update| Message::SystemThemeChange),
match &self.dialog_opt {
Some(dialog) => dialog.subscription(),
None => subscription::Subscription::none(),
},
])
}
}

View file

@ -17,7 +17,7 @@ use cosmic::{
};
use std::collections::HashMap;
use crate::{fl, Action, Config, KeyBind, Message};
use crate::{fl, Action, ColorSchemeId, Config, KeyBind, Message};
macro_rules! menu_button {
($($x:expr),+ $(,)?) => (
@ -108,6 +108,38 @@ pub fn context_menu<'a>(
.into()
}
pub fn color_scheme_menu<'a>(id: ColorSchemeId, name: &str) -> Element<'a, Message> {
let menu_item = |label, message| menu_button!(widget::text(label)).on_press(message);
widget::container(column!(
menu_item(
fl!("rename"),
Message::ColorSchemeRename(id, name.to_string())
),
menu_item(fl!("export"), Message::ColorSchemeExport(id)),
menu_item(fl!("delete"), Message::ColorSchemeDelete(id)),
))
.padding(1)
//TODO: move style to libcosmic
.style(theme::Container::custom(|theme| {
let cosmic = theme.cosmic();
let component = &cosmic.background.component;
widget::container::Appearance {
icon_color: Some(component.on.into()),
text_color: Some(component.on.into()),
background: Some(Background::Color(component.base.into())),
border: Border {
radius: 8.0.into(),
width: 1.0,
color: component.divider.into(),
},
..Default::default()
}
}))
.width(Length::Fixed(120.0))
.into()
}
pub fn menu_bar<'a>(config: &Config, key_binds: &HashMap<KeyBind, Action>) -> Element<'a, Message> {
//TODO: port to libcosmic
let menu_root = |label| {
@ -185,6 +217,7 @@ pub fn menu_bar<'a>(config: &Config, key_binds: &HashMap<KeyBind, Action>) -> El
menu_item(fl!("split-vertical"), Action::PaneSplitVertical),
menu_item(fl!("pane-toggle-maximize"), Action::PaneToggleMaximized),
MenuTree::new(horizontal_rule(1)),
menu_item(fl!("menu-color-schemes"), Action::ColorSchemes),
menu_item(fl!("menu-settings"), Action::Settings),
],
),

View file

@ -2,11 +2,14 @@ use alacritty_terminal::{
term::color::Colors,
vte::ansi::{NamedColor, Rgb},
};
use hex_color::HexColor;
use palette::{encoding::Srgb, rgb::Rgb as PRgb, FromColor, Okhsl};
use std::collections::HashMap;
use std::{collections::HashMap, fs};
use crate::config::{ColorScheme, ColorSchemeAnsi};
// Fill missing dim/bright colors with derived values from normal ones.
#[allow(dead_code)]
struct ColorDerive {
dim_saturation_adjustment: f32,
dim_lightness_adjustment: f32,
@ -14,6 +17,7 @@ struct ColorDerive {
bright_lightness_adjustment: f32,
}
#[allow(dead_code)]
impl ColorDerive {
fn new() -> Self {
Self {
@ -138,311 +142,108 @@ fn auto_colors() -> Colors {
colors
}
fn tango_palette() -> Colors {
let mut colors = auto_colors();
impl From<&ColorScheme> for Colors {
fn from(color_scheme: &ColorScheme) -> Self {
let mut colors = auto_colors();
let encode_rgb = |data: u32| -> Rgb {
Rgb {
r: (data >> 16) as u8,
g: (data >> 8) as u8,
b: data as u8,
let encode_rgb = |rgb_opt: Option<HexColor>| -> Option<Rgb> {
let rgb = rgb_opt?;
Some(Rgb {
r: rgb.r,
g: rgb.g,
b: rgb.b,
})
};
// Set normal colors
colors[NamedColor::Black] = encode_rgb(color_scheme.normal.black);
colors[NamedColor::Red] = encode_rgb(color_scheme.normal.red);
colors[NamedColor::Green] = encode_rgb(color_scheme.normal.green);
colors[NamedColor::Yellow] = encode_rgb(color_scheme.normal.yellow);
colors[NamedColor::Blue] = encode_rgb(color_scheme.normal.blue);
colors[NamedColor::Magenta] = encode_rgb(color_scheme.normal.magenta);
colors[NamedColor::Cyan] = encode_rgb(color_scheme.normal.cyan);
colors[NamedColor::White] = encode_rgb(color_scheme.normal.white);
// Set bright colors
colors[NamedColor::BrightBlack] = encode_rgb(color_scheme.bright.black);
colors[NamedColor::BrightRed] = encode_rgb(color_scheme.bright.red);
colors[NamedColor::BrightGreen] = encode_rgb(color_scheme.bright.green);
colors[NamedColor::BrightYellow] = encode_rgb(color_scheme.bright.yellow);
colors[NamedColor::BrightBlue] = encode_rgb(color_scheme.bright.blue);
colors[NamedColor::BrightMagenta] = encode_rgb(color_scheme.bright.magenta);
colors[NamedColor::BrightCyan] = encode_rgb(color_scheme.bright.cyan);
colors[NamedColor::BrightWhite] = encode_rgb(color_scheme.bright.white);
// Set dim colors
colors[NamedColor::DimBlack] = encode_rgb(color_scheme.dim.black);
colors[NamedColor::DimRed] = encode_rgb(color_scheme.dim.red);
colors[NamedColor::DimGreen] = encode_rgb(color_scheme.dim.green);
colors[NamedColor::DimYellow] = encode_rgb(color_scheme.dim.yellow);
colors[NamedColor::DimBlue] = encode_rgb(color_scheme.dim.blue);
colors[NamedColor::DimMagenta] = encode_rgb(color_scheme.dim.magenta);
colors[NamedColor::DimCyan] = encode_rgb(color_scheme.dim.cyan);
colors[NamedColor::DimWhite] = encode_rgb(color_scheme.dim.white);
// Set special colors
colors[NamedColor::Foreground] = encode_rgb(color_scheme.foreground);
colors[NamedColor::Background] = encode_rgb(color_scheme.background);
colors[NamedColor::Cursor] = encode_rgb(color_scheme.cursor);
colors[NamedColor::BrightForeground] = encode_rgb(color_scheme.bright_foreground);
colors[NamedColor::DimForeground] = encode_rgb(color_scheme.dim_foreground);
colors
}
}
impl From<(&str, &Colors)> for ColorScheme {
fn from(tuple: (&str, &Colors)) -> Self {
let (name, colors) = tuple;
let encode_rgb = |rgb_opt: Option<Rgb>| -> Option<HexColor> {
let rgb = rgb_opt?;
Some(HexColor::rgb(rgb.r, rgb.g, rgb.b))
};
Self {
name: name.to_string(),
foreground: encode_rgb(colors[NamedColor::Foreground]),
background: encode_rgb(colors[NamedColor::Background]),
cursor: encode_rgb(colors[NamedColor::Cursor]),
bright_foreground: encode_rgb(colors[NamedColor::BrightForeground]),
dim_foreground: encode_rgb(colors[NamedColor::DimForeground]),
normal: ColorSchemeAnsi {
black: encode_rgb(colors[NamedColor::Black]),
red: encode_rgb(colors[NamedColor::Red]),
green: encode_rgb(colors[NamedColor::Green]),
yellow: encode_rgb(colors[NamedColor::Yellow]),
blue: encode_rgb(colors[NamedColor::Blue]),
magenta: encode_rgb(colors[NamedColor::Magenta]),
cyan: encode_rgb(colors[NamedColor::Cyan]),
white: encode_rgb(colors[NamedColor::White]),
},
bright: ColorSchemeAnsi {
black: encode_rgb(colors[NamedColor::BrightBlack]),
red: encode_rgb(colors[NamedColor::BrightRed]),
green: encode_rgb(colors[NamedColor::BrightGreen]),
yellow: encode_rgb(colors[NamedColor::BrightYellow]),
blue: encode_rgb(colors[NamedColor::BrightBlue]),
magenta: encode_rgb(colors[NamedColor::BrightMagenta]),
cyan: encode_rgb(colors[NamedColor::BrightCyan]),
white: encode_rgb(colors[NamedColor::BrightWhite]),
},
dim: ColorSchemeAnsi {
black: encode_rgb(colors[NamedColor::DimBlack]),
red: encode_rgb(colors[NamedColor::DimRed]),
green: encode_rgb(colors[NamedColor::DimGreen]),
yellow: encode_rgb(colors[NamedColor::DimYellow]),
blue: encode_rgb(colors[NamedColor::DimBlue]),
magenta: encode_rgb(colors[NamedColor::DimMagenta]),
cyan: encode_rgb(colors[NamedColor::DimCyan]),
white: encode_rgb(colors[NamedColor::DimWhite]),
},
}
};
colors[NamedColor::Black] = Some(encode_rgb(0x2E3436));
colors[NamedColor::Red] = Some(encode_rgb(0xCC0000));
colors[NamedColor::Green] = Some(encode_rgb(0x4E9A06));
colors[NamedColor::Yellow] = Some(encode_rgb(0xC4A000));
colors[NamedColor::Blue] = Some(encode_rgb(0x3465A4));
colors[NamedColor::Magenta] = Some(encode_rgb(0x75507B));
colors[NamedColor::Cyan] = Some(encode_rgb(0x06989A));
colors[NamedColor::White] = Some(encode_rgb(0xD3D7CF));
colors[NamedColor::BrightBlack] = Some(encode_rgb(0x555753));
colors[NamedColor::BrightRed] = Some(encode_rgb(0xEF2929));
colors[NamedColor::BrightGreen] = Some(encode_rgb(0x8AE234));
colors[NamedColor::BrightYellow] = Some(encode_rgb(0xFCE94F));
colors[NamedColor::BrightBlue] = Some(encode_rgb(0x729FCF));
colors[NamedColor::BrightMagenta] = Some(encode_rgb(0xAD7FA8));
colors[NamedColor::BrightCyan] = Some(encode_rgb(0x34E2E2));
colors[NamedColor::BrightWhite] = Some(encode_rgb(0xEEEEEC));
colors
}
fn tango_dark() -> Colors {
let mut colors = tango_palette();
// Set special colors
colors[NamedColor::Foreground] = colors[NamedColor::White];
colors[NamedColor::Background] = colors[NamedColor::Black];
colors[NamedColor::BrightForeground] = colors[NamedColor::BrightWhite];
colors[NamedColor::Cursor] = colors[NamedColor::Foreground];
// Fill missing dim colors
ColorDerive::new()
// Dim less so colors are readable with default bg
.with_dim_lightness_adjustment(-0.10)
.fill_missing_dims(&mut colors);
colors
}
fn tango_light() -> Colors {
let mut colors = tango_palette();
// Set special colors
colors[NamedColor::Foreground] = colors[NamedColor::Black];
colors[NamedColor::Background] = colors[NamedColor::BrightWhite];
colors[NamedColor::BrightForeground] = colors[NamedColor::BrightBlack];
colors[NamedColor::Cursor] = colors[NamedColor::Foreground];
// Fill missing dim colors
ColorDerive::new().fill_missing_dims(&mut colors);
colors
}
fn linux_console_palette() -> Colors {
let mut colors = auto_colors();
let encode_rgb = |data: u32| -> Rgb {
Rgb {
r: (data >> 16) as u8,
g: (data >> 8) as u8,
b: data as u8,
}
};
colors[NamedColor::Black] = Some(encode_rgb(0x000000));
colors[NamedColor::Red] = Some(encode_rgb(0xAA0000));
colors[NamedColor::Green] = Some(encode_rgb(0x00AA00));
colors[NamedColor::Yellow] = Some(encode_rgb(0xAA5500));
colors[NamedColor::Blue] = Some(encode_rgb(0x0000AA));
colors[NamedColor::Magenta] = Some(encode_rgb(0xAA00AA));
colors[NamedColor::Cyan] = Some(encode_rgb(0x00AAAA));
colors[NamedColor::White] = Some(encode_rgb(0xAAAAAA));
colors[NamedColor::BrightBlack] = Some(encode_rgb(0x555555));
colors[NamedColor::BrightRed] = Some(encode_rgb(0xFF5555));
colors[NamedColor::BrightGreen] = Some(encode_rgb(0x55FF55));
colors[NamedColor::BrightYellow] = Some(encode_rgb(0xFFFF55));
colors[NamedColor::BrightBlue] = Some(encode_rgb(0x5555FF));
colors[NamedColor::BrightMagenta] = Some(encode_rgb(0xFF55FF));
colors[NamedColor::BrightCyan] = Some(encode_rgb(0x55FFFF));
colors[NamedColor::BrightWhite] = Some(encode_rgb(0xFFFFFF));
colors
}
fn linux_console() -> Colors {
let mut colors = linux_console_palette();
// Set special colors
colors[NamedColor::Foreground] = colors[NamedColor::BrightWhite];
colors[NamedColor::Background] = colors[NamedColor::Black];
colors[NamedColor::BrightForeground] = colors[NamedColor::White];
colors[NamedColor::Cursor] = colors[NamedColor::Foreground];
// Fill missing dim colors
ColorDerive::new()
// Dim less so colors are readable with default bg
.with_dim_lightness_adjustment(-0.10)
.fill_missing_dims(&mut colors);
colors
}
fn xterm_palette() -> Colors {
let mut colors = auto_colors();
let encode_rgb = |data: u32| -> Rgb {
Rgb {
r: (data >> 16) as u8,
g: (data >> 8) as u8,
b: data as u8,
}
};
colors[NamedColor::Black] = Some(encode_rgb(0x000000));
colors[NamedColor::Red] = Some(encode_rgb(0xCD0000));
colors[NamedColor::Green] = Some(encode_rgb(0x00CD00));
colors[NamedColor::Yellow] = Some(encode_rgb(0xCDCD00));
colors[NamedColor::Blue] = Some(encode_rgb(0x0000EE));
colors[NamedColor::Magenta] = Some(encode_rgb(0xCD00CD));
colors[NamedColor::Cyan] = Some(encode_rgb(0x00CDCD));
colors[NamedColor::White] = Some(encode_rgb(0xE5E5E5));
colors[NamedColor::BrightBlack] = Some(encode_rgb(0x7F7F7F));
colors[NamedColor::BrightRed] = Some(encode_rgb(0xFF0000));
colors[NamedColor::BrightGreen] = Some(encode_rgb(0x00FF00));
colors[NamedColor::BrightYellow] = Some(encode_rgb(0xFFFF00));
colors[NamedColor::BrightBlue] = Some(encode_rgb(0x5C5CFF));
colors[NamedColor::BrightMagenta] = Some(encode_rgb(0xFF00FF));
colors[NamedColor::BrightCyan] = Some(encode_rgb(0x00FFFF));
colors[NamedColor::BrightWhite] = Some(encode_rgb(0xFFFFFF));
colors
}
fn xterm_dark() -> Colors {
let mut colors = xterm_palette();
// Set special colors
colors[NamedColor::Foreground] = colors[NamedColor::BrightWhite];
colors[NamedColor::Background] = colors[NamedColor::Black];
colors[NamedColor::BrightForeground] = colors[NamedColor::White];
colors[NamedColor::Cursor] = colors[NamedColor::Foreground];
// Fill missing dim colors
ColorDerive::new()
// Dim less so colors are readable with default bg
.with_dim_lightness_adjustment(-0.12)
.fill_missing_dims(&mut colors);
colors
}
fn xterm_light() -> Colors {
let mut colors = xterm_palette();
// Set special colors
colors[NamedColor::Foreground] = colors[NamedColor::Black];
colors[NamedColor::Background] = colors[NamedColor::BrightWhite];
colors[NamedColor::BrightForeground] = colors[NamedColor::BrightBlack];
colors[NamedColor::Cursor] = colors[NamedColor::Foreground];
// Fill missing dim colors
ColorDerive::new().fill_missing_dims(&mut colors);
colors
}
fn rxvt_palette() -> Colors {
let mut colors = auto_colors();
let encode_rgb = |data: u32| -> Rgb {
Rgb {
r: (data >> 16) as u8,
g: (data >> 8) as u8,
b: data as u8,
}
};
colors[NamedColor::Black] = Some(encode_rgb(0x000000));
colors[NamedColor::Red] = Some(encode_rgb(0xCD0000));
colors[NamedColor::Green] = Some(encode_rgb(0x00CD00));
colors[NamedColor::Yellow] = Some(encode_rgb(0xCDCD00));
colors[NamedColor::Blue] = Some(encode_rgb(0x0000CD));
colors[NamedColor::Magenta] = Some(encode_rgb(0xCD00CD));
colors[NamedColor::Cyan] = Some(encode_rgb(0x00CDCD));
colors[NamedColor::White] = Some(encode_rgb(0xFAEBD7));
colors[NamedColor::BrightBlack] = Some(encode_rgb(0x404040));
colors[NamedColor::BrightRed] = Some(encode_rgb(0xFF0000));
colors[NamedColor::BrightGreen] = Some(encode_rgb(0x00FF00));
colors[NamedColor::BrightYellow] = Some(encode_rgb(0xFFFF00));
colors[NamedColor::BrightBlue] = Some(encode_rgb(0x0000FF));
colors[NamedColor::BrightMagenta] = Some(encode_rgb(0xFF00FF));
colors[NamedColor::BrightCyan] = Some(encode_rgb(0x00FFFF));
colors[NamedColor::BrightWhite] = Some(encode_rgb(0xFFFFFF));
colors
}
fn rxvt_dark() -> Colors {
let mut colors = rxvt_palette();
// Set special colors
colors[NamedColor::Foreground] = colors[NamedColor::BrightWhite];
colors[NamedColor::Background] = colors[NamedColor::Black];
colors[NamedColor::BrightForeground] = colors[NamedColor::White];
colors[NamedColor::Cursor] = colors[NamedColor::Foreground];
// Fill missing dim colors
ColorDerive::new()
// Dim less so colors are readable with default bg
.with_dim_lightness_adjustment(-0.12)
.fill_missing_dims(&mut colors);
colors
}
fn rxvt_light() -> Colors {
let mut colors = rxvt_palette();
// Set special colors
colors[NamedColor::Foreground] = colors[NamedColor::Black];
colors[NamedColor::Background] = colors[NamedColor::BrightWhite];
colors[NamedColor::BrightForeground] = colors[NamedColor::BrightBlack];
colors[NamedColor::Cursor] = colors[NamedColor::Foreground];
// Fill missing dim colors
ColorDerive::new().fill_missing_dims(&mut colors);
colors
}
fn solarized_palette() -> Colors {
let mut colors = auto_colors();
let encode_rgb = |data: u32| -> Rgb {
Rgb {
r: (data >> 16) as u8,
g: (data >> 8) as u8,
b: data as u8,
}
};
colors[NamedColor::Black] = Some(encode_rgb(0x073642));
colors[NamedColor::Red] = Some(encode_rgb(0xDC322F));
colors[NamedColor::Green] = Some(encode_rgb(0x859900));
colors[NamedColor::Yellow] = Some(encode_rgb(0xB58900));
colors[NamedColor::Blue] = Some(encode_rgb(0x268BD2));
colors[NamedColor::Magenta] = Some(encode_rgb(0xD33682));
colors[NamedColor::Cyan] = Some(encode_rgb(0x2AA198));
colors[NamedColor::White] = Some(encode_rgb(0xEEE8D5));
colors[NamedColor::BrightBlack] = Some(encode_rgb(0x002B36));
colors[NamedColor::BrightRed] = Some(encode_rgb(0xCB4B16));
colors[NamedColor::BrightGreen] = Some(encode_rgb(0x586E75));
colors[NamedColor::BrightYellow] = Some(encode_rgb(0x657B83));
colors[NamedColor::BrightBlue] = Some(encode_rgb(0x839496));
colors[NamedColor::BrightMagenta] = Some(encode_rgb(0x6C71C4));
colors[NamedColor::BrightCyan] = Some(encode_rgb(0x93A1A1));
colors[NamedColor::BrightWhite] = Some(encode_rgb(0xFDF6E3));
colors
}
fn solarized_dark() -> Colors {
let mut colors = solarized_palette();
// Set special colors
colors[NamedColor::Foreground] = colors[NamedColor::BrightBlue];
colors[NamedColor::Background] = colors[NamedColor::BrightBlack];
colors[NamedColor::BrightForeground] = colors[NamedColor::Blue];
colors[NamedColor::Cursor] = colors[NamedColor::Foreground];
// Fill missing dim colors
ColorDerive::new().fill_missing_dims(&mut colors);
colors
}
fn solarized_light() -> Colors {
let mut colors = solarized_palette();
// Set special colors
colors[NamedColor::Foreground] = colors[NamedColor::BrightYellow];
colors[NamedColor::Background] = colors[NamedColor::BrightWhite];
colors[NamedColor::BrightForeground] = colors[NamedColor::Yellow];
colors[NamedColor::Cursor] = colors[NamedColor::Foreground];
// Fill missing dim colors
ColorDerive::new().fill_missing_dims(&mut colors);
colors
}
}
fn cosmic_dark() -> Colors {
@ -533,308 +334,44 @@ fn cosmic_light() -> Colors {
colors
}
fn gruvbox_dark() -> Colors {
let mut colors = auto_colors();
let encode_rgb = |data: u32| -> Rgb {
Rgb {
r: (data >> 16) as u8,
g: (data >> 8) as u8,
b: data as u8,
}
};
colors[NamedColor::Black] = Some(encode_rgb(0x282828));
colors[NamedColor::Red] = Some(encode_rgb(0xcc241d));
colors[NamedColor::Green] = Some(encode_rgb(0x98971a));
colors[NamedColor::Yellow] = Some(encode_rgb(0xd79921));
colors[NamedColor::Blue] = Some(encode_rgb(0x458588));
colors[NamedColor::Magenta] = Some(encode_rgb(0xb16286));
colors[NamedColor::Cyan] = Some(encode_rgb(0x689d6a));
colors[NamedColor::White] = Some(encode_rgb(0xa89984));
colors[NamedColor::BrightBlack] = Some(encode_rgb(0x928374));
colors[NamedColor::BrightRed] = Some(encode_rgb(0xfb4934));
colors[NamedColor::BrightGreen] = Some(encode_rgb(0xb8bb26));
colors[NamedColor::BrightYellow] = Some(encode_rgb(0xfabd2f));
colors[NamedColor::BrightBlue] = Some(encode_rgb(0x83a598));
colors[NamedColor::BrightMagenta] = Some(encode_rgb(0xd3869b));
colors[NamedColor::BrightCyan] = Some(encode_rgb(0x8ec07c));
colors[NamedColor::BrightWhite] = Some(encode_rgb(0xebdbb2));
// Set special colors
colors[NamedColor::Foreground] = colors[NamedColor::BrightWhite];
colors[NamedColor::Background] = colors[NamedColor::Black];
colors[NamedColor::Cursor] = colors[NamedColor::BrightWhite];
colors[NamedColor::BrightForeground] = colors[NamedColor::BrightWhite];
// Fill missing dim colors
ColorDerive::new()
// Dim less so colors are readable with default bg
.with_dim_lightness_adjustment(-0.15)
.fill_missing_dims(&mut colors);
colors
}
fn one_half_dark() -> Colors {
let mut colors = auto_colors();
let encode_rgb = |data: u32| -> Rgb {
Rgb {
r: (data >> 16) as u8,
g: (data >> 8) as u8,
b: data as u8,
}
};
colors[NamedColor::Black] = Some(encode_rgb(0x282c34));
colors[NamedColor::Red] = Some(encode_rgb(0xe06c75));
colors[NamedColor::Green] = Some(encode_rgb(0x98c379));
colors[NamedColor::Yellow] = Some(encode_rgb(0xe5c07b));
colors[NamedColor::Blue] = Some(encode_rgb(0x61afef));
colors[NamedColor::Magenta] = Some(encode_rgb(0xc678dd));
colors[NamedColor::Cyan] = Some(encode_rgb(0x56b6c2));
colors[NamedColor::White] = Some(encode_rgb(0xdcdfe4));
colors[NamedColor::BrightBlack] = Some(encode_rgb(0x5d677a));
// Set this before filling bright colors (including BrightForeground)
colors[NamedColor::Foreground] = colors[NamedColor::White];
let color_derive = ColorDerive::new();
// Fill missing bright colors
color_derive.fill_missing_brights(&mut colors);
// Set the rest of special colors
colors[NamedColor::Background] = colors[NamedColor::Black];
colors[NamedColor::Cursor] = colors[NamedColor::BrightWhite];
// Fill missing dim colors
color_derive.fill_missing_dims(&mut colors);
colors
}
fn pop_dark() -> Colors {
let mut colors = auto_colors();
let encode_rgb = |r: u8, g: u8, b: u8| -> Rgb { Rgb { r, g, b } };
// Pop colors (from pop-desktop gsettings)
colors[NamedColor::Black] = Some(encode_rgb(51, 51, 51));
colors[NamedColor::Red] = Some(encode_rgb(204, 0, 0));
colors[NamedColor::Green] = Some(encode_rgb(78, 154, 6));
colors[NamedColor::Yellow] = Some(encode_rgb(196, 160, 0));
colors[NamedColor::Blue] = Some(encode_rgb(52, 101, 164));
colors[NamedColor::Magenta] = Some(encode_rgb(117, 80, 123));
colors[NamedColor::Cyan] = Some(encode_rgb(6, 152, 154));
colors[NamedColor::White] = Some(encode_rgb(211, 215, 207));
colors[NamedColor::BrightBlack] = Some(encode_rgb(136, 128, 124));
colors[NamedColor::BrightRed] = Some(encode_rgb(241, 93, 34));
colors[NamedColor::BrightGreen] = Some(encode_rgb(115, 196, 143));
colors[NamedColor::BrightYellow] = Some(encode_rgb(255, 206, 81));
colors[NamedColor::BrightBlue] = Some(encode_rgb(72, 185, 199));
colors[NamedColor::BrightMagenta] = Some(encode_rgb(173, 127, 168));
colors[NamedColor::BrightCyan] = Some(encode_rgb(52, 226, 226));
colors[NamedColor::BrightWhite] = Some(encode_rgb(238, 238, 236));
// Set special colors
// Pop colors (from pop-desktop gsettings)
colors[NamedColor::Foreground] = Some(encode_rgb(242, 242, 242));
colors[NamedColor::Background] = Some(encode_rgb(51, 51, 51));
colors[NamedColor::Cursor] = colors[NamedColor::BrightWhite];
colors[NamedColor::BrightForeground] = colors[NamedColor::BrightWhite];
// Fill missing dim colors
ColorDerive::new()
// Dim less so colors are readable with default bg
.with_dim_lightness_adjustment(-0.05)
.fill_missing_dims(&mut colors);
colors
}
fn selenized_white() -> Colors {
let mut colors = auto_colors();
let encode_rgb = |data: u32| -> Rgb {
Rgb {
r: (data >> 16) as u8,
g: (data >> 8) as u8,
b: data as u8,
}
};
colors[NamedColor::Black] = Some(encode_rgb(0xEBEBEB));
colors[NamedColor::Red] = Some(encode_rgb(0xD6000C));
colors[NamedColor::Green] = Some(encode_rgb(0x1D9700));
colors[NamedColor::Yellow] = Some(encode_rgb(0xC49700));
colors[NamedColor::Blue] = Some(encode_rgb(0x0064E4));
colors[NamedColor::Magenta] = Some(encode_rgb(0xDD0F9D));
colors[NamedColor::Cyan] = Some(encode_rgb(0x00AD9C));
colors[NamedColor::White] = Some(encode_rgb(0x878787));
colors[NamedColor::BrightBlack] = Some(encode_rgb(0xCDCDCD));
colors[NamedColor::BrightRed] = Some(encode_rgb(0xBF0000));
colors[NamedColor::BrightGreen] = Some(encode_rgb(0x008400));
colors[NamedColor::BrightYellow] = Some(encode_rgb(0xAF8500));
colors[NamedColor::BrightBlue] = Some(encode_rgb(0x0054CF));
colors[NamedColor::BrightMagenta] = Some(encode_rgb(0xC7008B));
colors[NamedColor::BrightCyan] = Some(encode_rgb(0x009A8A));
colors[NamedColor::BrightWhite] = Some(encode_rgb(0x282828));
// Set special colors
colors[NamedColor::Background] = Some(encode_rgb(0xFFFFFF));
colors[NamedColor::Foreground] = Some(encode_rgb(0x474747));
colors[NamedColor::Cursor] = colors[NamedColor::Black];
// Fill missing dim colors
ColorDerive::new().fill_missing_dims(&mut colors);
colors
}
fn selenized_light() -> Colors {
let mut colors = auto_colors();
let encode_rgb = |data: u32| -> Rgb {
Rgb {
r: (data >> 16) as u8,
g: (data >> 8) as u8,
b: data as u8,
}
};
colors[NamedColor::Black] = Some(encode_rgb(0xECE3CC));
colors[NamedColor::Red] = Some(encode_rgb(0xD2212D));
colors[NamedColor::Green] = Some(encode_rgb(0x489100));
colors[NamedColor::Yellow] = Some(encode_rgb(0xAD8900));
colors[NamedColor::Blue] = Some(encode_rgb(0x0072D4));
colors[NamedColor::Magenta] = Some(encode_rgb(0xCA4898));
colors[NamedColor::Cyan] = Some(encode_rgb(0x009C8F));
colors[NamedColor::White] = Some(encode_rgb(0x909995));
colors[NamedColor::BrightBlack] = Some(encode_rgb(0xD5CDB6));
colors[NamedColor::BrightRed] = Some(encode_rgb(0xCC1729));
colors[NamedColor::BrightGreen] = Some(encode_rgb(0x428B00));
colors[NamedColor::BrightYellow] = Some(encode_rgb(0xA78300));
colors[NamedColor::BrightBlue] = Some(encode_rgb(0x006DCE));
colors[NamedColor::BrightMagenta] = Some(encode_rgb(0xC44392));
colors[NamedColor::BrightCyan] = Some(encode_rgb(0x00978A));
colors[NamedColor::BrightWhite] = Some(encode_rgb(0x3A4D53));
// Set special colors
colors[NamedColor::Background] = Some(encode_rgb(0xFBF3DB));
colors[NamedColor::Foreground] = Some(encode_rgb(0x53676D));
colors[NamedColor::Cursor] = colors[NamedColor::Black];
// Fill missing dim colors
ColorDerive::new().fill_missing_dims(&mut colors);
colors
}
fn selenized_dark() -> Colors {
let mut colors = auto_colors();
let encode_rgb = |data: u32| -> Rgb {
Rgb {
r: (data >> 16) as u8,
g: (data >> 8) as u8,
b: data as u8,
}
};
colors[NamedColor::Black] = Some(encode_rgb(0x184956));
colors[NamedColor::Red] = Some(encode_rgb(0xFA5750));
colors[NamedColor::Green] = Some(encode_rgb(0x75B938));
colors[NamedColor::Yellow] = Some(encode_rgb(0xDBB32D));
colors[NamedColor::Blue] = Some(encode_rgb(0x4695F7));
colors[NamedColor::Magenta] = Some(encode_rgb(0xF275BE));
colors[NamedColor::Cyan] = Some(encode_rgb(0x41C7B9));
colors[NamedColor::White] = Some(encode_rgb(0x72898F));
colors[NamedColor::BrightBlack] = Some(encode_rgb(0x2D5B69));
colors[NamedColor::BrightRed] = Some(encode_rgb(0xFF665C));
colors[NamedColor::BrightGreen] = Some(encode_rgb(0x84C747));
colors[NamedColor::BrightYellow] = Some(encode_rgb(0xEBC13D));
colors[NamedColor::BrightBlue] = Some(encode_rgb(0x58A3FF));
colors[NamedColor::BrightMagenta] = Some(encode_rgb(0xFF84CD));
colors[NamedColor::BrightCyan] = Some(encode_rgb(0x53D6C7));
colors[NamedColor::BrightWhite] = Some(encode_rgb(0xCAD8D9));
// Set special colors
colors[NamedColor::Background] = Some(encode_rgb(0x103C48));
colors[NamedColor::Foreground] = Some(encode_rgb(0xADBCBC));
colors[NamedColor::Cursor] = colors[NamedColor::White];
// Fill missing dim colors
ColorDerive::new().fill_missing_dims(&mut colors);
colors
}
fn selenized_black() -> Colors {
let mut colors = auto_colors();
let encode_rgb = |data: u32| -> Rgb {
Rgb {
r: (data >> 16) as u8,
g: (data >> 8) as u8,
b: data as u8,
}
};
colors[NamedColor::Black] = Some(encode_rgb(0x252525));
colors[NamedColor::Red] = Some(encode_rgb(0xED4A46));
colors[NamedColor::Green] = Some(encode_rgb(0x70B433));
colors[NamedColor::Yellow] = Some(encode_rgb(0xDBB32D));
colors[NamedColor::Blue] = Some(encode_rgb(0x368AEB));
colors[NamedColor::Magenta] = Some(encode_rgb(0xEB6EB7));
colors[NamedColor::Cyan] = Some(encode_rgb(0x3FC5B7));
colors[NamedColor::White] = Some(encode_rgb(0x777777));
colors[NamedColor::BrightBlack] = Some(encode_rgb(0x3B3B3B));
colors[NamedColor::BrightRed] = Some(encode_rgb(0xFF5E56));
colors[NamedColor::BrightGreen] = Some(encode_rgb(0x83C746));
colors[NamedColor::BrightYellow] = Some(encode_rgb(0xEFC541));
colors[NamedColor::BrightBlue] = Some(encode_rgb(0x4F9CFE));
colors[NamedColor::BrightMagenta] = Some(encode_rgb(0xFF81CA));
colors[NamedColor::BrightCyan] = Some(encode_rgb(0x56D8C9));
colors[NamedColor::BrightWhite] = Some(encode_rgb(0xDEDEDE));
// Set special colors
colors[NamedColor::Background] = Some(encode_rgb(0x181818));
colors[NamedColor::Foreground] = Some(encode_rgb(0xB9B9B9));
colors[NamedColor::Cursor] = colors[NamedColor::White];
// Fill missing dim colors
ColorDerive::new().fill_missing_dims(&mut colors);
colors
}
// Get builtin themes
pub fn terminal_themes() -> HashMap<String, Colors> {
let mut themes = HashMap::new();
themes.insert("Tango Dark".to_string(), tango_dark());
themes.insert("Tango Light".to_string(), tango_light());
themes.insert("XTerm Dark".to_string(), xterm_dark());
themes.insert("XTerm Light".to_string(), xterm_light());
themes.insert("Linux Console".to_string(), linux_console());
themes.insert("Rxvt Dark".to_string(), rxvt_dark());
themes.insert("Rxvt Light".to_string(), rxvt_light());
themes.insert("Solarized Dark".to_string(), solarized_dark());
themes.insert("Solarized Light".to_string(), solarized_light());
themes.insert("COSMIC Dark".to_string(), cosmic_dark());
themes.insert("COSMIC Light".to_string(), cosmic_light());
themes.insert("gruvbox-dark".to_string(), gruvbox_dark());
themes.insert("OneHalfDark".to_string(), one_half_dark());
themes.insert("Pop Dark".to_string(), pop_dark());
themes.insert("Selenized Black".to_string(), selenized_black());
themes.insert("Selenized Dark".to_string(), selenized_dark());
themes.insert("Selenized Light".to_string(), selenized_light());
themes.insert("Selenized White".to_string(), selenized_white());
themes
}
// Helper function to export builtin themes to theme files
#[allow(dead_code)]
pub fn export() {
for (name, theme) in terminal_themes() {
let color_scheme = ColorScheme::from((name.as_str(), &theme));
// Ensure conversion to and from ColorScheme matches original theme
{
let theme_conv = Colors::from(&color_scheme);
for i in 0..alacritty_terminal::term::color::COUNT {
assert_eq!(theme[i], theme_conv[i]);
}
}
let ron = match ron::ser::to_string_pretty(&color_scheme, ron::ser::PrettyConfig::new()) {
Ok(ok) => ok,
Err(err) => {
log::error!("failed to export {name:?}: {err}");
continue;
}
};
let path = format!("color-schemes/{name}.ron");
match fs::write(&path, ron) {
Ok(()) => {
log::info!("exported {path:?}");
}
Err(err) => {
log::error!("failed to esport {path:?}: {err}");
}
}
}
}