feat: add gradient color mode

This commit is contained in:
Pihkaal
2024-01-20 00:08:21 +01:00
parent c6b9d857b0
commit 96ec7da7bc
4 changed files with 114 additions and 48 deletions

37
config
View File

@@ -1,37 +0,0 @@
# tlock config file
[general]
# Say goodbye when you press CTRL-C
# Value: true, false
polite=true
# FPS
# Value: int
fps=30
[format]
# Date format
# Value: TODO
date=%Y-%m-%d
[styling]
# Which color mode to use
# Value: "term", "hex" or "ansi"
color_mode=term
# Loaded if color_mode is set to "term"
# Value: 0-15
color_term=7
# Loaded if color_mode is set to "hex"
# Value: 0-255
color_hex=e6ecfe
# Loaded if color_mode is set to "ansi"
# Value: 0-255
color_ansi=100

View File

@@ -3,10 +3,32 @@ use core::panic;
use crossterm::style::Color;
use ini::configparser::ini::Ini;
pub struct ComputableColor {
values: Vec<Color>,
current: usize,
}
impl ComputableColor {
fn from(color: Color) -> ComputableColor {
return ComputableColor {
values: vec![color],
current: 0,
};
}
pub fn update(&mut self) {
self.current = (self.current + 1) % self.values.len();
}
pub fn get_value(&self) -> Color {
return *self.values.get(self.current).unwrap();
}
}
pub struct Config {
pub be_polite: bool,
pub fps: u64,
pub color: Color,
pub color: ComputableColor,
pub time_format: String,
pub date_format: String,
}
@@ -26,21 +48,24 @@ pub fn load_from_file(path: &str) -> Config {
return config;
}
fn load_color(ini: &Ini) -> Color {
fn load_color(ini: &Ini) -> ComputableColor {
let color_mode = ini.get("styling", "color_mode").unwrap();
match color_mode.as_str() {
"term" => {
let color = ini.getint("styling", "color_term").unwrap().unwrap();
return load_term_color(color);
return ComputableColor::from(load_term_color(color));
}
"hex" => {
let color = ini.get("styling", "color_hex").unwrap();
return load_hex_color(&color);
return ComputableColor::from(load_hex_color(&color));
}
"ansi" => {
let color = ini.getint("styling", "color_ansi").unwrap().unwrap();
return load_ansi_color(color);
return ComputableColor::from(load_ansi_color(color));
}
"gradient" => {
return load_gradient(ini);
}
_ => panic!("ERROR: Invalid color mode: {}", color_mode),
}
@@ -68,7 +93,7 @@ fn load_term_color(value: i64) -> Color {
}
}
fn load_hex_color(value: &str) -> Color {
fn parse_hex_color(value: &str) -> (u8, u8, u8) {
// Expand #XXX colors
let value = if value.len() == 3 {
format!(
@@ -92,9 +117,76 @@ fn load_hex_color(value: &str) -> Color {
let g = u8::from_str_radix(&value[2..4], 16).unwrap();
let b = u8::from_str_radix(&value[4..6], 16).unwrap();
return Color::Rgb { r, g, b };
return (r, g, b);
}
fn load_hex_color(value: &str) -> Color {
let rgb = parse_hex_color(value);
return Color::Rgb {
r: rgb.0,
g: rgb.1,
b: rgb.2,
};
}
fn load_ansi_color(value: i64) -> Color {
return Color::AnsiValue(value.try_into().unwrap());
}
fn clamp01(v: f32) -> f32 {
return if v < 0.0 {
0.0
} else if v > 1.0 {
1.0
} else {
v
};
}
fn lerp(a: u8, b: u8, t: f32) -> u8 {
let v = a as f32 + (b as f32 - a as f32) as f32 * clamp01(t);
return v as u8;
}
fn load_gradient(ini: &Ini) -> ComputableColor {
let mut keys = Vec::new();
let mut i = 0;
while let Some(key) = ini.get("gradient", &format!("gradient_key_{}", i)) {
keys.push(parse_hex_color(&key));
i += 1;
}
if ini.getbool("gradient", "gradient_loop").unwrap().unwrap() {
let mut loop_keys = keys.clone();
loop_keys.reverse();
for i in 1..loop_keys.len() {
keys.push(*loop_keys.get(i).unwrap());
}
}
let steps = ini.getuint("gradient", "gradient_steps").unwrap().unwrap();
let mut gradient = Vec::with_capacity(steps.try_into().unwrap());
let step_size = 1.0 / (steps as f32 / (keys.len() as f32 - 1.0));
for i in 0..keys.len() - 1 {
let current = keys.get(i).unwrap();
let next = keys.get(i + 1).unwrap();
let mut t = 0.0;
while t <= 1.0 {
t += step_size;
let r = lerp(current.0, next.0, t);
let g = lerp(current.1, next.1, t);
let b = lerp(current.2, next.2, t);
gradient.push(Color::Rgb { r, g, b });
}
}
return ComputableColor {
values: gradient,
current: 0,
};
}

View File

@@ -25,8 +25,8 @@ time=%H:%M
[styling]
# Which color mode to use
# Value: "term", "hex" or "ansi"
color_mode=term
# Value: "term", "hex", "ansi" or "gradient"
color_mode=gradient
# Loaded if color_mode is set to "term"
# Value: 0-15
@@ -40,3 +40,12 @@ color_hex=e6ecfe
# Value: 0-255
color_ansi=100
[gradient]
gradient_steps=256
gradient_loop=true
gradient_key_0=ff0000
gradient_key_1=00ff00
gradient_key_2=0000ff

View File

@@ -52,7 +52,7 @@ fn main() -> io::Result<()> {
panic!("ERROR: Configuration file not found");
}
let config = config::load_from_file(&config_file.to_str().unwrap());
let mut config = config::load_from_file(&config_file.to_str().unwrap());
let mut stdout = io::stdout();
@@ -85,6 +85,8 @@ fn main() -> io::Result<()> {
// Render
render_frame(&config)?;
config.color.update();
stdout.flush()?;
thread::sleep(Duration::from_millis(1000 / config.fps));
@@ -112,7 +114,7 @@ fn render_frame(config: &Config) -> io::Result<()> {
let text_width = draw_time_width(&time);
let text_height = 5;
let color = config.color;
let color = config.color.get_value();
let x = width / 2 - text_width / 2;
let y = height / 2 - text_height / 2;