-
Hey! I am new to Rust & to this crate and I can't find how to easily create a HSL color and then print it as an RGB hex ( Maybe it's just me but I feel like the documentation is super hard to understand when manipulating colors should be pretty straightforward, but I guess I am just missing something. Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Hello! All the bits and pieces are there, but maybe not all put front and center. I would like to improve that. Until that happens, here's a recipe for HSL to HEX RGB: use palette::{Hsl, Srgb, FromColor};
// Assuming f32 input, but depends on your case.
let hsl = Hsl::new(123.0f32, 0.5, 0.8);
// Convert to RGB and then convert the float components to u8
// The type of RGB is perhaps not important in your case, but sRGB is the typical output.
let rgb_u8: Srgb<u8> = Srgb::from_color(hsl).into_format();
// The hex conversion is a formatting option. The `:x` means hexadecimal formatting.
// The # is not a mandatory part of the RGB format, so has to be added by us.
let rgb_hex = format!("#{rgb_u8:x}"); I didn't test run this, so let me know if there are any mistakes. Here's also a preview of an updated |
Beta Was this translation helpful? Give feedback.
Hello! All the bits and pieces are there, but maybe not all put front and center. I would like to improve that. Until that happens, here's a recipe for HSL to HEX RGB: