diff options
| author | Aram Drevekenin <aram@poor.dev> | 2021-05-04 16:06:55 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-04 16:06:55 +0200 |
| commit | 1f88b342e2a381b958da4bebd852b73e42db1a8c (patch) | |
| tree | 0edaf24bdfbe85c0f51a71ecf26a29d626d9e25d /src | |
| parent | chore: simplify boundary combination function (diff) | |
| download | sl0ppij-1f88b342e2a381b958da4bebd852b73e42db1a8c.tar.gz sl0ppij-1f88b342e2a381b958da4bebd852b73e42db1a8c.tar.xz sl0ppij-1f88b342e2a381b958da4bebd852b73e42db1a8c.zip | |
fix(colors): stabilize colors (#453)
* fix(colors): stabilize colors
* style(fmt): rustfmt
Diffstat (limited to 'src')
| -rw-r--r-- | src/client/boundaries.rs | 30 | ||||
| -rw-r--r-- | src/common/os_input_output.rs | 49 | ||||
| -rw-r--r-- | src/common/utils/shared.rs | 62 | ||||
| -rw-r--r-- | src/tests/fakes.rs | 38 |
4 files changed, 52 insertions, 127 deletions
diff --git a/src/client/boundaries.rs b/src/client/boundaries.rs index 7a85812f..86fb37fc 100644 --- a/src/client/boundaries.rs +++ b/src/client/boundaries.rs @@ -1,8 +1,8 @@ use crate::tab::Pane; use crate::utils::shared::colors; -use ansi_term::Colour::RGB; +use ansi_term::Colour::{Fixed, RGB}; use std::collections::HashMap; -use zellij_tile::data::{InputMode, Palette}; +use zellij_tile::data::{InputMode, Palette, PaletteColor}; use std::fmt::{Display, Error, Formatter}; pub mod boundary_type { @@ -19,20 +19,13 @@ pub mod boundary_type { pub const CROSS: &str = "┼"; } -// pub mod colors { -// use ansi_term::Colour::{self, Fixed}; -// pub const GREEN: Colour = Fixed(154); -// pub const GRAY: Colour = Fixed(238); -// pub const ORANGE: Colour = Fixed(166); -// } - pub type BoundaryType = &'static str; // easy way to refer to boundary_type above #[derive(Clone, Copy, Debug)] pub struct BoundarySymbol { boundary_type: BoundaryType, invisible: bool, - color: Option<(u8, u8, u8)>, + color: Option<PaletteColor>, } impl BoundarySymbol { @@ -40,14 +33,14 @@ impl BoundarySymbol { BoundarySymbol { boundary_type, invisible: false, - color: Some(colors::GRAY), + color: Some(PaletteColor::EightBit(colors::GRAY)), } } pub fn invisible(mut self) -> Self { self.invisible = true; self } - pub fn color(&mut self, color: Option<(u8, u8, u8)>) -> Self { + pub fn color(&mut self, color: Option<PaletteColor>) -> Self { self.color = color; *self } @@ -58,11 +51,14 @@ impl Display for BoundarySymbol { match self.invisible { true => write!(f, " "), false => match self.color { - Some(color) => write!( - f, - "{}", - RGB(color.0, color.1, color.2).paint(self.boundary_type) - ), + Some(color) => match color { + PaletteColor::Rgb((r, g, b)) => { + write!(f, "{}", RGB(r, g, b).paint(self.boundary_type)) + } + PaletteColor::EightBit(color) => { + write!(f, "{}", Fixed(color).paint(self.boundary_type)) + } + }, None => write!(f, "{}", self.boundary_type), }, } diff --git a/src/common/os_input_output.rs b/src/common/os_input_output.rs index 6ed402de..74d14036 100644 --- a/src/common/os_input_output.rs +++ b/src/common/os_input_output.rs @@ -1,5 +1,5 @@ use crate::panes::PositionAndSize; -use crate::utils::shared::{default_palette, detect_theme, hex_to_rgb}; +use crate::utils::shared::default_palette; use nix::fcntl::{fcntl, FcntlArg, OFlag}; use nix::pty::{forkpty, Winsize}; use nix::sys::signal::{kill, Signal}; @@ -13,8 +13,7 @@ use std::os::unix::io::RawFd; use std::path::PathBuf; use std::process::{Child, Command}; use std::sync::{Arc, Mutex}; -use xrdb::Colors; -use zellij_tile::data::{Palette, PaletteSource}; +use zellij_tile::data::Palette; use signal_hook::{consts::signal::*, iterator::Signals}; @@ -266,49 +265,7 @@ impl OsApi for OsInputOutput { } } fn load_palette(&self) -> Palette { - let palette = match Colors::new("xresources") { - Some(palette) => { - let fg = if let Some(foreground) = palette.fg.as_deref().map(hex_to_rgb) { - foreground - } else { - return default_palette(); - }; - - let bg = if let Some(background) = palette.bg.as_deref().map(hex_to_rgb) { - background - } else { - return default_palette(); - }; - - // NOTE: `16` is the same as the length of `palette.colors`. - let mut colors: [(u8, u8, u8); 16] = [(0, 0, 0); 16]; - for (idx, color) in palette.colors.iter().enumerate() { - if let Some(c) = color { - colors[idx] = hex_to_rgb(c); - } else { - return default_palette(); - } - } - let theme = detect_theme(bg); - Palette { - source: PaletteSource::Xresources, - theme, - fg, - bg, - black: colors[0], - red: colors[1], - green: colors[2], - yellow: colors[3], - blue: colors[4], - magenta: colors[5], - cyan: colors[6], - white: colors[7], - orange: colors[9], - } - } - None => default_palette(), - }; - palette + default_palette() } } diff --git a/src/common/utils/shared.rs b/src/common/utils/shared.rs index c705ed0d..7b4cbcb5 100644 --- a/src/common/utils/shared.rs +++ b/src/common/utils/shared.rs @@ -5,7 +5,7 @@ use std::{iter, str::from_utf8}; use strip_ansi_escapes::strip; use colors_transform::{Color, Rgb}; -use zellij_tile::data::{Palette, PaletteSource, Theme}; +use zellij_tile::data::{Palette, PaletteColor, PaletteSource, Theme}; fn ansi_len(s: &str) -> usize { from_utf8(&strip(s.as_bytes()).unwrap()) @@ -34,13 +34,13 @@ pub fn adjust_to_size(s: &str, rows: usize, columns: usize) -> String { // Colors pub mod colors { - pub const WHITE: (u8, u8, u8) = (238, 238, 238); - pub const GREEN: (u8, u8, u8) = (175, 255, 0); - pub const GRAY: (u8, u8, u8) = (68, 68, 68); - pub const BRIGHT_GRAY: (u8, u8, u8) = (138, 138, 138); - pub const RED: (u8, u8, u8) = (135, 0, 0); - pub const ORANGE: (u8, u8, u8) = (215, 95, 0); - pub const BLACK: (u8, u8, u8) = (0, 0, 0); + pub const WHITE: u8 = 255; + pub const GREEN: u8 = 154; + pub const GRAY: u8 = 238; + pub const BRIGHT_GRAY: u8 = 245; + pub const RED: u8 = 88; + pub const ORANGE: u8 = 166; + pub const BLACK: u8 = 16; } pub fn hex_to_rgb(hex: &str) -> (u8, u8, u8) { @@ -54,30 +54,34 @@ pub fn default_palette() -> Palette { Palette { source: PaletteSource::Default, theme: Theme::Dark, - fg: colors::BRIGHT_GRAY, - bg: colors::GRAY, - black: colors::BLACK, - red: colors::RED, - green: colors::GREEN, - yellow: colors::GRAY, - blue: colors::GRAY, - magenta: colors::GRAY, - cyan: colors::GRAY, - white: colors::WHITE, - orange: colors::ORANGE, + fg: PaletteColor::EightBit(colors::BRIGHT_GRAY), + bg: PaletteColor::EightBit(colors::GRAY), + black: PaletteColor::EightBit(colors::BLACK), + red: PaletteColor::EightBit(colors::RED), + green: PaletteColor::EightBit(colors::GREEN), + yellow: PaletteColor::EightBit(colors::GRAY), + blue: PaletteColor::EightBit(colors::GRAY), + magenta: PaletteColor::EightBit(colors::GRAY), + cyan: PaletteColor::EightBit(colors::GRAY), + white: PaletteColor::EightBit(colors::WHITE), + orange: PaletteColor::EightBit(colors::ORANGE), } } // Dark magic -pub fn detect_theme(bg: (u8, u8, u8)) -> Theme { - let (r, g, b) = bg; - // HSP, P stands for perceived brightness - let hsp: f64 = (0.299 * (r as f64 * r as f64) - + 0.587 * (g as f64 * g as f64) - + 0.114 * (b as f64 * b as f64)) - .sqrt(); - match hsp > 127.5 { - true => Theme::Light, - false => Theme::Dark, +pub fn detect_theme(bg: PaletteColor) -> Theme { + match bg { + PaletteColor::Rgb((r, g, b)) => { + // HSP, P stands for perceived brightness + let hsp: f64 = (0.299 * (r as f64 * r as f64) + + 0.587 * (g as f64 * g as f64) + + 0.114 * (b as f64 * b as f64)) + .sqrt(); + match hsp > 127.5 { + true => Theme::Light, + false => Theme::Dark, + } + } + _ => Theme::Dark, } } diff --git a/src/tests/fakes.rs b/src/tests/fakes.rs index e00de017..684a24a7 100644 --- a/src/tests/fakes.rs +++ b/src/tests/fakes.rs @@ -8,8 +8,8 @@ use std::time::{Duration, Instant}; use crate::os_input_output::OsApi; use crate::tests::possible_tty_inputs::{get_possible_tty_inputs, Bytes}; -use crate::utils::shared::colors; -use zellij_tile::data::{Palette, PaletteSource, Theme}; +use crate::utils::shared::default_palette; +use zellij_tile::data::Palette; use crate::tests::utils::commands::{QUIT, SLEEP}; @@ -75,7 +75,6 @@ pub struct FakeInputOutput { last_snapshot_time: Arc<Mutex<Instant>>, should_trigger_sigwinch: Arc<(Mutex<bool>, Condvar)>, sigwinch_event: Option<PositionAndSize>, - palette: Arc<Mutex<Palette>>, } impl FakeInputOutput { @@ -84,21 +83,6 @@ impl FakeInputOutput { let last_snapshot_time = Arc::new(Mutex::new(Instant::now())); let stdout_writer = FakeStdoutWriter::new(last_snapshot_time.clone()); win_sizes.insert(0, winsize); // 0 is the current terminal - let palette: Palette = Palette { - source: PaletteSource::Default, - theme: Theme::Dark, - fg: colors::BRIGHT_GRAY, - bg: colors::GRAY, - black: colors::BLACK, - red: colors::RED, - green: colors::GREEN, - yellow: colors::GRAY, - blue: colors::GRAY, - magenta: colors::GRAY, - cyan: colors::GRAY, - white: colors::WHITE, - orange: colors::ORANGE, - }; FakeInputOutput { read_buffers: Arc::new(Mutex::new(HashMap::new())), stdin_writes: Arc::new(Mutex::new(HashMap::new())), @@ -111,7 +95,6 @@ impl FakeInputOutput { possible_tty_inputs: get_possible_tty_inputs(), should_trigger_sigwinch: Arc::new((Mutex::new(false), Condvar::new())), sigwinch_event: None, - palette: Arc::new(Mutex::new(palette)), } } pub fn with_tty_inputs(mut self, tty_inputs: HashMap<u16, Bytes>) -> Self { @@ -255,21 +238,6 @@ impl OsApi for FakeInputOutput { } } fn load_palette(&self) -> Palette { - let palette: Palette = Palette { - source: PaletteSource::Default, - theme: Theme::Dark, - fg: colors::BRIGHT_GRAY, - bg: colors::GRAY, - black: colors::BLACK, - red: colors::RED, - green: colors::GREEN, - yellow: colors::GRAY, - blue: colors::GRAY, - magenta: colors::GRAY, - cyan: colors::GRAY, - white: colors::WHITE, - orange: colors::ORANGE, - }; - palette + default_palette() } } |
