aboutsummaryrefslogtreecommitdiffstats
path: root/src/common/utils
diff options
context:
space:
mode:
authorDenis Maximov <denismaximov98@gmail.com>2021-05-02 13:53:36 +0300
committerGitHub <noreply@github.com>2021-05-02 13:53:36 +0300
commit98d9eacd2221162c60f71b73ea7f40ff7256e225 (patch)
treebad162ac3c59a16723f75fcd32d6d0cc724be09e /src/common/utils
parentdocs(changelog): Improved Config Loading #423 (diff)
parentchore: remove unused dependency from zellij-tile (diff)
downloadsl0ppij-98d9eacd2221162c60f71b73ea7f40ff7256e225.tar.gz
sl0ppij-98d9eacd2221162c60f71b73ea7f40ff7256e225.tar.xz
sl0ppij-98d9eacd2221162c60f71b73ea7f40ff7256e225.zip
Merge pull request #239 from zellij-org/x-colors
X colors
Diffstat (limited to 'src/common/utils')
-rw-r--r--src/common/utils/shared.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/common/utils/shared.rs b/src/common/utils/shared.rs
index d5ba943b..3f336f7b 100644
--- a/src/common/utils/shared.rs
+++ b/src/common/utils/shared.rs
@@ -4,6 +4,9 @@ use std::{iter, str::from_utf8};
use strip_ansi_escapes::strip;
+use colors_transform::{Color, Rgb};
+use zellij_tile::data::Theme;
+
fn ansi_len(s: &str) -> usize {
from_utf8(&strip(s.as_bytes()).unwrap())
.unwrap()
@@ -28,3 +31,36 @@ pub fn adjust_to_size(s: &str, rows: usize, columns: usize) -> String {
.collect::<Vec<_>>()
.join("\n\r")
}
+
+// 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 fn hex_to_rgb(hex: &Option<String>) -> (u8, u8, u8) {
+ let c = hex.clone();
+ let imm_str = &c.unwrap();
+ let hex_str: &str = &imm_str;
+ let rgb = Rgb::from_hex_str(hex_str).unwrap().as_tuple();
+ (rgb.0 as u8, rgb.1 as u8, rgb.2 as u8)
+}
+
+// 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,
+ }
+}