Skip to content

Create HSL color and convert it to Hex RGB string #359

Answered by Ogeon
BlueskyFR asked this question in Q&A
Discussion options

You must be logged in to vote

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!("#{…

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@BlueskyFR
Comment options

@Ogeon
Comment options

Ogeon Oct 6, 2023
Maintainer

@BlueskyFR
Comment options

@Ogeon
Comment options

Ogeon Oct 6, 2023
Maintainer

@Ogeon
Comment options

Ogeon Oct 6, 2023
Maintainer

Answer selected by BlueskyFR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants