Skip to content

color

Colors without a dependency: parsing, hex, CSS output, HSL, alpha, 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.11", default-features = false, features = ["color"] }

Import path: helpers4::color.

ItemWhat it does
best_text_colorPicks black or white text, whichever contrasts more with background.
BlendModeThe blend modes of the W3C Compositing and Blending specification, the same ones as CSS mix-blend-mode.
blendComposites a source color over a backdrop with a blend mode, as the browser does for mix-blend-mode (W3C Compositing and Blending Level 1, source-over).
contrast_ratioThe WCAG contrast ratio between two colors, from 1.0 (identical) to 21.0 (black on white).
HsbA color as hue, saturation and brightness (also called HSV), the model of design tool color pickers.
HslA color as hue, saturation and lightness.
mixBlends two colors channel by channel.
premultiplyMultiplies the color channels by the alpha, the form a renderer composites with (a translucent color then adds to what is underneath without a further multiplication).
RgbAn sRGB color with 8 bits per channel.
RgbaAn sRGB color with 8 bits per channel and an alpha (opacity) channel.
unpremultiplyDivides the color channels by the alpha, the inverse of premultiply.

Documented on the page of the helper that returns them.

TypeReturned by
ParseColorErrorRgb, Rgba