GPU-accelerated UI toolkit (Vulkan)
git clone https://git.lucas.co/cce-ui.git
refactor(color): use shared hex parsers in internal widgets
Retire the private parse_hex copies in color_selector/treelist/parameters_bg in
favor of crate::color::parse_hex_bytes / parse_hex_rgba_linear.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
src/widget/container/parameters_bg.rs | 11 +----------
src/widget/container/treelist.rs | 17 +----------------
src/widget/input/color_selector.rs | 17 +----------------
3 files changed, 3 insertions(+), 42 deletions(-)
diff --git a/src/widget/container/parameters_bg.rs b/src/widget/container/parameters_bg.rs
index 1c0d5fd..2a3f83c 100644
--- a/src/widget/container/parameters_bg.rs
+++ b/src/widget/container/parameters_bg.rs
@@ -289,16 +289,7 @@ fn parse_slider_range(ptype: &str) -> (f32, f32) {
}
fn parse_hex_to_rgb(s: &str) -> Option<[u8; 3]> {
- let s = s.trim_matches(|c| c == '"' || c == '\'' || c == ' ');
- let s = s.trim_start_matches('#');
- if s.len() == 6 || s.len() == 8 {
- let r = u8::from_str_radix(&s[0..2], 16).ok()?;
- let g = u8::from_str_radix(&s[2..4], 16).ok()?;
- let b = u8::from_str_radix(&s[4..6], 16).ok()?;
- Some([r, g, b])
- } else {
- None
- }
+ crate::color::parse_hex_bytes(s).map(|[r, g, b, _]| [r, g, b])
}
fn parse_spinbox_range(ptype: &str) -> (i32, i32, i32) {
diff --git a/src/widget/container/treelist.rs b/src/widget/container/treelist.rs
index 7adfdb4..215f08d 100644
--- a/src/widget/container/treelist.rs
+++ b/src/widget/container/treelist.rs
@@ -1566,22 +1566,7 @@ impl TreeList {
}
fn parse_hex_f32(s: &str) -> Option<[f32; 4]> {
- let s = s.trim_matches(|c| c == '"' || c == '\'' || c == ' ');
- let s = s.trim_start_matches('#');
- if s.len() == 6 {
- let r = crate::color::srgb_to_linear(u8::from_str_radix(&s[0..2], 16).ok()? as f32 / 255.0);
- let g = crate::color::srgb_to_linear(u8::from_str_radix(&s[2..4], 16).ok()? as f32 / 255.0);
- let b = crate::color::srgb_to_linear(u8::from_str_radix(&s[4..6], 16).ok()? as f32 / 255.0);
- Some([r, g, b, 1.0])
- } else if s.len() == 8 {
- let r = crate::color::srgb_to_linear(u8::from_str_radix(&s[0..2], 16).ok()? as f32 / 255.0);
- let g = crate::color::srgb_to_linear(u8::from_str_radix(&s[2..4], 16).ok()? as f32 / 255.0);
- let b = crate::color::srgb_to_linear(u8::from_str_radix(&s[4..6], 16).ok()? as f32 / 255.0);
- let a = u8::from_str_radix(&s[6..8], 16).ok()? as f32 / 255.0;
- Some([r, g, b, a])
- } else {
- None
- }
+ crate::color::parse_hex_rgba_linear(s)
}
unsafe impl Send for TreeList {}
diff --git a/src/widget/input/color_selector.rs b/src/widget/input/color_selector.rs
index e3666a3..0435e9e 100644
--- a/src/widget/input/color_selector.rs
+++ b/src/widget/input/color_selector.rs
@@ -535,22 +535,7 @@ impl Drop for ColorSelector {
}
fn parse_hex(s: &str) -> Option<[u8; 4]> {
- let s = s.trim_matches(|c| c == '"' || c == '\'' || c == ' ');
- let s = s.trim_start_matches('#');
- if s.len() == 6 {
- let r = u8::from_str_radix(&s[0..2], 16).ok()?;
- let g = u8::from_str_radix(&s[2..4], 16).ok()?;
- let b = u8::from_str_radix(&s[4..6], 16).ok()?;
- Some([r, g, b, 255])
- } else if s.len() == 8 {
- let r = u8::from_str_radix(&s[0..2], 16).ok()?;
- let g = u8::from_str_radix(&s[2..4], 16).ok()?;
- let b = u8::from_str_radix(&s[4..6], 16).ok()?;
- let a = u8::from_str_radix(&s[6..8], 16).ok()?;
- Some([r, g, b, a])
- } else {
- None
- }
+ crate::color::parse_hex_bytes(s)
}
impl Control for ColorSelector {}