#[repr(C)]
pub struct Packed<C = Argb> { pub color: u32, pub channel_order: PhantomData<C>, }
Expand description

RGBA color packed into a 32-bit unsigned integer. Defaults to ARGB ordering for Rgb types and RGBA ordering for Rgba types.

Packed integer type represented in u32. Two hexadecimal digits (8-bits) express each value of the Red, Green, Blue, and Alpha components in the RGBA color.

Note that conversion from float to integer component types in palette rounds to nearest even: an Rgb component of 0.5 will convert to 0x80/128, not 0x7F/127.

use approx::assert_relative_eq;
use palette::{Packed, Srgb, Srgba};
use palette::rgb::channels::{Argb, Rgba};

let packed: Packed = Srgb::new(0.5, 0.0, 0.5).into_format().into();
assert_eq!(0xFF80_0080, packed.color);

let unpacked: Srgba<u8> = Packed::<Rgba>::from(0xFFFF_FF80).into();
assert_relative_eq!(
    Srgba::new(1.0, 1.0, 1.0, 0.5),
    unpacked.into_format(),
    epsilon = 0.01
);

// By default, `Packed` uses `Argb` order for creating `Rgb` colors to make
// entering 6-digit hex numbers more convenient
let rgb = Srgb::from(0xFF8000);
assert_eq!(Srgb::new(0xFF, 0x80, 0x00), rgb);

let rgba = Srgba::from(0xFF80007F);
assert_eq!(Srgba::new(0xFF, 0x80, 0x00, 0x7F), rgba);

When an Rgb type is packed, the alpha value will be 0xFF in the corresponding u32. Converting from a packed color type back to an Rgb type will disregard the alpha value.

Packed implements Pixel and can be constructed from a slice of &[u32].

use palette::{Packed, Pixel};
use palette::rgb::channels::Argb;

let raw = &[0x7F0080u32, 0x60BBCC];
let colors = Packed::<Argb>::from_raw_slice(raw);

assert_eq!(colors.len(), 2);
assert_eq!(colors[0].color, 0x7F0080);
assert_eq!(colors[1].color, 0x60BBCC);

Fields

color: u32

The sRGB color packed into a u32.

channel_order: PhantomData<C>

The channel ordering for red, green, blue, and alpha components in the packed integer; can be Abgr, Argb, Bgra, or Rgba. See RgbChannels.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

The number of color channels.

Cast as a reference to raw color components.

Cast as a mutable reference to raw color components.

Convert into raw color components.

Cast from a reference to raw color components.

Cast from a mutable reference to raw color components.

Cast a slice of raw color components to a slice of colors. Read more

Cast a mutable slice of raw color components to a mutable slice of colors. Read more

Cast a slice of colors to a slice of raw color components. Read more

Cast a mutable slice of colors to a mutable slice of raw color components. Read more

Auto Trait Implementations

Blanket Implementations

Convert the source color to the destination color using the specified method Read more

Convert the source color to the destination color using the bradford method by default Read more

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

Convert into T with values clamped to the color defined bounds Read more

Convert into T. The resulting color might be invalid in its color space Read more

The resulting type after obtaining ownership.

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

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Convert into T, returning ok if the color is inside of its defined range, otherwise an OutOfBounds error is returned which contains the unclamped color. Read more