From 45ae131f7ead96f432f8a3221a027a87278f5c19 Mon Sep 17 00:00:00 2001 From: zhouhang95 <765229842@qq.com> Date: Fri, 26 Jan 2024 13:52:06 +0800 Subject: [PATCH] use-egui-from-hex --- egui_node_graph/src/color_hex_utils.rs | 94 -------------------------- egui_node_graph/src/editor_ui.rs | 21 +++--- egui_node_graph/src/lib.rs | 1 - egui_node_graph/src/node_finder.rs | 10 +-- 4 files changed, 15 insertions(+), 111 deletions(-) delete mode 100644 egui_node_graph/src/color_hex_utils.rs diff --git a/egui_node_graph/src/color_hex_utils.rs b/egui_node_graph/src/color_hex_utils.rs deleted file mode 100644 index 0479a8c..0000000 --- a/egui_node_graph/src/color_hex_utils.rs +++ /dev/null @@ -1,94 +0,0 @@ -use egui::Color32; - -/// Converts a hex string with a leading '#' into a egui::Color32. -/// - The first three channels are interpreted as R, G, B. -/// - The fourth channel, if present, is used as the alpha value. -/// - Both upper and lowercase characters can be used for the hex values. -/// -/// *Adapted from: https://docs.rs/raster/0.1.0/src/raster/lib.rs.html#425-725. -/// Credit goes to original authors.* -pub fn color_from_hex(hex: &str) -> Result { - // Convert a hex string to decimal. Eg. "00" -> 0. "FF" -> 255. - fn _hex_dec(hex_string: &str) -> Result { - match u8::from_str_radix(hex_string, 16) { - Ok(o) => Ok(o), - Err(e) => Err(format!("Error parsing hex: {}", e)), - } - } - - if hex.len() == 9 && hex.starts_with('#') { - // #FFFFFFFF (Red Green Blue Alpha) - return Ok(Color32::from_rgba_premultiplied( - _hex_dec(&hex[1..3])?, - _hex_dec(&hex[3..5])?, - _hex_dec(&hex[5..7])?, - _hex_dec(&hex[7..9])?, - )); - } else if hex.len() == 7 && hex.starts_with('#') { - // #FFFFFF (Red Green Blue) - return Ok(Color32::from_rgb( - _hex_dec(&hex[1..3])?, - _hex_dec(&hex[3..5])?, - _hex_dec(&hex[5..7])?, - )); - } - - Err(format!( - "Error parsing hex: {}. Example of valid formats: #FFFFFF or #ffffffff", - hex - )) -} - -/// Converts a Color32 into its canonical hexadecimal representation. -/// - The color string will be preceded by '#'. -/// - If the alpha channel is completely opaque, it will be ommitted. -/// - Characters from 'a' to 'f' will be written in lowercase. -#[allow(dead_code)] -pub fn color_to_hex(color: Color32) -> String { - if color.a() < 255 { - format!( - "#{:02x?}{:02x?}{:02x?}{:02x?}", - color.r(), - color.g(), - color.b(), - color.a() - ) - } else { - format!("#{:02x?}{:02x?}{:02x?}", color.r(), color.g(), color.b()) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - pub fn test_color_from_and_to_hex() { - assert_eq!( - color_from_hex("#00ff00").unwrap(), - Color32::from_rgb(0, 255, 0) - ); - assert_eq!( - color_from_hex("#5577AA").unwrap(), - Color32::from_rgb(85, 119, 170) - ); - assert_eq!( - color_from_hex("#E2e2e277").unwrap(), - Color32::from_rgba_premultiplied(226, 226, 226, 119) - ); - assert!(color_from_hex("abcdefgh").is_err()); - - assert_eq!( - color_to_hex(Color32::from_rgb(0, 255, 0)), - "#00ff00".to_string() - ); - assert_eq!( - color_to_hex(Color32::from_rgb(85, 119, 170)), - "#5577aa".to_string() - ); - assert_eq!( - color_to_hex(Color32::from_rgba_premultiplied(226, 226, 226, 119)), - "#e2e2e277".to_string() - ); - } -} diff --git a/egui_node_graph/src/editor_ui.rs b/egui_node_graph/src/editor_ui.rs index 9a4b816..651c2e8 100644 --- a/egui_node_graph/src/editor_ui.rs +++ b/egui_node_graph/src/editor_ui.rs @@ -1,6 +1,5 @@ use std::collections::HashSet; -use crate::color_hex_utils::*; use crate::utils::ColorUtils; use super::*; @@ -593,11 +592,11 @@ where let background_color; let text_color; if ui.visuals().dark_mode { - background_color = color_from_hex("#3f3f3f").unwrap(); - text_color = color_from_hex("#fefefe").unwrap(); + background_color = Color32::from_hex("#3f3f3f").unwrap(); + text_color = Color32::from_hex("#fefefe").unwrap(); } else { - background_color = color_from_hex("#ffffff").unwrap(); - text_color = color_from_hex("#505050").unwrap(); + background_color = Color32::from_hex("#ffffff").unwrap(); + text_color = Color32::from_hex("#505050").unwrap(); } ui.visuals_mut().widgets.noninteractive.fg_stroke = @@ -1013,22 +1012,22 @@ where let dark_mode = ui.visuals().dark_mode; let color = if resp.clicked() { if dark_mode { - color_from_hex("#ffffff").unwrap() + Color32::from_hex("#ffffff").unwrap() } else { - color_from_hex("#000000").unwrap() + Color32::from_hex("#000000").unwrap() } } else if resp.hovered() { if dark_mode { - color_from_hex("#dddddd").unwrap() + Color32::from_hex("#dddddd").unwrap() } else { - color_from_hex("#222222").unwrap() + Color32::from_hex("#222222").unwrap() } } else { #[allow(clippy::collapsible_else_if)] if dark_mode { - color_from_hex("#aaaaaa").unwrap() + Color32::from_hex("#aaaaaa").unwrap() } else { - color_from_hex("#555555").unwrap() + Color32::from_hex("#555555").unwrap() } }; let stroke = Stroke { diff --git a/egui_node_graph/src/lib.rs b/egui_node_graph/src/lib.rs index a3d562e..37e2750 100644 --- a/egui_node_graph/src/lib.rs +++ b/egui_node_graph/src/lib.rs @@ -43,5 +43,4 @@ pub use traits::*; mod utils; -mod color_hex_utils; mod scale; diff --git a/egui_node_graph/src/node_finder.rs b/egui_node_graph/src/node_finder.rs index c0a6b2f..d2aed46 100644 --- a/egui_node_graph/src/node_finder.rs +++ b/egui_node_graph/src/node_finder.rs @@ -1,6 +1,6 @@ use std::{collections::BTreeMap, marker::PhantomData}; -use crate::{color_hex_utils::*, CategoryTrait, NodeTemplateIter, NodeTemplateTrait}; +use crate::{CategoryTrait, NodeTemplateIter, NodeTemplateTrait}; use egui::*; @@ -42,11 +42,11 @@ where let text_color; if ui.visuals().dark_mode { - background_color = color_from_hex("#3f3f3f").unwrap(); - text_color = color_from_hex("#fefefe").unwrap(); + background_color = Color32::from_hex("#3f3f3f").unwrap(); + text_color = Color32::from_hex("#fefefe").unwrap(); } else { - background_color = color_from_hex("#fefefe").unwrap(); - text_color = color_from_hex("#3f3f3f").unwrap(); + background_color = Color32::from_hex("#fefefe").unwrap(); + text_color = Color32::from_hex("#3f3f3f").unwrap(); } ui.visuals_mut().widgets.noninteractive.fg_stroke = Stroke::new(2.0, text_color);