Skip to content

Hsl

A color as hue, saturation and lightness.

The hue is in degrees, 0.0..360.0; saturation and lightness are fractions from 0.0 to 1.0. Hsl::new brings any input into those ranges. Get one from Rgb::to_hsl and go back with Hsl::to_rgb.

use helpers4::color::Hsl;

Cargo feature color (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features color

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.6", default-features = false, features = ["color"] }
pub struct Hsl { /* private fields */ }
use helpers4::color::{Hsl, Rgb};

let teal = Hsl::new(180.0, 1.0, 0.25);
assert_eq!(teal.to_rgb(), Rgb::new(0, 128, 128));
assert_eq!(Hsl::new(-90.0, 2.0, -1.0), Hsl::new(270.0, 1.0, 0.0));
pub fn new(h: f64, s: f64, l: f64) -> Self

Builds a color, normalizing the values: the hue wraps around 360 degrees, saturation and lightness are clamped to 0.0..=1.0, and a NaN counts as 0.0.

Parameters

ParameterTypeDescription
hf64The hue in degrees.
sf64The saturation, 0.0 (grey) to 1.0 (vivid).
lf64The lightness, 0.0 (black) to 1.0 (white).

Returns

Self — The normalized color.

pub fn h(self) -> f64

The hue in degrees.

Returns

f64 — A value in 0.0..360.0.

pub fn s(self) -> f64

The saturation.

Returns

f64 — A value from 0.0 to 1.0.

pub fn l(self) -> f64

The lightness.

Returns

f64 — A value from 0.0 to 1.0.

pub fn to_rgb(self) -> Rgb

The nearest 8-bit sRGB color.

Returns

Rgb — The Rgb color, each channel rounded to the nearest integer.

src/color/hsl.rs