Skip to content

color

Colors without a dependency: parsing, hex, HSL, blending and WCAG contrast.

Rgb is an 8-bit sRGB color that parses from #rgb, #rrggbb, rgb(r, g, b) or a basic CSS name; Hsl converts to and from it; mix blends two colors; contrast_ratio and best_text_color answer “is this text readable on that background”.

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"] }

Import path: helpers4::color.

ItemWhat it does
best_text_colorPicks black or white text, whichever contrasts more with background.
contrast_ratioThe WCAG contrast ratio between two colors, from 1.0 (identical) to 21.0 (black on white).
HslA color as hue, saturation and lightness.
mixBlends two colors channel by channel.
RgbAn sRGB color with 8 bits per channel.

Documented on the page of the helper that returns them.

TypeReturned by
ParseColorErrorRgb