srdusr
aboutsummaryrefslogtreecommitdiffstats
path: root/crates/core/src/theme.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/core/src/theme.rs')
-rw-r--r--crates/core/src/theme.rs22
1 files changed, 22 insertions, 0 deletions
diff --git a/crates/core/src/theme.rs b/crates/core/src/theme.rs
index 882326f..4dbf96d 100644
--- a/crates/core/src/theme.rs
+++ b/crates/core/src/theme.rs
@@ -194,6 +194,16 @@ pub fn parse_hex_color(s: &str) -> Option<(u8, u8, u8)> {
Some((r, g, b))
}
+/// [`parse_hex_color`]'s exact inverse - `#rrggbb`, lowercase, always six
+/// hex digits (`{:02x}` per channel, so a channel below `0x10` doesn't
+/// collapse to a five-character string). Exists for settings readback: a
+/// caller reading `border_color` back over IPC should get the identical
+/// string shape `srd set border_color` itself accepts, not a different
+/// representation of the same colour.
+pub fn format_hex_color(rgb: (u8, u8, u8)) -> String {
+ format!("#{:02x}{:02x}{:02x}", rgb.0, rgb.1, rgb.2)
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -216,6 +226,18 @@ mod tests {
}
#[test]
+ fn format_hex_color_round_trips_through_parse_hex_color() {
+ for rgb in [(0x88, 0xc0, 0xd0), (0, 0, 0), (0xff, 0xff, 0xff), (0x05, 0x0a, 0x0f)] {
+ assert_eq!(parse_hex_color(&format_hex_color(rgb)), Some(rgb));
+ }
+ }
+
+ #[test]
+ fn format_hex_color_pads_low_channel_values() {
+ assert_eq!(format_hex_color((0x05, 0x0a, 0x0f)), "#050a0f");
+ }
+
+ #[test]
fn default_matches_the_legacy_hardcoded_nord_palette() {
let t = ThemeConfig::default();
assert_eq!(t.titlebar_bg, (0x2e, 0x34, 0x40));