Rgba
An sRGB color with 8 bits per channel and an alpha (opacity) channel.
Rgb is always opaque; Rgba adds the alpha of #rrggbbaa and rgb(r g b / a). The alpha
is a u8 like the other channels: 255 is opaque, 0 fully transparent. Display writes
the #rrggbb form when opaque and #rrggbbaa otherwise.
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 four channels.
Parameters
| Parameter | Type | Description |
|---|---|---|
r | u8 | Red, 0..=255. |
g | u8 | Green, 0..=255. |
b | u8 | Blue, 0..=255. |
a | u8 | Alpha, 0 (transparent) to 255 (opaque). |
Returns
Self — The color.
Examples
from_rgb
Section titled “from_rgb”Adds an alpha to an opaque color.
Parameters
| Parameter | Type | Description |
|---|---|---|
rgb | Rgb | The color. |
a | u8 | Alpha, 0 (transparent) to 255 (opaque). |
Returns
Self — The color with that alpha.
Examples
Parses a color written as #rgba, #rrggbbaa, rgb(r g b / a), rgb(r, g, b, a),
rgba(...), or any form Rgb::parse reads (which is opaque).
The alpha of a function is a number (0.5) or a percentage (50%); like in CSS, a value
outside 0..=1 is clamped. Surrounding whitespace is ignored, and so is case.
Parameters
| Parameter | Type | Description |
|---|---|---|
text | &str | The color to parse. |
Returns
Result<Self, ParseColorError> — The color.
Errors
A ParseColorError for an empty text, a # color with a bad length or digit, a
malformed or out-of-range function, or an unknown name.
Examples
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.
The alpha channel.
Returns
u8 — A value from 0 (transparent) to 255 (opaque).
opacity
Section titled “opacity”The alpha as an opacity.
Returns
f64 — A value from 0.0 (transparent) to 1.0 (opaque).
Examples
to_rgb
Section titled “to_rgb”The same color without its alpha.
Returns
Rgb — The opaque Rgb.
Examples
to_hex
Section titled “to_hex”The #rrggbb form when opaque, #rrggbbaa otherwise, in lower case.
Returns
String — For instance "#ff8800" or "#ff880080".
Examples
to_css
Section titled “to_css”The CSS form: rgb(r g b) when opaque, rgb(r g b / alpha) otherwise, with the alpha
as an opacity rounded to 3 decimals.
Returns
String — For instance "rgb(255 136 0 / 0.502)".
Examples
Error type: ParseColorError
Section titled “Error type: ParseColorError”Why a string could not be parsed as a color.
