Skip to main content

Rgba

Struct Rgba 

Source
#[repr(C)]
pub struct Rgba<C>
where C: Copy,
{ pub r: C, pub g: C, pub b: C, pub a: C, }
Expand description

Four-component vector type for representing RGBA colors.

Values use straight (un-premultiplied) alpha. See Premultiplied for the other convention, and Rgba::premultiply to convert.

§Layout

The layout of the Rgba struct is as follows:

+----------------+----------------+----------------+----------------+
|      r         |      g         |      b         |      a         |
+----------------+----------------+----------------+----------------+

As a C-style struct, it represents:

template <typename C>
struct Rgba {
   C r;
   C g;
   C b;
   C a;
};

See U8x4Rgba and F32x4Rgba for type aliases with specific component types.

Fields§

§r: C

Red component.

§g: C

Green component.

§b: C

Blue component.

§a: C

Alpha component.

Implementations§

Source§

impl<C> Rgba<C>
where C: Copy,

Source

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

Creates a new Rgba instance with the specified components.

Source

pub const fn red(&self) -> C

Returns the red component.

Source

pub const fn green(&self) -> C

Returns the green component.

Source

pub const fn blue(&self) -> C

Returns the blue component.

Source

pub const fn alpha(&self) -> C

Returns the alpha component.

Source§

impl<C> Rgba<C>
where C: Channel,

Source

pub fn premultiply(self) -> Premultiplied<Self>

Converts from straight alpha to premultiplied alpha.

premultiplied.rgb = straight.rgb * straight.a

Source

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

Linearly interpolates between self and other by t, clamped to MIN..=MAX.

t == Channel::ZERO returns self; t == Channel::ONE returns other. Both endpoints are exact for every channel type.

Source§

impl Rgba<u8>

Source

pub const TRANSPARENT: Self

Fully transparent black ((0, 0, 0, 0)).

Source

pub const BLACK: Self

Fully opaque black ((0, 0, 0, 255)).

Source

pub const WHITE: Self

Fully opaque white ((255, 255, 255, 255)).

Source§

impl Rgba<f32>

Source

pub const TRANSPARENT: Self

Fully transparent black ((0.0, 0.0, 0.0, 0.0)).

Source

pub const BLACK: Self

Fully opaque black ((0.0, 0.0, 0.0, 1.0)).

Source

pub const WHITE: Self

Fully opaque white ((1.0, 1.0, 1.0, 1.0)).

Source§

impl Rgba<u8>

Source

pub const fn zeroed() -> Self

Creates a new U8x4Rgba instance with 0 for all components.

Source

pub const fn from_rgb_u32(pixel: u32) -> Self

Constructs an opaque U8x4Rgba from a 0x00RRGGBB packed pixel.

The alpha channel is set to 255 (fully opaque). The top byte of pixel is ignored.

Source

pub const fn to_rgb_u32(self) -> u32

Packs this color into a 0x00RRGGBB u32, discarding the alpha channel.

Source

pub const fn source_over(self, dst: Self) -> Self

Blends self (source) over dst (destination) using integer SourceOver.

Equivalent to Composite::NORMAL_OVER, specialized to straight-alpha u8 and computed without an intermediate premultiplied round trip:

out_a   = src_a + dst_a * (1 - src_a)
out_rgb = (src_rgb * src_a + dst_rgb * dst_a * (1 - src_a)) / out_a
§Rounding

Rounds to nearest, once, from an exact widened intermediate. It does not truncate. Compositing an opaque source is idempotent and repeated blends do not drift darker.

Before 0.3.0 this used (v + (v >> 8) + 1) >> 8, which is exactly floor(v / 255) and biased every result downward by up to a full LSB. Results changed accordingly.

§Correctness for translucent destinations

Also fixed in 0.3.0: the previous implementation assumed an opaque destination and produced wrong colors when dst.a < 255. The general case now takes one division per channel; the common opaque-destination case still takes none.

