From e13fb31287ac698551f0ead745889adc00b2cd3e Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Thu, 25 Jan 2024 13:36:52 +0100 Subject: [PATCH] small refactoring in color crate (#6695) Make the code more readable --- crates/color/src/color.rs | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/crates/color/src/color.rs b/crates/color/src/color.rs index 8529f3bc5f..43bbc1c032 100644 --- a/crates/color/src/color.rs +++ b/crates/color/src/color.rs @@ -59,10 +59,7 @@ pub fn hex_to_hsla(s: &str) -> Result { // Expand shorthand formats #RGB and #RGBA to #RRGGBB and #RRGGBBAA let hex = match hex.len() { - 3 => hex - .chars() - .map(|c| c.to_string().repeat(2)) - .collect::(), + 3 => hex.chars().map(|c| c.to_string().repeat(2)).collect(), 4 => { let (rgb, alpha) = hex.split_at(3); let rgb = rgb @@ -80,14 +77,12 @@ pub fn hex_to_hsla(s: &str) -> Result { let hex_val = u32::from_str_radix(&hex, 16).map_err(|_| format!("Invalid hexadecimal string: {}", s))?; - let r = ((hex_val >> 24) & 0xFF) as f32 / 255.0; - let g = ((hex_val >> 16) & 0xFF) as f32 / 255.0; - let b = ((hex_val >> 8) & 0xFF) as f32 / 255.0; - let a = (hex_val & 0xFF) as f32 / 255.0; - - let color = RGBAColor { r, g, b, a }; - - Ok(color) + Ok(RGBAColor { + r: ((hex_val >> 24) & 0xFF) as f32 / 255.0, + g: ((hex_val >> 16) & 0xFF) as f32 / 255.0, + b: ((hex_val >> 8) & 0xFF) as f32 / 255.0, + a: (hex_val & 0xFF) as f32 / 255.0, + }) } // These derives implement to and from palette's color types. @@ -128,8 +123,7 @@ where Rgb: FromColorUnclamped, { fn from_color_unclamped(color: RGBAColor) -> Self { - let srgb = Srgb::new(color.r, color.g, color.b); - Self::from_color_unclamped(srgb) + Self::from_color_unclamped(Srgb::new(color.r, color.g, color.b)) } }