diff --git a/Cargo.lock b/Cargo.lock index 528b0fbd4..d10d833ff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5329,6 +5329,7 @@ dependencies = [ "deltae", "lazy_static", "serde", + "wezterm-dynamic", ] [[package]] diff --git a/color-types/Cargo.toml b/color-types/Cargo.toml index 97e2700fe..8e6d5dcf9 100644 --- a/color-types/Cargo.toml +++ b/color-types/Cargo.toml @@ -15,3 +15,4 @@ csscolorparser = {version="0.6", features=["lab"]} deltae = "0.3" lazy_static = "1.4" serde = {version="1.0", features = ["derive"], optional=true} +wezterm-dynamic = { path = "../wezterm-dynamic" } diff --git a/color-types/src/lib.rs b/color-types/src/lib.rs index bf1c368d7..f11b9b425 100644 --- a/color-types/src/lib.rs +++ b/color-types/src/lib.rs @@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::hash::{Hash, Hasher}; use std::str::FromStr; +use wezterm_dynamic::{FromDynamic, FromDynamicOptions, ToDynamic, Value}; lazy_static::lazy_static! { static ref SRGB_TO_F32_TABLE: [f32;256] = generate_srgb8_to_linear_f32_table(); @@ -214,6 +215,22 @@ impl SrgbaPixel { #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] pub struct SrgbaTuple(pub f32, pub f32, pub f32, pub f32); +impl ToDynamic for SrgbaTuple { + fn to_dynamic(&self) -> Value { + self.to_string().to_dynamic() + } +} + +impl FromDynamic for SrgbaTuple { + fn from_dynamic( + value: &Value, + options: FromDynamicOptions, + ) -> Result { + let s = String::from_dynamic(value, options)?; + Ok(SrgbaTuple::from_str(&s).map_err(|()| format!("unknown color name: {}", s))?) + } +} + impl From<(f32, f32, f32, f32)> for SrgbaTuple { fn from((r, g, b, a): (f32, f32, f32, f32)) -> SrgbaTuple { SrgbaTuple(r, g, b, a) @@ -231,6 +248,12 @@ impl From<(u8, u8, u8, u8)> for SrgbaTuple { } } +impl From<(u8, u8, u8)> for SrgbaTuple { + fn from((r, g, b): (u8, u8, u8)) -> SrgbaTuple { + SrgbaTuple(r as f32 / 255., g as f32 / 255., b as f32 / 255., 1.0) + } +} + impl From for (f32, f32, f32, f32) { fn from(t: SrgbaTuple) -> (f32, f32, f32, f32) { (t.0, t.1, t.2, t.3) diff --git a/config/src/color.rs b/config/src/color.rs index e067f9935..c99586f88 100644 --- a/config/src/color.rs +++ b/config/src/color.rs @@ -48,6 +48,13 @@ impl From for RgbaColor { } } +impl From<(u8, u8, u8)> for RgbaColor { + fn from((r, g, b): (u8, u8, u8)) -> Self { + let color: SrgbaTuple = (r, g, b).into(); + Self { color } + } +} + impl std::ops::Deref for RgbaColor { type Target = SrgbaTuple; fn deref(&self) -> &SrgbaTuple { @@ -219,9 +226,9 @@ pub struct TabBarColor { #[dynamic(default)] pub strikethrough: bool, /// The background color for the tab - pub bg_color: RgbColor, + pub bg_color: RgbaColor, /// The forgeground/text color for the tab - pub fg_color: RgbColor, + pub fg_color: RgbaColor, } impl TabBarColor { @@ -231,8 +238,8 @@ impl TabBarColor { .set_underline(self.underline) .set_italic(self.italic) .set_strikethrough(self.strikethrough) - .set_background(ColorSpec::TrueColor(self.bg_color)) - .set_foreground(ColorSpec::TrueColor(self.fg_color)); + .set_background(ColorSpec::TrueColor(*self.bg_color)) + .set_foreground(ColorSpec::TrueColor(*self.fg_color)); attr } } @@ -244,7 +251,7 @@ impl TabBarColor { pub struct TabBarColors { /// The background color for the tab bar #[dynamic(default = "default_background")] - pub background: RgbColor, + pub background: RgbaColor, /// Styling for the active tab #[dynamic(default = "default_active_tab")] @@ -273,8 +280,8 @@ pub struct TabBarColors { pub inactive_tab_edge_hover: RgbaColor, } -fn default_background() -> RgbColor { - RgbColor::new_8bpc(0x33, 0x33, 0x33) +fn default_background() -> RgbaColor { + (0x33, 0x33, 0x33).into() } fn default_inactive_tab_edge() -> RgbaColor { @@ -287,23 +294,23 @@ fn default_inactive_tab_edge_hover() -> RgbaColor { fn default_inactive_tab() -> TabBarColor { TabBarColor { - bg_color: RgbColor::new_8bpc(0x33, 0x33, 0x33), - fg_color: RgbColor::new_8bpc(0x80, 0x80, 0x80), + bg_color: (0x33, 0x33, 0x33).into(), + fg_color: (0x80, 0x80, 0x80).into(), ..TabBarColor::default() } } fn default_inactive_tab_hover() -> TabBarColor { TabBarColor { - bg_color: RgbColor::new_8bpc(0x1f, 0x1f, 0x1f), - fg_color: RgbColor::new_8bpc(0x90, 0x90, 0x90), + bg_color: (0x1f, 0x1f, 0x1f).into(), + fg_color: (0x90, 0x90, 0x90).into(), italic: true, ..TabBarColor::default() } } fn default_active_tab() -> TabBarColor { TabBarColor { - bg_color: RgbColor::new_8bpc(0x00, 0x00, 0x00), - fg_color: RgbColor::new_8bpc(0xc0, 0xc0, 0xc0), + bg_color: (0x00, 0x00, 0x00).into(), + fg_color: (0xc0, 0xc0, 0xc0).into(), ..TabBarColor::default() } } diff --git a/termwiz/examples/line_editor.rs b/termwiz/examples/line_editor.rs index 045f13d8e..06e78158d 100644 --- a/termwiz/examples/line_editor.rs +++ b/termwiz/examples/line_editor.rs @@ -1,5 +1,6 @@ +use std::str::FromStr; use termwiz::cell::AttributeChange; -use termwiz::color::{AnsiColor, ColorAttribute, RgbColor}; +use termwiz::color::{AnsiColor, ColorAttribute, SrgbaTuple}; use termwiz::lineedit::*; #[derive(Default)] @@ -15,7 +16,7 @@ impl LineEditorHost for Host { vec![ OutputElement::Attribute(AttributeChange::Background( ColorAttribute::TrueColorWithPaletteFallback( - RgbColor::from_named("darkslateblue").unwrap(), + SrgbaTuple::from_str("darkslateblue").unwrap(), AnsiColor::Navy.into(), ), )), diff --git a/termwiz/src/cell.rs b/termwiz/src/cell.rs index a37f10c29..c4262c308 100644 --- a/termwiz/src/cell.rs +++ b/termwiz/src/cell.rs @@ -1005,7 +1005,7 @@ mod test { #[cfg(target_pointer_width = "64")] fn memory_usage() { assert_eq!(std::mem::size_of::(), 4); - assert_eq!(std::mem::size_of::(), 8); + assert_eq!(std::mem::size_of::(), 20); assert_eq!(std::mem::size_of::(), 16); assert_eq!(std::mem::size_of::(), 24); assert_eq!(std::mem::size_of::>(), 24); diff --git a/termwiz/src/color.rs b/termwiz/src/color.rs index 9df1ab225..f8cae95f6 100644 --- a/termwiz/src/color.rs +++ b/termwiz/src/color.rs @@ -230,7 +230,7 @@ pub enum ColorSpec { Default, /// Use either a raw number, or use values from the `AnsiColor` enum PaletteIndex(PaletteIndex), - TrueColor(RgbColor), + TrueColor(SrgbaTuple), } impl Default for ColorSpec { @@ -247,6 +247,12 @@ impl From for ColorSpec { impl From for ColorSpec { fn from(col: RgbColor) -> Self { + ColorSpec::TrueColor(col.into()) + } +} + +impl From for ColorSpec { + fn from(col: SrgbaTuple) -> Self { ColorSpec::TrueColor(col) } } @@ -259,9 +265,9 @@ impl From for ColorSpec { #[derive(Debug, Clone, Copy, Eq, PartialEq, FromDynamic, ToDynamic)] pub enum ColorAttribute { /// Use RgbColor when supported, falling back to the specified PaletteIndex. - TrueColorWithPaletteFallback(RgbColor, PaletteIndex), + TrueColorWithPaletteFallback(SrgbaTuple, PaletteIndex), /// Use RgbColor when supported, falling back to the default color - TrueColorWithDefaultFallback(RgbColor), + TrueColorWithDefaultFallback(SrgbaTuple), /// Use the specified PaletteIndex PaletteIndex(PaletteIndex), /// Use the default color diff --git a/termwiz/src/escape/csi.rs b/termwiz/src/escape/csi.rs index 4668b75c5..1b8787dfe 100644 --- a/termwiz/src/escape/csi.rs +++ b/termwiz/src/escape/csi.rs @@ -1435,7 +1435,7 @@ impl Display for Sgr { (White, ForegroundBrightWhite) ), Sgr::Foreground(ColorSpec::TrueColor(c)) => { - let (red, green, blue) = c.to_tuple_rgb8(); + let (red, green, blue, _alpha) = c.to_srgb_u8(); write!( f, "{}:2::{}:{}:{}m", @@ -1469,7 +1469,7 @@ impl Display for Sgr { (White, BackgroundBrightWhite) ), Sgr::Background(ColorSpec::TrueColor(c)) => { - let (red, green, blue) = c.to_tuple_rgb8(); + let (red, green, blue, _alpha) = c.to_srgb_u8(); write!( f, "{}:2::{}:{}:{}m", @@ -1481,7 +1481,7 @@ impl Display for Sgr { } Sgr::UnderlineColor(ColorSpec::Default) => code!(ResetUnderlineColor), Sgr::UnderlineColor(ColorSpec::TrueColor(c)) => { - let (red, green, blue) = c.to_tuple_rgb8(); + let (red, green, blue, _alpha) = c.to_srgb_u8(); write!( f, "{}:2::{}:{}:{}m", @@ -2720,7 +2720,7 @@ mod test { assert_eq!( parse('m', &[58, 2, 255, 255, 255], "\x1b[58:2::255:255:255m"), vec![CSI::Sgr(Sgr::UnderlineColor(ColorSpec::TrueColor( - RgbColor::new_8bpc(255, 255, 255), + (255, 255, 255).into(), )))] ); assert_eq!( @@ -2760,7 +2760,7 @@ mod test { assert_eq!( parse('m', &[38, 2, 255, 255, 255], "\x1b[38:2::255:255:255m"), vec![CSI::Sgr(Sgr::Foreground(ColorSpec::TrueColor( - RgbColor::new_8bpc(255, 255, 255), + (255, 255, 255).into(), )))] ); assert_eq!( diff --git a/termwiz/src/escape/parser/mod.rs b/termwiz/src/escape/parser/mod.rs index ef0c70155..1a71a1d54 100644 --- a/termwiz/src/escape/parser/mod.rs +++ b/termwiz/src/escape/parser/mod.rs @@ -337,7 +337,7 @@ impl<'a, F: FnMut(Action)> VTActor for Performer<'a, F> { mod test { use super::*; use crate::cell::{Intensity, Underline}; - use crate::color::{ColorSpec, RgbColor}; + use crate::color::ColorSpec; use crate::escape::csi::{ CharacterPath, DecPrivateMode, DecPrivateModeCode, Device, Mode, Sgr, Window, XtSmGraphics, XtSmGraphicsItem, XtermKeyModifierResource, @@ -433,7 +433,7 @@ mod test { assert_eq!( vec![ Action::CSI(CSI::Sgr(Sgr::Foreground(ColorSpec::TrueColor( - RgbColor::new_8bpc(128, 64, 192) + (128, 64, 192).into() )))), Action::Print('w'), ], @@ -446,7 +446,7 @@ mod test { assert_eq!( vec![ Action::CSI(CSI::Sgr(Sgr::Foreground(ColorSpec::TrueColor( - RgbColor::new_8bpc(0, 255, 0) + (0, 255, 0).into() )))), Action::Print('w'), ], diff --git a/termwiz/src/render/terminfo.rs b/termwiz/src/render/terminfo.rs index 88b03c4bc..e66a793ac 100644 --- a/termwiz/src/render/terminfo.rs +++ b/termwiz/src/render/terminfo.rs @@ -691,7 +691,7 @@ mod test { use super::*; use crate::bail; use crate::caps::ProbeHints; - use crate::color::{AnsiColor, ColorAttribute, RgbColor}; + use crate::color::{AnsiColor, ColorAttribute}; use crate::escape::parser::Parser; use crate::escape::{Action, Esc, EscCode}; use crate::input::InputEvent; @@ -1180,7 +1180,7 @@ mod test { let mut out = FakeTerm::new(xterm_terminfo()); out.render(&[ Change::Attribute(AttributeChange::Foreground( - ColorSpec::TrueColor(RgbColor::new_8bpc(255, 128, 64)).into(), + ColorSpec::TrueColor((255, 128, 64).into()).into(), )), Change::Text("A".into()), ]) @@ -1191,7 +1191,7 @@ mod test { result, vec![ Action::CSI(CSI::Sgr(Sgr::Foreground( - ColorSpec::TrueColor(RgbColor::new_8bpc(255, 128, 64)).into(), + ColorSpec::TrueColor((255, 128, 64).into()).into(), ))), Action::Print('A'), ] @@ -1203,7 +1203,7 @@ mod test { let mut out = FakeTerm::new(no_terminfo_all_enabled()); out.render(&[ Change::Attribute(AttributeChange::Foreground( - ColorSpec::TrueColor(RgbColor::new_8bpc(255, 128, 64)).into(), + ColorSpec::TrueColor((255, 128, 64).into()).into(), )), Change::Text("A".into()), ]) @@ -1214,7 +1214,7 @@ mod test { result, vec![ Action::CSI(CSI::Sgr(Sgr::Foreground( - ColorSpec::TrueColor(RgbColor::new_8bpc(255, 128, 64)).into(), + ColorSpec::TrueColor((255, 128, 64).into()).into(), ))), Action::Print('A'), ] diff --git a/wezterm-gui/src/tabbar.rs b/wezterm-gui/src/tabbar.rs index 61e3117fd..bd7930844 100644 --- a/wezterm-gui/src/tabbar.rs +++ b/wezterm-gui/src/tabbar.rs @@ -334,7 +334,7 @@ impl TabBarState { let black_cell = Cell::blank_with_attrs( CellAttributes::default() - .set_background(ColorSpec::TrueColor(colors.background)) + .set_background(ColorSpec::TrueColor(*colors.background)) .clone(), ); diff --git a/wezterm-gui/src/termwindow/render.rs b/wezterm-gui/src/termwindow/render.rs index 4f5833447..4779e5b10 100644 --- a/wezterm-gui/src/termwindow/render.rs +++ b/wezterm-gui/src/termwindow/render.rs @@ -528,8 +528,8 @@ impl super::TermWindow { .border(BoxDimension::new(Dimension::Pixels(0.))) .colors(ElementColors { border: BorderColor::default(), - bg: rgbcolor_to_window_color(colors.inactive_tab.bg_color).into(), - text: rgbcolor_to_window_color(colors.inactive_tab.fg_color).into(), + bg: colors.inactive_tab.bg_color.to_linear().into(), + text: colors.inactive_tab.fg_color.to_linear().into(), }), TabBarItem::NewTabButton => Element::new( &font, @@ -559,13 +559,13 @@ impl super::TermWindow { .border(BoxDimension::new(Dimension::Pixels(1.))) .colors(ElementColors { border: BorderColor::default(), - bg: rgbcolor_to_window_color(colors.new_tab.bg_color).into(), - text: rgbcolor_to_window_color(colors.new_tab.fg_color).into(), + bg: colors.new_tab.bg_color.to_linear().into(), + text: colors.new_tab.fg_color.to_linear().into(), }) .hover_colors(Some(ElementColors { border: BorderColor::default(), - bg: rgbcolor_to_window_color(colors.new_tab_hover.bg_color).into(), - text: rgbcolor_to_window_color(colors.new_tab_hover.fg_color).into(), + bg: colors.new_tab_hover.bg_color.to_linear().into(), + text: colors.new_tab_hover.fg_color.to_linear().into(), })), TabBarItem::Tab { active, .. } if active => element .item_type(UIItemType::TabBar(item.item.clone())) @@ -725,17 +725,19 @@ impl super::TermWindow { .item_type(UIItemType::CloseTab(tab_idx)) .hover_colors(Some(ElementColors { border: BorderColor::default(), - bg: rgbcolor_to_window_color(if active { + bg: (if active { colors.inactive_tab_hover.bg_color } else { colors.active_tab.bg_color }) + .to_linear() .into(), - text: rgbcolor_to_window_color(if active { + text: (if active { colors.inactive_tab_hover.fg_color } else { colors.active_tab.fg_color }) + .to_linear() .into(), })) .padding(BoxDimension {