use alpha_blend::rgba::U8x4Rgba;

let src = U8x4Rgba::new(0, 255, 0, 128);
let dst = U8x4Rgba::new(255, 0, 0, 255);
assert_eq!(src.source_over(dst), U8x4Rgba::new(127, 128, 0, 255));
Source

pub const fn is_transparent(self) -> bool

Returns true if this pixel is fully transparent (alpha == 0).

Source

pub const fn is_opaque(self) -> bool

Returns true if this pixel is fully opaque (alpha == 255).

Source§

impl Rgba<f32>

Source

pub const fn zeroed() -> Self

Creates a new F32x4Rgba instance with 0 for all components.

Source

pub const fn clamp(self) -> Self

Clamps all channels to [0.0, 1.0].

Blending never produces out-of-range channels (every Channel operation saturates), so this is for colors that arrive from elsewhere.

Source

pub const fn is_transparent(self) -> bool

Returns true if this pixel is fully transparent (alpha == 0.0).

An exact comparison, not an epsilon: this answers “can I skip this pixel”, and skipping a pixel with alpha == 1e-9 would be wrong.

Source

pub const fn is_opaque(self) -> bool

Returns true if this pixel is fully opaque (alpha == 1.0).

An exact comparison; see F32x4Rgba::is_transparent.

Trait Implementations§

Source§

impl<C: Copy> AsMut<[C]> for Rgba<C>

Source§

fn as_mut(&mut self) -> &mut [C]

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<C: Copy> AsRef<[C]> for Rgba<C>

Source§

fn as_ref(&self) -> &[C]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<C> Clone for Rgba<C>
where C: Copy + Clone,

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<C> Copy for Rgba<C>
where C: Copy + Copy,

Source§

impl<C> Debug for Rgba<C>
where C: Copy + Debug,

Source§

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

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

impl Display for Rgba<u8>

Source§

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

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

impl Display for Rgba<f32>

Source§

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

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

impl Eq for Rgba<u8>

Source§

impl<C: Copy> From<(C, C, C, C)> for Rgba<C>

Source§

fn from((r, g, b, a): (C, C, C, C)) -> Self

Converts to this type from the input type.
Source§

impl<C: Copy> From<Rgba<C>> for [C; 4]

Source§

fn from(c: Rgba<C>) -> Self

Converts to this type from the input type.
Source§

impl<C: Copy> From<Rgba<C>> for (C, C, C, C)

Source§

fn from(c: Rgba<C>) -> Self

Converts to this type from the input type.
Source§

impl From<Rgba<f32>> for U8x4Rgba

Source§

fn from(rgba: F32x4Rgba) -> Self

Converts to this type from the input type.
Source§

impl From<Rgba<u8>> for F32x4Rgba

Source§

fn from(rgba: U8x4Rgba) -> Self

Converts to this type from the input type.
Source§

impl<C: Copy> From<[C; 4]> for Rgba<C>

Source§

fn from([r, g, b, a]: [C; 4]) -> Self

Converts to this type from the input type.
Source§

impl Hash for Rgba<u8>

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<C> PartialEq for Rgba<C>
where C: Copy + PartialEq,

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<C> PartialOrd for Rgba<C>
where C: Copy + PartialOrd,

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<C> StructuralPartialEq for Rgba<C>
where C: Copy + PartialEq,

Auto Trait Implementations§

§

impl<C> Freeze for Rgba<C>
where C: Freeze,

§

impl<C> RefUnwindSafe for Rgba<C>
where C: RefUnwindSafe,

§

impl<C> Send for Rgba<C>
where C: Send,

§

impl<C> Sync for Rgba<C>
where C: Sync,

§

impl<C> Unpin for Rgba<C>
where C: Unpin,

§

impl<C> UnsafeUnpin for Rgba<C>
where C: UnsafeUnpin,

§

impl<C> UnwindSafe for Rgba<C>
where C: UnwindSafe,

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, 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.