#[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: CRed component.
g: CGreen component.
b: CBlue component.
a: CAlpha component.
Implementations§
Source§impl<C> Rgba<C>where
C: Copy,
impl<C> Rgba<C>where
C: Copy,
Source§impl<C> Rgba<C>where
C: Channel,
impl<C> Rgba<C>where
C: Channel,
Sourcepub fn premultiply(self) -> Premultiplied<Self>
pub fn premultiply(self) -> Premultiplied<Self>
Converts from straight alpha to premultiplied alpha.
premultiplied.rgb = straight.rgb * straight.a
Source§impl Rgba<u8>
impl Rgba<u8>
Sourcepub const fn from_rgb_u32(pixel: u32) -> Self
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.
Sourcepub const fn to_rgb_u32(self) -> u32
pub const fn to_rgb_u32(self) -> u32
Packs this color into a 0x00RRGGBB u32, discarding the alpha channel.
Sourcepub const fn source_over(self, dst: Self) -> Self
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));Sourcepub const fn is_transparent(self) -> bool
pub const fn is_transparent(self) -> bool
Returns true if this pixel is fully transparent (alpha == 0).
Source§impl Rgba<f32>
impl Rgba<f32>
Sourcepub const fn clamp(self) -> Self
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.
Sourcepub const fn is_transparent(self) -> bool
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.
Sourcepub const fn is_opaque(self) -> bool
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.