From a02f4048320e5bb5b93759e27b73143aca0a749c Mon Sep 17 00:00:00 2001 From: Frieder Hannenheim Date: Sun, 11 May 2025 15:55:46 +0200 Subject: [PATCH] Use color configuration options --- src/core/graphics.rs | 49 +++++++++++++++++++++------------------ src/wayland/dispatcher.rs | 2 +- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/core/graphics.rs b/src/core/graphics.rs index 5cbd273..086f338 100644 --- a/src/core/graphics.rs +++ b/src/core/graphics.rs @@ -3,6 +3,8 @@ * Copyright (c) 2024, Richard Acayan. All rights reserved. */ +use crate::core::Configuration; +use crate::core::KeyboardColors; use crate::core::Layout; use crate::core::button::ModState; use crate::core::layout::Key; @@ -13,6 +15,7 @@ use imgref::ImgRefMut; use imgref::ImgVec; use rgb::alt::BGR; use rgb::alt::BGRA; +use rgb::ComponentMap; use std::collections::HashMap; use std::fs::File; use std::io::Read; @@ -89,6 +92,8 @@ pub struct Graphics { x_scale: f64, y_scale: f64, + + colors: KeyboardColors, } struct Label<'a> { @@ -162,7 +167,7 @@ const ANCHORS: [(Anchor, Anchor); 8] = [ ]; impl Graphics { - pub fn new(disp: D) -> Graphics + pub fn new(disp: D, cfg: &Configuration) -> Graphics { let ft = unsafe { let mut ft = std::ptr::null_mut(); @@ -211,6 +216,8 @@ impl Graphics { x_scale: 0.0, y_scale: 0.0, + + colors: cfg.colors().clone(), } } @@ -488,49 +495,45 @@ impl Graphics { } fn keypart_to_color(part: &Part, sublabel: bool, - mod_state: &[ModState]) -> BGR + mod_state: &[ModState], colors: &KeyboardColors) -> BGR { - let color_locked = BGR { r: 0.0, g: 1.0, b: 0.1, }; - let color_pressed = BGR { r: 0.0, g: 0.6, b: 1.0, }; - let color_label = BGR { r: 1.0, g: 1.0, b: 1.0, }; - let color_sublabel = BGR { r: 0.7, g: 0.7, b: 0.7, }; - let modifier = part.modifier_id(); if modifier != 0 { let modifier = modifier - 1; if mod_state[modifier] == ModState::Locked || mod_state[modifier] == ModState::HeldLocked || mod_state[modifier] == ModState::HeldLockedPressed { - return color_locked; + return colors.label_locked; } else if mod_state[modifier] != ModState::Released { - return color_pressed; + return colors.label_pressed; } } if part.presses() != 0 { - return color_pressed; + return colors.label_locked; } if sublabel { - return color_sublabel; + return colors.sublabel; } let label = part.display_label(); let res = LABELS.binary_search_by_key(&label, |&Label { label, .. }| label); if let Ok(idx) = res { if LABELS[idx].secondary { - return color_sublabel; + return colors.sublabel; } } - color_label + colors.label } fn draw_key(labels: &HashMap, sublabels: &HashMap, x_scale: f64, y_scale: f64, img: &mut ImgRefMut>, - key: &Key, mod_state: &[ModState]) + key: &Key, mod_state: &[ModState], + colors: &KeyboardColors) { let x1 = (key.x1 * x_scale) as usize; let y1 = (key.y1 * y_scale) as usize; @@ -554,21 +557,21 @@ impl Graphics { } if pressed { - fill_image(&mut keyimg, BGRA { r: 51, g: 51, b: 51, a: 0, }); + fill_image(&mut keyimg, colors.keycap_pressed); } else { - fill_image(&mut keyimg, BGRA { r: 0, g: 0, b: 0, a: 0, }); + fill_image(&mut keyimg, colors.keycap); } - let color = Self::keypart_to_color(&key.parts[0], false, mod_state); + let color = Self::keypart_to_color(&key.parts[0], false, mod_state, colors); let label = key.parts[0].display_label(); Self::draw_label_part(labels, Anchor::Center, Anchor::Center, - &mut keyimg, label, color); + &mut keyimg, label, color.map(|c| c as f32 / 255.)); for i in 0..8 { - let color = Self::keypart_to_color(&key.parts[i + 1], true, mod_state); + let color = Self::keypart_to_color(&key.parts[i + 1], true, mod_state, colors); let label = key.parts[i + 1].display_label(); Self::draw_label_part(sublabels, ANCHORS[i].0, ANCHORS[i].1, - &mut keyimg, label, color); + &mut keyimg, label, color.map(|c| c as f32 / 255.)); } } @@ -583,7 +586,7 @@ impl Graphics { Self::draw_key(&self.labels, &self.sublabels, self.x_scale, self.y_scale, - &mut img, key, mod_state); + &mut img, key, mod_state, &self.colors); self.disp.end(x1, y1, x2 - x1, y2 - y1); } @@ -608,13 +611,13 @@ impl Graphics { { let mut img = self.disp.begin(); - fill_image(&mut img, BGRA { r: 0, g: 0, b: 0, a: 0, }); + fill_image(&mut img, self.colors.background); for row in layout.rows() { for key in row { Self::draw_key(&self.labels, &self.sublabels, self.x_scale, self.y_scale, - &mut img, key, mod_state); + &mut img, key, mod_state, &self.colors); } } diff --git a/src/wayland/dispatcher.rs b/src/wayland/dispatcher.rs index 2075a9e..becb0dd 100644 --- a/src/wayland/dispatcher.rs +++ b/src/wayland/dispatcher.rs @@ -102,7 +102,7 @@ impl Dispatcher { let disp = Surface::new(cfg, queue.clone(), &shm, compositor, layer_shell, frac_scale_man, vper)?; - let gfx = Graphics::new(disp); + let gfx = Graphics::new(disp, cfg); let gfx = Mutex::new(gfx); let gfx = Arc::new(gfx);