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”.
Install
Section titled “Install”Cargo feature color (enabled by default). To compile only this module:
or in Cargo.toml:
Import path: helpers4::color.
| Item | What it does |
|---|---|
best_text_color | Picks black or white text, whichever contrasts more with background. |
BlendMode | The blend modes of the W3C Compositing and Blending specification, the same ones as CSS mix-blend-mode. |
blend | Composites 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_ratio | The WCAG contrast ratio between two colors, from 1.0 (identical) to 21.0 (black on white). |
Hsb | A color as hue, saturation and brightness (also called HSV), the model of design tool color pickers. |
Hsl | A color as hue, saturation and lightness. |
mix | Blends two colors channel by channel. |
premultiply | Multiplies 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). |
Rgb | An sRGB color with 8 bits per channel. |
Rgba | An sRGB color with 8 bits per channel and an alpha (opacity) channel. |
unpremultiply | Divides the color channels by the alpha, the inverse of premultiply. |
Error types
Section titled “Error types”Documented on the page of the helper that returns them.
| Type | Returned by |
|---|---|
ParseColorError | Rgb, Rgba |
