termwiz: ColorSpec now allows for alpha to be tracked

This doesn't really change any behavior, but adjusts the types
such that CSIs that set colors have the potential to track the
alpha channel and that can make it through to the GUI/render layer.
This commit is contained in:
Wez Furlong committed 2022-07-26 19:39:53 -07:00
1 parent cb89f2c36e
commit bc083ee470
12 files changed
+82 -41

No files matched your search

Generated
+1
View File
@@ -5329,6 +5329,7 @@ dependencies = [
"deltae",
"lazy_static",
"serde",
"wezterm-dynamic",
]
[[package]]
+1
View File
@@ -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" }
+23
View File
@@ -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<Self, wezterm_dynamic::Error> {
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<SrgbaTuple> for (f32, f32, f32, f32) {
fn from(t: SrgbaTuple) -> (f32, f32, f32, f32) {
(t.0, t.1, t.2, t.3)
+20 -13
View File
@@ -48,6 +48,13 @@ impl From<SrgbaTuple> 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()
}
}
+3 -2
View File
@@ -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(),
),
)),
+1 -1
View File
@@ -1005,7 +1005,7 @@ mod test {
#[cfg(target_pointer_width = "64")]
fn memory_usage() {
assert_eq!(std::mem::size_of::<crate::color::RgbColor>(), 4);
assert_eq!(std::mem::size_of::<ColorAttribute>(), 8);
assert_eq!(std::mem::size_of::<ColorAttribute>(), 20);
assert_eq!(std::mem::size_of::<CellAttributes>(), 16);
assert_eq!(std::mem::size_of::<Cell>(), 24);
assert_eq!(std::mem::size_of::<Vec<u8>>(), 24);
+9 -3
View File
@@ -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<AnsiColor> for ColorSpec {
impl From<RgbColor> for ColorSpec {
fn from(col: RgbColor) -> Self {
ColorSpec::TrueColor(col.into())
}
}
impl From<SrgbaTuple> for ColorSpec {
fn from(col: SrgbaTuple) -> Self {
ColorSpec::TrueColor(col)
}
}
@@ -259,9 +265,9 @@ impl From<RgbColor> 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
+5 -5
View File
@@ -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!(
+3 -3
View File
@@ -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'),
],
+5 -5
View File
@@ -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'),
]
+1 -1
View File
@@ -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(),
);
+10 -8
View File
@@ -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 {