diff --git a/Cargo.lock b/Cargo.lock index 95e7f3d54..0e56de55b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4701,6 +4701,7 @@ name = "wezterm-color-types" version = "0.1.0" dependencies = [ "lazy_static", + "serde", ] [[package]] diff --git a/codec/src/lib.rs b/codec/src/lib.rs index 085aea170..e23efd279 100644 --- a/codec/src/lib.rs +++ b/codec/src/lib.rs @@ -405,7 +405,7 @@ macro_rules! pdu { /// The overall version of the codec. /// This must be bumped when backwards incompatible changes /// are made to the types and protocol. -pub const CODEC_VERSION: usize = 17; +pub const CODEC_VERSION: usize = 18; // Defines the Pdu enum. // Each struct has an explicit identifying number. diff --git a/color-types/Cargo.toml b/color-types/Cargo.toml index 358243e0d..8777bb115 100644 --- a/color-types/Cargo.toml +++ b/color-types/Cargo.toml @@ -7,5 +7,9 @@ repository = "https://github.com/wez/wezterm" description = "Types for working with colors" license = "MIT" +[features] +use_serde = ["serde"] + [dependencies] lazy_static = "1.4" +serde = {version="1.0", features = ["derive"], optional=true} diff --git a/color-types/src/lib.rs b/color-types/src/lib.rs index 394035593..eed16e803 100644 --- a/color-types/src/lib.rs +++ b/color-types/src/lib.rs @@ -1,3 +1,5 @@ +#[cfg(feature = "use_serde")] +use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::str::FromStr; @@ -209,6 +211,7 @@ impl SrgbaPixel { /// A pixel value encoded as SRGBA RGBA values in f32 format (range: 0.0-1.0) #[derive(Copy, Clone, Debug, Default, PartialEq)] +#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] pub struct SrgbaTuple(pub f32, pub f32, pub f32, pub f32); impl From<(f32, f32, f32, f32)> for SrgbaTuple { @@ -230,6 +233,11 @@ lazy_static::lazy_static! { fn build_colors() -> HashMap { let mut map = HashMap::new(); let rgb_txt = include_str!("rgb.txt"); + + map.insert("transparent".to_string(), SrgbaTuple(0., 0., 0., 0.)); + map.insert("none".to_string(), SrgbaTuple(0., 0., 0., 0.)); + map.insert("clear".to_string(), SrgbaTuple(0., 0., 0., 0.)); + for line in rgb_txt.lines() { let mut fields = line.split_ascii_whitespace(); let red = fields.next().unwrap(); @@ -289,6 +297,16 @@ impl SrgbaTuple { ) } + pub fn to_rgba_string(self) -> String { + format!( + "rgba:{} {} {} {}%", + (self.0 * 255.) as u8, + (self.1 * 255.) as u8, + (self.2 * 255.) as u8, + (self.3 * 100.) as u8 + ) + } + /// Returns a string of the form `rgb:RRRR/GGGG/BBBB` pub fn to_x11_16bit_rgb_string(self) -> String { format!( @@ -402,6 +420,31 @@ impl FromStr for SrgbaTuple { let blue = digit!(); Ok(Self(red, green, blue, 1.0)) + } else if s.starts_with("rgba:") { + let fields: Vec<_> = s[5..].split_ascii_whitespace().collect(); + if fields.len() == 4 { + fn field(s: &str) -> Result { + if s.ends_with('%') { + let v: f32 = s[0..s.len() - 1].parse().map_err(|_| ())?; + Ok(v / 100.) + } else { + let v: f32 = s.parse().map_err(|_| ())?; + if v > 255.0 || v < 0. { + Err(()) + } else { + Ok(v / 255.) + } + } + } + let r: f32 = field(fields[0])?; + let g: f32 = field(fields[1])?; + let b: f32 = field(fields[2])?; + let a: f32 = field(fields[3])?; + + Ok(Self(r, g, b, a)) + } else { + Err(()) + } } else if s.starts_with("hsl:") { let fields: Vec<_> = s[4..].split_ascii_whitespace().collect(); if fields.len() == 3 { @@ -431,7 +474,7 @@ impl FromStr for SrgbaTuple { Err(()) } } else { - Err(()) + Self::from_named(s).ok_or(()) } } } @@ -470,6 +513,20 @@ impl LinearRgba { pub const TRANSPARENT: Self = Self::with_components(0., 0., 0., 0.); + /// Returns true if this color is fully transparent + pub fn is_fully_transparent(self) -> bool { + self.3 == 0.0 + } + + /// Returns self, except when self is transparent, in which case returns other + pub fn when_fully_transparent(self, other: Self) -> Self { + if self.is_fully_transparent() { + other + } else { + self + } + } + /// Convert to an SRGB u32 pixel pub fn srgba_pixel(self) -> SrgbaPixel { SrgbaPixel::rgba( @@ -501,6 +558,20 @@ mod tests { assert_eq!(foo.to_rgb_string(), "#0015ff"); } + #[test] + fn from_rgba() { + assert_eq!( + SrgbaTuple::from_str("clear").unwrap().to_rgba_string(), + "rgba:0 0 0 0%" + ); + assert_eq!( + SrgbaTuple::from_str("rgba:100% 0 0 50%") + .unwrap() + .to_rgba_string(), + "rgba:255 0 0 50%" + ); + } + #[test] fn from_rgb() { assert!(SrgbaTuple::from_str("").is_err()); @@ -524,12 +595,4 @@ mod tests { let grey = SrgbaTuple::from_str("rgb:f0f0/f0f0/f0f0").unwrap(); assert_eq!(grey.to_rgb_string(), "#f0f0f0"); } - - #[cfg(feature = "use_serde")] - #[test] - fn roundtrip_rgbcolor() { - let data = varbincode::serialize(&SrgbaTuple::from_named("DarkGreen").unwrap()).unwrap(); - eprintln!("serialized as {:?}", data); - let _decoded: SrgbaTuple = varbincode::deserialize(data.as_slice()).unwrap(); - } } diff --git a/config/Cargo.toml b/config/Cargo.toml index 200a77b9c..f96beb2f2 100644 --- a/config/Cargo.toml +++ b/config/Cargo.toml @@ -36,7 +36,7 @@ serde = {version="1.0", features = ["rc", "derive"]} serde_json = "1.0" smol = "1.2" terminfo = "0.7" -termwiz = { path = "../termwiz" } +termwiz = { path = "../termwiz", features=["use_serde"] } toml = "0.5" umask = { path = "../umask" } unicode-segmentation = "1.8" diff --git a/config/src/color.rs b/config/src/color.rs index 8c1c2a2b8..70c98cc78 100644 --- a/config/src/color.rs +++ b/config/src/color.rs @@ -1,8 +1,9 @@ use crate::lua::{format_as_escapes, FormatItem}; use crate::*; use luahelper::impl_lua_conversion; +use std::str::FromStr; use termwiz::cell::CellAttributes; -pub use termwiz::color::{ColorSpec, RgbColor}; +pub use termwiz::color::{ColorSpec, RgbColor, SrgbaTuple}; #[derive(Debug, Copy, Deserialize, Serialize, Clone)] pub struct HsbTransform { @@ -44,6 +45,35 @@ where .collect()) } +#[derive(Default, Debug, Deserialize, Serialize, Clone, Copy)] +#[serde(try_from = "String", into = "String")] +pub struct RgbaColor { + #[serde(flatten)] + color: SrgbaTuple, +} + +impl Into for RgbaColor { + fn into(self) -> String { + self.color.to_rgb_string() + } +} + +impl Into for RgbaColor { + fn into(self) -> SrgbaTuple { + self.color + } +} + +impl std::convert::TryFrom for RgbaColor { + type Error = anyhow::Error; + fn try_from(s: String) -> anyhow::Result { + Ok(RgbaColor { + color: SrgbaTuple::from_str(&s) + .map_err(|_| anyhow::anyhow!("failed to parse {} as RgbaColor", &s))?, + }) + } +} + #[derive(Default, Debug, Deserialize, Serialize, Clone)] pub struct Palette { /// The text color to use when the attributes are reset to default @@ -55,8 +85,8 @@ pub struct Palette { pub cursor_bg: Option, pub cursor_border: Option, /// The color of selected text - pub selection_fg: Option, - pub selection_bg: Option, + pub selection_fg: Option, + pub selection_bg: Option, /// A list of 8 colors corresponding to the basic ANSI palette pub ansi: Option<[RgbColor; 8]>, /// A list of 8 colors corresponding to bright versions of the @@ -87,7 +117,7 @@ impl From for wezterm_term::color::ColorPalette { macro_rules! apply_color { ($name:ident) => { if let Some($name) = cfg.$name { - p.$name = $name; + p.$name = $name.into(); } }; } diff --git a/term/src/color.rs b/term/src/color.rs index 371d4865e..82816f7de 100644 --- a/term/src/color.rs +++ b/term/src/color.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer}; use std::fmt; use std::result::Result; -pub use termwiz::color::{AnsiColor, ColorAttribute, RgbColor}; +pub use termwiz::color::{AnsiColor, ColorAttribute, RgbColor, SrgbaTuple}; #[derive(Clone, PartialEq, Eq)] pub struct Palette256(pub [RgbColor; 256]); @@ -43,7 +43,7 @@ impl std::iter::FromIterator for Palette256 { } } -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq)] #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] pub struct ColorPalette { pub colors: Palette256, @@ -52,8 +52,8 @@ pub struct ColorPalette { pub cursor_fg: RgbColor, pub cursor_bg: RgbColor, pub cursor_border: RgbColor, - pub selection_fg: RgbColor, - pub selection_bg: RgbColor, + pub selection_fg: SrgbaTuple, + pub selection_bg: SrgbaTuple, pub scrollbar_thumb: RgbColor, pub split: RgbColor, } @@ -114,8 +114,8 @@ impl ColorPalette { cursor_fg: grey_out(self.cursor_fg), cursor_bg: grey_out(self.cursor_bg), cursor_border: grey_out(self.cursor_border), - selection_fg: grey_out(self.selection_fg), - selection_bg: grey_out(self.selection_bg), + selection_fg: grey_out(self.selection_fg.into()).into(), + selection_bg: grey_out(self.selection_bg.into()).into(), scrollbar_thumb: grey_out(self.scrollbar_thumb), split: grey_out(self.split), } @@ -206,8 +206,8 @@ impl ColorPalette { let cursor_border = RgbColor::new_8bpc(0x52, 0xad, 0x70); let cursor_fg = colors[AnsiColor::Black as usize]; - let selection_fg = colors[AnsiColor::Black as usize]; - let selection_bg = RgbColor::new_8bpc(0xff, 0xfa, 0xcd); + let selection_fg = colors[AnsiColor::Black as usize].into(); + let selection_bg = RgbColor::new_8bpc(0xff, 0xfa, 0xcd).into(); let scrollbar_thumb = RgbColor::new_8bpc(0x22, 0x22, 0x22); let split = RgbColor::new_8bpc(0x44, 0x44, 0x44); diff --git a/term/src/terminalstate/performer.rs b/term/src/terminalstate/performer.rs index a8de8ca21..0ee81a19e 100644 --- a/term/src/terminalstate/performer.rs +++ b/term/src/terminalstate/performer.rs @@ -800,13 +800,13 @@ impl<'a> Performer<'a> { ColorOrQuery::Query => { let response = OperatingSystemCommand::ChangeDynamicColors( which_color, - vec![ColorOrQuery::Color(self.palette().$name)], + vec![ColorOrQuery::Color(self.palette().$name.into())], ); log::trace!("Color Query response {:?}", response); write!(self.writer, "{}", response).ok(); self.writer.flush().ok(); } - ColorOrQuery::Color(c) => self.palette_mut().$name = c, + ColorOrQuery::Color(c) => self.palette_mut().$name = c.into(), } }; } diff --git a/termwiz/Cargo.toml b/termwiz/Cargo.toml index f759dec37..d9d08dba7 100644 --- a/termwiz/Cargo.toml +++ b/termwiz/Cargo.toml @@ -42,7 +42,7 @@ wezterm-color-types = { path = "../color-types" } [features] widgets = ["cassowary", "fnv"] -use_serde = ["serde"] +use_serde = ["serde", "wezterm-color-types/use_serde"] use_image = ["image"] docs = ["widgets", "use_serde"] diff --git a/termwiz/src/color.rs b/termwiz/src/color.rs index 9404c76e1..da0e34a2a 100644 --- a/termwiz/src/color.rs +++ b/termwiz/src/color.rs @@ -60,6 +60,12 @@ pub struct RgbColor { bits: u32, } +impl Into for RgbColor { + fn into(self) -> SrgbaTuple { + self.to_tuple_rgba() + } +} + impl RgbColor { /// Construct a color from discrete red, green, blue values /// in the range 0-255. diff --git a/wezterm-gui/src/termwindow/render.rs b/wezterm-gui/src/termwindow/render.rs index b8ac2bd21..f1b71310a 100644 --- a/wezterm-gui/src/termwindow/render.rs +++ b/wezterm-gui/src/termwindow/render.rs @@ -1276,8 +1276,8 @@ impl super::TermWindow { let selrange = self.selection(pos.pane.pane_id()).range.clone(); let start = Instant::now(); - let selection_fg = rgbcolor_to_window_color(palette.selection_fg); - let selection_bg = rgbcolor_to_window_color(palette.selection_bg); + let selection_fg = palette.selection_fg.to_linear(); + let selection_bg = palette.selection_bg.to_linear(); let cursor_fg = rgbcolor_to_window_color(palette.cursor_fg); let cursor_bg = rgbcolor_to_window_color(palette.cursor_bg); for (line_idx, line) in lines.iter().enumerate() { @@ -2495,9 +2495,11 @@ impl super::TermWindow { visibility, ) { // Selected text overrides colors - (true, _, _, CursorVisibility::Hidden) => { - (params.selection_fg, params.selection_bg, params.cursor_bg) - } + (true, _, _, CursorVisibility::Hidden) => ( + params.selection_fg.when_fully_transparent(params.fg_color), + params.selection_bg, + params.cursor_bg, + ), // block Cursor cell overrides colors ( _,