Rgb
An sRGB color with 8 bits per channel.
Built with Rgb::new or parsed from text with Rgb::parse (#rgb, #rrggbb,
rgb(r, g, b) or one of the 17 basic CSS color names). Display writes the #rrggbb form.
Import
Section titled “Import”Cargo feature color (enabled by default). To compile only this module:
or in Cargo.toml:
Definition
Section titled “Definition”Examples
Section titled “Examples”Methods
Section titled “Methods”Builds a color from its three channels.
Parameters
| Parameter | Type | Description |
|---|---|---|
r | u8 | Red, 0..=255. |
g | u8 | Green, 0..=255. |
b | u8 | Blue, 0..=255. |
Returns
Self — The color.
Parses a color written as #rgb, #rrggbb, rgb(r, g, b) or a basic CSS color name.
Surrounding whitespace is ignored, hex digits and names are case-insensitive. #rgb
repeats each digit (#f80 is #ff8800). In rgb(...) the three whole numbers (0..=255)
may be separated by commas or spaces. The names are the 17 basic CSS keywords: black,
silver, gray, white, maroon, red, purple, fuchsia, green, lime, olive,
yellow, navy, blue, teal, aqua and orange.
Parameters
| Parameter | Type | Description |
|---|---|---|
text | &str | The color to parse. |
Returns
Result<Self, ParseColorError>
Errors
A ParseColorError for an empty text, a # color with a bad length or digit, a
malformed or out-of-range rgb(...), or an unknown name.
The red channel.
Returns
u8 — A value from 0 to 255.
The green channel.
Returns
u8 — A value from 0 to 255.
The blue channel.
Returns
u8 — A value from 0 to 255.
to_hex
Section titled “to_hex”The #rrggbb form, in lower case.
Returns
String — For instance "#ff8800".
to_hsl
Section titled “to_hsl”The same color as hue, saturation and lightness.
Returns
Hsl — The Hsl value. Converting it back with Hsl::to_rgb gives this color again.
luminance
Section titled “luminance”The WCAG relative luminance: 0.0 for black, 1.0 for white.
The channels are linearized from sRGB and weighted by how bright the eye finds each (0.2126 red, 0.7152 green, 0.0722 blue).
Returns
f64 — A value from 0.0 to 1.0.
lighten
Section titled “lighten”Moves the color towards white.
Parameters
| Parameter | Type | Description |
|---|---|---|
amount | f64 | How far to go, from 0.0 (unchanged) to 1.0 (white); out-of-range values are clamped. |
Returns
Self — The lighter color.
darken
Section titled “darken”Moves the color towards black.
Parameters
| Parameter | Type | Description |
|---|---|---|
amount | f64 | How far to go, from 0.0 (unchanged) to 1.0 (black); out-of-range values are clamped. |
Returns
Self — The darker color.
Error type: ParseColorError
Section titled “Error type: ParseColorError”Why a string could not be parsed as a color.
