Skip to main content

Color

Struct Color 

Source
pub struct Color { /* private fields */ }
Expand description

Compact 32-bit sRGB color with 8-bit red, green, blue, and alpha channels.

Implementations§

Source§

impl Color

Color constructors, packing, and blending operations.

Source

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

Constructs an opaque color from RGB channels.

§Arguments
  • u8 - Red channel.
  • u8 - Green channel.
  • u8 - Blue channel.
§Returns
  • Self - Opaque RGBA color.
Source

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

Constructs a color from RGBA channels.

§Arguments
  • u8 - Red channel.
  • u8 - Green channel.
  • u8 - Blue channel.
  • u8 - Alpha channel.
§Returns
  • Self - Constructed RGBA color.
Source

pub const fn hex(rgb: u32) -> Self

Constructs an opaque color from a packed 0xRRGGBB integer literal.

§Arguments
  • u32 - Packed 24-bit RGB hexadecimal value.
§Returns
  • Self - Opaque RGBA color.
Source

pub const fn hexa(rgba: u32) -> Self

Constructs a color from a packed 0xRRGGBBAA integer literal.

§Arguments
  • u32 - Packed 32-bit RGBA hexadecimal value.
§Returns
  • Self - Constructed RGBA color.
Source

pub const fn mul_channel_u8(a: u8, b: u8) -> u8

Computes exact rounded integer multiplication (a * b + 127) / 255 for 8-bit channels without division.

§Arguments
  • u8 - First 8-bit channel operand.
  • u8 - Second 8-bit channel operand.
§Returns
  • u8 - Exact rounded 8-bit product.
Source

pub fn with_alpha(&self, alpha: u8) -> Self

Returns a copy of this color with its alpha replaced by alpha.

§Arguments
  • &Self - Source color.
  • u8 - Replacement alpha channel.
§Returns
  • Self - Color with updated alpha channel.
Source

pub const fn multiply_alpha_u8(&self, alpha_scale: u8) -> Self

Returns a copy of this color with alpha scaled by an 8-bit factor alpha_scale in 0..=255.

§Arguments
  • &Self - Source color.
  • u8 - 8-bit alpha scale factor (0 = transparent, 255 = unchanged).
§Returns
  • Self - Color with exact fixed-point scaled alpha channel.
Source

pub fn multiply_alpha(&self, opacity: f32) -> Self

Returns a copy of this color with alpha multiplied by opacity in [0.0, 1.0].

§Arguments
  • &Self - Source color.
  • f32 - Opacity factor clamped to [0.0, 1.0].
§Returns
  • Self - Color with scaled alpha channel.
Source

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

Converts the color to normalized [r, g, b, a] floating-point components in [0.0, 1.0].

§Arguments
  • &Self - Source color.
§Returns
  • [f32; 4] - Normalized RGBA components for GPU uniforms and vertex attributes.
Source

pub fn to_rgba_bytes(&self) -> [u8; 4]

Packs the color into little-endian RGBA8 bytes [r, g, b, a].

§Arguments
  • &Self - Source color.
§Returns
  • [u8; 4] - RGBA byte array.
Source

pub fn to_bgra_bytes(&self) -> [u8; 4]

Packs the color into BGRA8 bytes [b, g, r, a] used by Win32 GDI and Cocoa/Metal.

§Arguments
  • &Self - Source color.
§Returns
  • [u8; 4] - BGRA byte array.
Source

pub const fn lerp_u8(&self, other: Self, weight: u8) -> Self

Linearly interpolates between self and other using an exact 8-bit integer weight 0..=255.

§Arguments
  • &Self - Start color at weight = 0.
  • Self - End color at weight = 255.
  • u8 - Integer interpolation weight in 0..=255.
§Returns
  • Self - Interpolated RGBA color.
Source

pub fn lerp(&self, other: Self, t: f32) -> Self

Linearly interpolates between self and other by factor t in [0.0, 1.0].

§Arguments
  • &Self - Start color at t = 0.0.
  • Self - End color at t = 1.0.
  • f32 - Interpolation parameter clamped to [0.0, 1.0].
§Returns
  • Self - Interpolated RGBA color.
Source

pub fn blend_over(&self, dst: Self) -> Self

Porter-Duff source-over composites self on top of opaque or translucent dst.

§Arguments
  • &Self - Foreground source color.
  • Self - Background destination color.
§Returns
  • Self - Composited RGBA color.
Source§

impl Color

Source

pub fn get_r(&self) -> u8

Source

pub fn get_mut_r(&mut self) -> &mut u8

Source

pub fn set_r(&mut self, val: u8) -> &mut Self

Source

pub fn get_g(&self) -> u8

Source

pub fn get_mut_g(&mut self) -> &mut u8

Source

pub fn set_g(&mut self, val: u8) -> &mut Self

Source

pub fn get_b(&self) -> u8

Source

pub fn get_mut_b(&mut self) -> &mut u8

Source

pub fn set_b(&mut self, val: u8) -> &mut Self

Source

pub fn get_a(&self) -> u8

Source

pub fn get_mut_a(&mut self) -> &mut u8

Source

pub fn set_a(&mut self, val: u8) -> &mut Self

Source§

impl Color

Source

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

Trait Implementations§

Source§

impl Clone for Color

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for Color

Source§

impl Debug for Color

Source§

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

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

impl Default for Color

Source§

fn default() -> Self

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

impl Eq for Color

Source§

impl Hash for Color

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 Ord for Color

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for Color

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialOrd for Color

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for Color

Auto Trait Implementations§

§

impl Forget for Color

§

impl Freeze for Color

§

impl Move for Color

§

impl RefUnwindSafe for Color

§

impl Send for Color

§

impl Sync for Color

§

impl Unpin for Color

§

impl UnsafeUnpin for Color

§

impl UnwindSafe for Color

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.