From 93ec0abd201c5d4d9a319acdacdab73332b1cee2 Mon Sep 17 00:00:00 2001 From: Thomas Le Duc Date: Sat, 3 Sep 2022 20:44:45 +0200 Subject: [PATCH] Refactor to utilize from_hex color method --- Cargo.lock | 6 +++--- Cargo.toml | 5 ++--- src/main.rs | 6 +++--- src/xcolor.rs | 54 +++++++++++++++++++++------------------------------ 4 files changed, 30 insertions(+), 41 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2f63a4b..9df8047 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -266,13 +266,14 @@ dependencies = [ [[package]] name = "cnx" version = "0.3.0" -source = "git+https://github.com/mjkillough/cnx?rev=0f0406d750e583f6632a1d42945902ccd00379c4#0f0406d750e583f6632a1d42945902ccd00379c4" +source = "git+https://github.com/thled/cnx#74b92e21b32d8e7550c60393904b1391305dc075" dependencies = [ "anyhow", "async-stream", "cairo-rs", "cairo-sys-rs", "chrono", + "colors-transform", "futures", "lazy_static", "ordered-float", @@ -287,7 +288,7 @@ dependencies = [ [[package]] name = "cnx-contrib" version = "0.1.0" -source = "git+https://github.com/mjkillough/cnx?rev=0f0406d750e583f6632a1d42945902ccd00379c4#0f0406d750e583f6632a1d42945902ccd00379c4" +source = "git+https://github.com/thled/cnx#74b92e21b32d8e7550c60393904b1391305dc075" dependencies = [ "alsa", "anyhow", @@ -1402,7 +1403,6 @@ dependencies = [ "anyhow", "cnx", "cnx-contrib", - "colors-transform", "xrdb", ] diff --git a/Cargo.toml b/Cargo.toml index 672371f..a2c034e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,8 +4,7 @@ version = "1.0.0" edition = "2021" [dependencies] -cnx = { git = "https://github.com/mjkillough/cnx", rev = "0f0406d750e583f6632a1d42945902ccd00379c4" } -cnx-contrib = { git = "https://github.com/mjkillough/cnx", rev = "0f0406d750e583f6632a1d42945902ccd00379c4" } +cnx = { git = "https://github.com/thled/cnx" } +cnx-contrib = { git = "https://github.com/thled/cnx" } anyhow = "1.0.62" xrdb = "0.1.1" -colors-transform = "0.2.11" diff --git a/src/main.rs b/src/main.rs index cec7385..040dc1f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,12 +36,12 @@ fn window_title() -> ActiveWindowTitle { fn cpu() -> Result { let render = Box::new(|load| { - let mut color = Color::white().to_hex(); + let mut color = xcolor::foreground().to_hex(); if load > 10 { - color = Color::yellow().to_hex(); + color = xcolor::yellow().to_hex(); } if load > 50 { - color = Color::red().to_hex(); + color = xcolor::red().to_hex(); } format!("CPU {}%", color, load) }); diff --git a/src/xcolor.rs b/src/xcolor.rs index 0a5f313..59ad4f3 100644 --- a/src/xcolor.rs +++ b/src/xcolor.rs @@ -1,47 +1,37 @@ use cnx::text::Color; -use colors_transform::{Color as ColorTransform, Rgb}; use xrdb::Colors; -fn read_xresources_color(color_code: usize) -> Color { - let xcolors = thledbar_colors_from_xresources(); - let hex = xcolors.colors[color_code].as_ref().unwrap(); - let rgb = Rgb::from_hex_str(hex).unwrap(); - - Color::from_rgb( - rgb.get_red() as u8, - rgb.get_green() as u8, - rgb.get_blue() as u8, - ) +pub fn background() -> Color { + let hex = xcolors().bg.unwrap(); + + Color::from_hex(&hex) } -fn thledbar_colors_from_xresources() -> Colors { - Colors::new("thledbar").unwrap() +pub fn foreground() -> Color { + let hex = xcolors().fg.unwrap(); + + Color::from_hex(&hex) +} + +pub fn red() -> Color { + read_xresources_color(1) } pub fn blue() -> Color { read_xresources_color(4) } -pub fn background() -> Color { - let xcolors = thledbar_colors_from_xresources(); - let hex = xcolors.bg.unwrap(); - let rgb = Rgb::from_hex_str(&hex).unwrap(); - - Color::from_rgb( - rgb.get_red() as u8, - rgb.get_green() as u8, - rgb.get_blue() as u8, - ) +pub fn yellow() -> Color { + read_xresources_color(3) +} + +fn xcolors() -> Colors { + Colors::new("thledbar").unwrap() } -pub(crate) fn foreground() -> Color { - let xcolors = thledbar_colors_from_xresources(); - let hex = xcolors.fg.unwrap(); - let rgb = Rgb::from_hex_str(&hex).unwrap(); +fn read_xresources_color(color_code: usize) -> Color { + let xcolor = xcolors(); + let hex = xcolor.colors[color_code].as_ref().unwrap(); - Color::from_rgb( - rgb.get_red() as u8, - rgb.get_green() as u8, - rgb.get_blue() as u8, - ) + Color::from_hex(hex) }