pub struct Rgba {
pub r: u8,
pub g: u8,
pub b: u8,
pub a: u8,
}Expand description
An sRGB color with alpha, stored as four u8 components.
All values are in the sRGB color space. When parsing hex strings, alpha defaults to 255 (fully opaque) if omitted.
§Hex Format
Supports parsing from and displaying as hex strings:
#RGB/RGB– 3-digit shorthand (each digit doubled:#abc->#aabbcc)#RGBA/RGBA– 4-digit shorthand with alpha#RRGGBB/RRGGBB– standard 6-digit hex#RRGGBBAA/RRGGBBAA– 8-digit hex with alpha
Display outputs lowercase hex: #rrggbb when alpha is 255,
#rrggbbaa otherwise.
§Examples
use native_theme::Rgba;
// Create an opaque color
let blue = Rgba::rgb(0, 120, 215);
assert_eq!(blue.a, 255);
// Parse from a hex string
let parsed: Rgba = "#3daee9".parse().unwrap();
assert_eq!(parsed.r, 61);
// Convert to f32 array for toolkit interop
let arr = Rgba::rgb(255, 0, 0).to_f32_array();
assert_eq!(arr, [1.0, 0.0, 0.0, 1.0]);§Default
Rgba::default() is transparent black (0, 0, 0, 0), not opaque black.
Use Rgba::rgb(0, 0, 0) for opaque black.
Fields§
§r: u8Red component (0-255).
g: u8Green component (0-255).
b: u8Blue component (0-255).
a: u8Alpha component (0-255, where 255 is fully opaque).
Implementations§
Source§impl Rgba
impl Rgba
Sourcepub fn from_f32(r: f32, g: f32, b: f32, a: f32) -> Rgba
pub fn from_f32(r: f32, g: f32, b: f32, a: f32) -> Rgba
Create a color from floating-point components in the 0.0..=1.0 range.
Values are clamped to 0.0..=1.0 before conversion.
Note: round-trip through from_f32 -> to_f32_array is lossy due to
u8 quantization (e.g., from_f32(0.5, ...) -> r=128 ->
to_f32_array() -> 0.50196…).
Sourcepub fn to_f32_array(&self) -> [f32; 4]
pub fn to_f32_array(&self) -> [f32; 4]
Convert to [r, g, b, a] in the 0.0..=1.0 range (for toolkit interop).
Note: round-trip through from_f32 -> to_f32_array is lossy due to
u8 quantization (256 discrete steps per channel).