Use color configuration options

This commit is contained in:
Frieder Hannenheim 2025-05-11 15:55:46 +02:00 committed by Richard Acayan
parent f6405cad69
commit a02f404832
No known key found for this signature in database
GPG key ID: 0346F4894879DB73
2 changed files with 27 additions and 24 deletions

View file

@ -3,6 +3,8 @@
* Copyright (c) 2024, Richard Acayan. All rights reserved. * Copyright (c) 2024, Richard Acayan. All rights reserved.
*/ */
use crate::core::Configuration;
use crate::core::KeyboardColors;
use crate::core::Layout; use crate::core::Layout;
use crate::core::button::ModState; use crate::core::button::ModState;
use crate::core::layout::Key; use crate::core::layout::Key;
@ -13,6 +15,7 @@ use imgref::ImgRefMut;
use imgref::ImgVec; use imgref::ImgVec;
use rgb::alt::BGR; use rgb::alt::BGR;
use rgb::alt::BGRA; use rgb::alt::BGRA;
use rgb::ComponentMap;
use std::collections::HashMap; use std::collections::HashMap;
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
@ -89,6 +92,8 @@ pub struct Graphics<D: Display> {
x_scale: f64, x_scale: f64,
y_scale: f64, y_scale: f64,
colors: KeyboardColors,
} }
struct Label<'a> { struct Label<'a> {
@ -162,7 +167,7 @@ const ANCHORS: [(Anchor, Anchor); 8] = [
]; ];
impl<D: Display> Graphics<D> { impl<D: Display> Graphics<D> {
pub fn new(disp: D) -> Graphics<D> pub fn new(disp: D, cfg: &Configuration) -> Graphics<D>
{ {
let ft = unsafe { let ft = unsafe {
let mut ft = std::ptr::null_mut(); let mut ft = std::ptr::null_mut();
@ -211,6 +216,8 @@ impl<D: Display> Graphics<D> {
x_scale: 0.0, x_scale: 0.0,
y_scale: 0.0, y_scale: 0.0,
colors: cfg.colors().clone(),
} }
} }
@ -488,49 +495,45 @@ impl<D: Display> Graphics<D> {
} }
fn keypart_to_color(part: &Part, sublabel: bool, fn keypart_to_color(part: &Part, sublabel: bool,
mod_state: &[ModState]) -> BGR<f32> mod_state: &[ModState], colors: &KeyboardColors) -> BGR<u8>
{ {
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(); let modifier = part.modifier_id();
if modifier != 0 { if modifier != 0 {
let modifier = modifier - 1; let modifier = modifier - 1;
if mod_state[modifier] == ModState::Locked if mod_state[modifier] == ModState::Locked
|| mod_state[modifier] == ModState::HeldLocked || mod_state[modifier] == ModState::HeldLocked
|| mod_state[modifier] == ModState::HeldLockedPressed { || mod_state[modifier] == ModState::HeldLockedPressed {
return color_locked; return colors.label_locked;
} else if mod_state[modifier] != ModState::Released { } else if mod_state[modifier] != ModState::Released {
return color_pressed; return colors.label_pressed;
} }
} }
if part.presses() != 0 { if part.presses() != 0 {
return color_pressed; return colors.label_locked;
} }
if sublabel { if sublabel {
return color_sublabel; return colors.sublabel;
} }
let label = part.display_label(); let label = part.display_label();
let res = LABELS.binary_search_by_key(&label, |&Label { label, .. }| label); let res = LABELS.binary_search_by_key(&label, |&Label { label, .. }| label);
if let Ok(idx) = res { if let Ok(idx) = res {
if LABELS[idx].secondary { if LABELS[idx].secondary {
return color_sublabel; return colors.sublabel;
} }
} }
color_label colors.label
} }
fn draw_key(labels: &HashMap<String, TextRaster>, fn draw_key(labels: &HashMap<String, TextRaster>,
sublabels: &HashMap<String, TextRaster>, sublabels: &HashMap<String, TextRaster>,
x_scale: f64, y_scale: f64, x_scale: f64, y_scale: f64,
img: &mut ImgRefMut<BGRA<u8>>, img: &mut ImgRefMut<BGRA<u8>>,
key: &Key, mod_state: &[ModState]) key: &Key, mod_state: &[ModState],
colors: &KeyboardColors)
{ {
let x1 = (key.x1 * x_scale) as usize; let x1 = (key.x1 * x_scale) as usize;
let y1 = (key.y1 * y_scale) as usize; let y1 = (key.y1 * y_scale) as usize;
@ -554,21 +557,21 @@ impl<D: Display> Graphics<D> {
} }
if pressed { if pressed {
fill_image(&mut keyimg, BGRA { r: 51, g: 51, b: 51, a: 0, }); fill_image(&mut keyimg, colors.keycap_pressed);
} else { } 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(); let label = key.parts[0].display_label();
Self::draw_label_part(labels, Anchor::Center, Anchor::Center, 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 { 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(); let label = key.parts[i + 1].display_label();
Self::draw_label_part(sublabels, ANCHORS[i].0, ANCHORS[i].1, 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<D: Display> Graphics<D> {
Self::draw_key(&self.labels, &self.sublabels, Self::draw_key(&self.labels, &self.sublabels,
self.x_scale, self.y_scale, 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); self.disp.end(x1, y1, x2 - x1, y2 - y1);
} }
@ -608,13 +611,13 @@ impl<D: Display> Graphics<D> {
{ {
let mut img = self.disp.begin(); 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 row in layout.rows() {
for key in row { for key in row {
Self::draw_key(&self.labels, &self.sublabels, Self::draw_key(&self.labels, &self.sublabels,
self.x_scale, self.y_scale, self.x_scale, self.y_scale,
&mut img, key, mod_state); &mut img, key, mod_state, &self.colors);
} }
} }

View file

@ -102,7 +102,7 @@ impl Dispatcher {
let disp = Surface::new(cfg, queue.clone(), let disp = Surface::new(cfg, queue.clone(),
&shm, compositor, layer_shell, &shm, compositor, layer_shell,
frac_scale_man, vper)?; frac_scale_man, vper)?;
let gfx = Graphics::new(disp); let gfx = Graphics::new(disp, cfg);
let gfx = Mutex::new(gfx); let gfx = Mutex::new(gfx);
let gfx = Arc::new(gfx); let gfx = Arc::new(gfx);