diff options
author | Kirill Chibisov <contact@kchibisov.com> | 2023-11-10 18:16:22 +0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-10 18:16:22 +0400 |
commit | 5060f8eeb864e8c304fbad9588bdd882db942356 (patch) | |
tree | b615ded19e6ac545b495f716e2a22ecd903332af /alacritty/src/display/color.rs | |
parent | 3ffd6c8f26f9788466b9ba95659b8de970a10f08 (diff) | |
download | r-alacritty-5060f8eeb864e8c304fbad9588bdd882db942356.tar.gz r-alacritty-5060f8eeb864e8c304fbad9588bdd882db942356.tar.bz2 r-alacritty-5060f8eeb864e8c304fbad9588bdd882db942356.zip |
Remove `alacritty_config` from alacritty_terminal
There's no need to force alacritty's user configuration on
other users of the crate, thus provide the options actually used
by alacritty_terminal itself.
Diffstat (limited to 'alacritty/src/display/color.rs')
-rw-r--r-- | alacritty/src/display/color.rs | 199 |
1 files changed, 196 insertions, 3 deletions
diff --git a/alacritty/src/display/color.rs b/alacritty/src/display/color.rs index f742ee84..669bf502 100644 --- a/alacritty/src/display/color.rs +++ b/alacritty/src/display/color.rs @@ -1,9 +1,14 @@ -use std::ops::{Index, IndexMut}; +use std::fmt::{self, Display, Formatter}; +use std::ops::{Add, Deref, Index, IndexMut, Mul}; +use std::str::FromStr; use log::trace; +use serde::de::{Error as SerdeError, Visitor}; +use serde::{Deserialize, Deserializer}; -use alacritty_terminal::ansi::NamedColor; -use alacritty_terminal::term::color::{Rgb, COUNT}; +use alacritty_config_derive::SerdeReplace; +use alacritty_terminal::term::color::COUNT; +use alacritty_terminal::vte::ansi::{NamedColor, Rgb as VteRgb}; use crate::config::color::Colors; @@ -165,3 +170,191 @@ impl IndexMut<NamedColor> for List { &mut self.0[idx as usize] } } + +#[derive(SerdeReplace, Debug, Eq, PartialEq, Copy, Clone, Default)] +pub struct Rgb(pub VteRgb); + +impl Rgb { + #[inline] + pub const fn new(r: u8, g: u8, b: u8) -> Self { + Self(VteRgb { r, g, b }) + } + + #[inline] + pub fn as_tuple(self) -> (u8, u8, u8) { + (self.0.r, self.0.g, self.0.b) + } +} + +impl From<VteRgb> for Rgb { + fn from(value: VteRgb) -> Self { + Self(value) + } +} + +impl Deref for Rgb { + type Target = VteRgb; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl Mul<f32> for Rgb { + type Output = Rgb; + + fn mul(self, rhs: f32) -> Self::Output { + Rgb(self.0 * rhs) + } +} + +impl Add<Rgb> for Rgb { + type Output = Rgb; + + fn add(self, rhs: Rgb) -> Self::Output { + Rgb(self.0 + rhs.0) + } +} + +/// Deserialize an Rgb from a hex string. +/// +/// This is *not* the deserialize impl for Rgb since we want a symmetric +/// serialize/deserialize impl for ref tests. +impl<'de> Deserialize<'de> for Rgb { + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> + where + D: Deserializer<'de>, + { + struct RgbVisitor; + + // Used for deserializing reftests. + #[derive(Deserialize)] + struct RgbDerivedDeser { + r: u8, + g: u8, + b: u8, + } + + impl<'a> Visitor<'a> for RgbVisitor { + type Value = Rgb; + + fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result { + f.write_str("hex color like #ff00ff") + } + + fn visit_str<E>(self, value: &str) -> Result<Rgb, E> + where + E: serde::de::Error, + { + Rgb::from_str(value).map_err(|_| { + E::custom(format!( + "failed to parse rgb color {value}; expected hex color like #ff00ff" + )) + }) + } + } + + // Return an error if the syntax is incorrect. + let value = toml::Value::deserialize(deserializer)?; + + // Attempt to deserialize from struct form. + if let Ok(RgbDerivedDeser { r, g, b }) = RgbDerivedDeser::deserialize(value.clone()) { + return Ok(Rgb::new(r, g, b)); + } + + // Deserialize from hex notation (either 0xff00ff or #ff00ff). + value.deserialize_str(RgbVisitor).map_err(D::Error::custom) + } +} + +impl Display for Rgb { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "#{:02x}{:02x}{:02x}", self.r, self.g, self.b) + } +} + +impl FromStr for Rgb { + type Err = (); + + fn from_str(s: &str) -> Result<Rgb, ()> { + let chars = if s.starts_with("0x") && s.len() == 8 { + &s[2..] + } else if s.starts_with('#') && s.len() == 7 { + &s[1..] + } else { + return Err(()); + }; + + match u32::from_str_radix(chars, 16) { + Ok(mut color) => { + let b = (color & 0xff) as u8; + color >>= 8; + let g = (color & 0xff) as u8; + color >>= 8; + let r = color as u8; + Ok(Rgb::new(r, g, b)) + }, + Err(_) => Err(()), + } + } +} + +/// RGB color optionally referencing the cell's foreground or background. +#[derive(SerdeReplace, Copy, Clone, Debug, PartialEq, Eq)] +pub enum CellRgb { + CellForeground, + CellBackground, + Rgb(Rgb), +} + +impl CellRgb { + pub fn color(self, foreground: Rgb, background: Rgb) -> Rgb { + match self { + Self::CellForeground => foreground, + Self::CellBackground => background, + Self::Rgb(rgb) => rgb, + } + } +} + +impl Default for CellRgb { + fn default() -> Self { + Self::Rgb(Rgb::default()) + } +} + +impl<'de> Deserialize<'de> for CellRgb { + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> + where + D: Deserializer<'de>, + { + const EXPECTING: &str = "CellForeground, CellBackground, or hex color like #ff00ff"; + + struct CellRgbVisitor; + impl<'a> Visitor<'a> for CellRgbVisitor { + type Value = CellRgb; + + fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result { + f.write_str(EXPECTING) + } + + fn visit_str<E>(self, value: &str) -> Result<CellRgb, E> + where + E: serde::de::Error, + { + // Attempt to deserialize as enum constants. + match value { + "CellForeground" => return Ok(CellRgb::CellForeground), + "CellBackground" => return Ok(CellRgb::CellBackground), + _ => (), + } + + Rgb::from_str(value).map(CellRgb::Rgb).map_err(|_| { + E::custom(format!("failed to parse color {value}; expected {EXPECTING}")) + }) + } + } + + deserializer.deserialize_str(CellRgbVisitor).map_err(D::Error::custom) + } +} |