From 119e437cb0ef543cf4652bbb7700870b49ac9227 Mon Sep 17 00:00:00 2001 From: Minijackson Date: Wed, 4 Jan 2023 09:32:20 +0100 Subject: core/colors: ahh hex conversion --- core/src/colors.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/core/src/colors.rs b/core/src/colors.rs index ba78fa3..e69f9d0 100644 --- a/core/src/colors.rs +++ b/core/src/colors.rs @@ -31,4 +31,29 @@ impl Color { pub fn rgba(&self) -> (f64, f64, f64, f64) { (self.0.red, self.0.green, self.0.blue, self.0.alpha) } + + pub fn from_hex(hex: &str) -> Self { + // palette::Srgb::::from_str(hex) + let color: palette::Srgb = hex.parse().unwrap(); + let color: palette::Srgba = color.into(); + // TODO: really? no conversion from u8 -> f64? + let (r, g, b, a) = color.into_components(); + Self::from_rgba( + r as f64 / 255., + g as f64 / 255., + b as f64 / 255., + a as f64 / 255., + ) + } + + pub fn to_hex(&self) -> String { + let (r, g, b, a) = self.0.into_components(); + format!( + "#{:02x}{:02x}{:02x}{:02x}", + (r * 255.) as u8, + (g * 255.) as u8, + (b * 255.) as u8, + (a * 255.) as u8 + ) + } } -- cgit v1.2.3