diff options
| author | Daniel Jankowski <daniel.jankowski@rub.de> | 2023-09-02 14:33:40 +0200 |
|---|---|---|
| committer | Daniel Jankowski <daniel.jankowski@rub.de> | 2023-09-02 14:33:40 +0200 |
| commit | cbe8fc773435c03b2174edc9febc3813551957d1 (patch) | |
| tree | 061743db0d202290fdde8da4622689f0ae36bf5d /src/render.rs | |
| parent | chore: refactor renderer (diff) | |
| download | sl0pstatus-cbe8fc773435c03b2174edc9febc3813551957d1.tar.gz sl0pstatus-cbe8fc773435c03b2174edc9febc3813551957d1.tar.xz sl0pstatus-cbe8fc773435c03b2174edc9febc3813551957d1.zip | |
chore: implement hex2rgb instead of crate
Diffstat (limited to 'src/render.rs')
| -rw-r--r-- | src/render.rs | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/src/render.rs b/src/render.rs index 561e2ab..659c48d 100644 --- a/src/render.rs +++ b/src/render.rs @@ -1,4 +1,4 @@ -use std::{collections::BTreeMap, sync::Arc}; +use std::{collections::BTreeMap, num::ParseIntError, sync::Arc}; use ansi_term::{Colour, Colour::Fixed, Colour::RGB, Style}; @@ -106,11 +106,30 @@ impl Default for FormattedPart { } } +fn hex_to_rgb(s: &str) -> Result<Vec<u8>, ParseIntError> { + (0..s.len()) + .step_by(2) + .map(|i| u8::from_str_radix(&s[i..i + 2], 16)) + .collect() +} + fn parse_color(color: &str) -> Option<Colour> { if color.starts_with('#') { - let rgb = hex_rgb::convert_hexcode_to_rgb(color.to_string()).unwrap(); + let rgb = hex_to_rgb(color.strip_prefix('#').unwrap()); + if rgb.is_err() { + return None; + } + + let rgb = rgb.unwrap(); + if rgb.len() != 3 { + return None; + } - return Some(RGB(rgb.red, rgb.green, rgb.blue)); + return Some(RGB( + *rgb.first().unwrap(), + *rgb.get(1).unwrap(), + *rgb.get(2).unwrap(), + )); } if let Ok(result) = color.parse::<u8>() { |
