Skip to content

Hsb

A color as hue, saturation and brightness (also called HSV), the model of design tool color pickers. Unlike Hsl lightness, brightness 1.0 is the pure color, not white.

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

use helpers4::color::Hsb;

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.11", default-features = false, features = ["color"] }
pub struct Hsb { /* private fields */ }
use helpers4::color::{Hsb, Rgb};

assert_eq!(Hsb::new(0.0, 1.0, 1.0).to_rgb(), Rgb::new(255, 0, 0));
assert_eq!(Rgb::new(0, 128, 128).to_hsb(), Hsb::new(180.0, 1.0, 128.0 / 255.0));
pub fn new(h: f64, s: f64, b: f64) -> Self

Builds a color, normalizing the values: the hue wraps around 360 degrees, saturation and brightness 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).
bf64The brightness, 0.0 (black) to 1.0 (the pure color).

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 b(self) -> f64

The brightness.

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/hsb.rs