Skip to main content

Rgba

Struct Rgba 

Source
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]);

Fields§

§r: u8

Red component (0-255).

§g: u8

Green component (0-255).

§b: u8

Blue component (0-255).

§a: u8

Alpha component (0-255, where 255 is fully opaque).

Implementations§

Source§

impl Rgba

Source

pub const fn rgb(r: u8, g: u8, b: u8) -> Self

Create an opaque color (alpha = 255).

Source

pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self

Create a color with explicit alpha.

Source

pub fn from_f32(r: f32, g: f32, b: f32, a: f32) -> Self

Create a color from floating-point components in the 0.0..=1.0 range.

Values are clamped to 0.0..=1.0 before conversion.

Source

pub fn to_f32_array(&self) -> [f32; 4]

Convert to [r, g, b, a] in the 0.0..=1.0 range (for toolkit interop).

Source

pub fn to_f32_tuple(&self) -> (f32, f32, f32, f32)

Convert to (r, g, b, a) tuple in the 0.0..=1.0 range.

Trait Implementations§

Source§

impl Clone for Rgba

Source§

fn clone(&self) -> Rgba

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Rgba

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Rgba

Source§

fn default() -> Rgba

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Rgba

Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Rgba

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl FromStr for Rgba

Source§

type Err = ParseColorError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for Rgba

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Rgba

Source§

fn eq(&self, other: &Rgba) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Rgba

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl Copy for Rgba

Source§

impl Eq for Rgba

Source§

impl StructuralPartialEq for Rgba

Auto Trait Implementations§

§

impl Freeze for Rgba

§

impl RefUnwindSafe for Rgba

§

impl Send for Rgba

§

impl Sync for Rgba

§

impl Unpin for Rgba

§

impl UnsafeUnpin for Rgba

§

impl UnwindSafe for Rgba

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,