From cb89f2c36e08e74512ee7b8e1aff3bf461f774c2 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Tue, 26 Jul 2022 19:02:47 -0700 Subject: [PATCH] allow setting alpha for OSC 10, 11, 12 refs: https://github.com/wez/wezterm/issues/2313 --- color-types/src/lib.rs | 94 +++++++++++++++++---------------------- docs/changelog.md | 1 + termwiz/src/escape/osc.rs | 13 +++--- 3 files changed, 49 insertions(+), 59 deletions(-) diff --git a/color-types/src/lib.rs b/color-types/src/lib.rs index 21b5a1ac6..bf1c368d7 100644 --- a/color-types/src/lib.rs +++ b/color-types/src/lib.rs @@ -543,6 +543,32 @@ impl Hash for SrgbaTuple { impl Eq for SrgbaTuple {} +fn x_parse_color_component(value: &str) -> Result { + let mut component = 0u16; + let mut num_digits = 0; + + for c in value.chars() { + num_digits += 1; + component = component << 4; + + let nybble = match c.to_digit(16) { + Some(v) => v as u16, + None => return Err(()), + }; + component |= nybble; + } + + // From XParseColor, the `rgb:` prefixed syntax scales the + // value into 16 bits from the number of bits specified + Ok((match num_digits { + 1 => (component | component << 4) as f32, + 2 => component as f32, + 3 => (component >> 4) as f32, + 4 => (component >> 8) as f32, + _ => return Err(()), + }) / 255.0) +} + impl FromStr for SrgbaTuple { type Err = (); @@ -594,62 +620,26 @@ impl FromStr for SrgbaTuple { }}; } Ok(Self(digit!(), digit!(), digit!(), 1.0)) - } else if s.starts_with("rgb:") && s.len() > 6 { - // The string includes two slashes: `rgb:r/g/b` - let digits = (s.len() - 3) / 3; - if 3 + (digits * 3) != s.len() { + } else if let Some(value) = s.strip_prefix("rgb:") { + let fields: Vec<&str> = value.split('/').collect(); + if fields.len() != 3 { return Err(()); } - let digits = digits - 1; - if digits == 0 || digits > 4 { - // Max of 16 bits supported - return Err(()); - } - - let mut chars = s.chars().skip(4); - - macro_rules! digit { - () => {{ - let mut component = 0u16; - - for _ in 0..digits { - component = component << 4; - - let nybble = match chars.next().unwrap().to_digit(16) { - Some(v) => v as u16, - None => return Err(()), - }; - component |= nybble; - } - - // From XParseColor, the `rgb:` prefixed syntax scales the - // value into 16 bits from the number of bits specified - (match digits { - 1 => (component | component << 4) as f32, - 2 => component as f32, - 3 => (component >> 4) as f32, - 4 => (component >> 8) as f32, - _ => return Err(()), - }) / 255.0 - }}; - } - macro_rules! slash { - () => {{ - match chars.next() { - Some('/') => {} - _ => return Err(()), - } - }}; - } - let red = digit!(); - slash!(); - let green = digit!(); - slash!(); - let blue = digit!(); - + let red = x_parse_color_component(fields[0])?; + let green = x_parse_color_component(fields[1])?; + let blue = x_parse_color_component(fields[2])?; Ok(Self(red, green, blue, 1.0)) - } else if s.starts_with("rgba:") { + } else if let Some(value) = s.strip_prefix("rgba:") { + let fields: Vec<&str> = value.split('/').collect(); + if fields.len() == 4 { + let red = x_parse_color_component(fields[0])?; + let green = x_parse_color_component(fields[1])?; + let blue = x_parse_color_component(fields[2])?; + let alpha = x_parse_color_component(fields[3])?; + return Ok(Self(red, green, blue, alpha)); + } + let fields: Vec<_> = s[5..].split_ascii_whitespace().collect(); if fields.len() == 4 { fn field(s: &str) -> Result { diff --git a/docs/changelog.md b/docs/changelog.md index ebd736d0c..96b7b5d0b 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -47,6 +47,7 @@ As features stabilize some brief notes about them will accumulate here. * Internal scrollback datastructure improvements reduce per-cell overhead by up to ~40x depending on the composition of the line (lines with lots of varied attributes or image attachments will have more overhead). * Improved search performance * Quickselect: now defaults to searching 1000 lines above and below the current viewport, making it faster and the labels shorter for users with a larger scrollback. A new `scope_lines` parameter to [QuickSelectArgs](config/lua/keyassignment/QuickSelectArgs.md) allows controlling the search region explicitly. Thanks to [@yyogo](https://github.com/yyogo) for the initial PR! [#1317](https://github.com/wez/wezterm/pull/1317) +* OSC 10, 11 and 12 (Set Default Text Background, Default Text Foreground Color, and Text Cursor Color) now support setting the alpha component [#2313](https://github.com/wez/wezterm/issues/2313) #### Fixed * [ActivateKeyTable](config/lua/keyassignment/ActivateKeyTable.md)'s `replace_current` field was not actually optional. Made it optional. [#2179](https://github.com/wez/wezterm/issues/2179) diff --git a/termwiz/src/escape/osc.rs b/termwiz/src/escape/osc.rs index 04f56fc32..c8646e7f2 100644 --- a/termwiz/src/escape/osc.rs +++ b/termwiz/src/escape/osc.rs @@ -1,4 +1,4 @@ -use crate::color::{RgbColor, SrgbaTuple}; +use crate::color::SrgbaTuple; pub use crate::hyperlink::Hyperlink; use crate::{bail, ensure, Result}; use bitflags::bitflags; @@ -8,6 +8,7 @@ use ordered_float::NotNan; use std::collections::HashMap; use std::fmt::{Display, Error as FmtError, Formatter, Result as FmtResult}; use std::str; +use std::str::FromStr; #[derive(Debug, Clone, PartialEq)] pub enum ColorOrQuery { @@ -203,9 +204,8 @@ impl OperatingSystemCommand { ColorOrQuery::Query } else { ColorOrQuery::Color( - RgbColor::from_named_or_rgb_string(spec) - .ok_or_else(|| format!("invalid color spec {:?}", spec))? - .into(), + SrgbaTuple::from_str(spec) + .map_err(|()| format!("invalid color spec {:?}", spec))?, ) }; @@ -235,9 +235,8 @@ impl OperatingSystemCommand { } else { let spec = str::from_utf8(spec)?; colors.push(ColorOrQuery::Color( - RgbColor::from_named_or_rgb_string(spec) - .ok_or_else(|| format!("invalid color spec {:?}", spec))? - .into(), + SrgbaTuple::from_str(spec) + .map_err(|()| format!("invalid color spec {:?}", spec))?, )); } }