diff options
author | Minijackson <minijackson@riseup.net> | 2023-01-04 09:32:20 +0100 |
---|---|---|
committer | Minijackson <minijackson@riseup.net> | 2023-01-04 09:32:20 +0100 |
commit | 119e437cb0ef543cf4652bbb7700870b49ac9227 (patch) | |
tree | b619e99f3159ecfc860fb19c88119eed88eff11e /core | |
parent | 105524fe60b1434e22cd666e529710db319e3495 (diff) | |
download | diaphragm-119e437cb0ef543cf4652bbb7700870b49ac9227.tar.gz diaphragm-119e437cb0ef543cf4652bbb7700870b49ac9227.zip |
core/colors: ahh hex conversion
Diffstat (limited to 'core')
-rw-r--r-- | core/src/colors.rs | 25 |
1 files changed, 25 insertions, 0 deletions
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 { | |||
31 | pub fn rgba(&self) -> (f64, f64, f64, f64) { | 31 | pub fn rgba(&self) -> (f64, f64, f64, f64) { |
32 | (self.0.red, self.0.green, self.0.blue, self.0.alpha) | 32 | (self.0.red, self.0.green, self.0.blue, self.0.alpha) |
33 | } | 33 | } |
34 | |||
35 | pub fn from_hex(hex: &str) -> Self { | ||
36 | // palette::Srgb::<f64>::from_str(hex) | ||
37 | let color: palette::Srgb<u8> = hex.parse().unwrap(); | ||
38 | let color: palette::Srgba<u8> = color.into(); | ||
39 | // TODO: really? no conversion from u8 -> f64? | ||
40 | let (r, g, b, a) = color.into_components(); | ||
41 | Self::from_rgba( | ||
42 | r as f64 / 255., | ||
43 | g as f64 / 255., | ||
44 | b as f64 / 255., | ||
45 | a as f64 / 255., | ||
46 | ) | ||
47 | } | ||
48 | |||
49 | pub fn to_hex(&self) -> String { | ||
50 | let (r, g, b, a) = self.0.into_components(); | ||
51 | format!( | ||
52 | "#{:02x}{:02x}{:02x}{:02x}", | ||
53 | (r * 255.) as u8, | ||
54 | (g * 255.) as u8, | ||
55 | (b * 255.) as u8, | ||
56 | (a * 255.) as u8 | ||
57 | ) | ||
58 | } | ||
34 | } | 59 | } |