palette::cast

Struct Packed

source
#[repr(transparent)]
pub struct Packed<O, P> { pub color: P, pub channel_order: PhantomData<O>, }
Expand description

A color packed into a compact format, such as an unsigned integer.

Packed implements ArrayCast and UintCast so it can easily be constructed from slices, arrays and unsigned integers.

// `PackedArgb` is an alias for `Packed<rgb::channels::Argb, P = u32>`.
use palette::{rgb::PackedArgb, cast::UintsAs};

let raw = [0x7F0080u32, 0x60BBCC];
let colors: &[PackedArgb] = raw.uints_as();

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

§Packed Integer Type Represented in u32.

A common example of a packed format is when an RGBA color is encoded as a hexadecimal number (such as 0x7F0080 from above). 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::{Srgb, Srgba};
use palette::rgb::{PackedArgb, PackedRgba};

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

let unpacked: Srgba<u8> = PackedRgba::from(0xFFFF_FF80u32).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.

Fields§

§color: P

The color packed into a type P, such as u32 or [u8; 4].

§channel_order: PhantomData<O>

The channel order for the color components in the packed data. See ComponentOrder.

Implementations§

source§

impl<O, P> Packed<O, P>

source

pub fn pack<C>(color: C) -> Self
where O: ComponentOrder<C, P>,

Transform a color value into a packed memory representation.

source

pub fn unpack<C>(self) -> C
where O: ComponentOrder<C, P>,

Transform a packed color into a regular color value.

Trait Implementations§

source§

impl<O, T, const N: usize> ArrayCast for Packed<O, [T; N]>

source§

type Array = [T; N]

The output type of a cast to an array.
source§

impl<O, T, const N: usize> AsMut<[T]> for Packed<O, [T; N]>

source§

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

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

impl<O, T, const N: usize> AsMut<[T; N]> for Packed<O, [T; N]>

source§

fn as_mut(&mut self) -> &mut [T; N]

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

impl<O, P> AsMut<P> for Packed<O, P>
where Packed<O, P>: UintCast<Uint = P>,

source§

fn as_mut(&mut self) -> &mut P

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

impl<O, T, const N: usize> AsMut<Packed<O, [T; N]>> for [T; N]

source§

fn as_mut(&mut self) -> &mut Packed<O, [T; N]>

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

impl<O> AsMut<Packed<O, u128>> for u128

source§

fn as_mut(&mut self) -> &mut Packed<O, u128>

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

impl<O> AsMut<Packed<O, u16>> for u16

source§

fn as_mut(&mut self) -> &mut Packed<O, u16>

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

impl<O> AsMut<Packed<O, u32>> for u32

source§

fn as_mut(&mut self) -> &mut Packed<O, u32>

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

impl<O> AsMut<Packed<O, u64>> for u64

source§

fn as_mut(&mut self) -> &mut Packed<O, u64>

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

impl<O> AsMut<Packed<O, u8>> for u8

source§

fn as_mut(&mut self) -> &mut Packed<O, u8>

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

impl<O, T, const N: usize> AsRef<[T]> for Packed<O, [T; N]>

source§

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

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

impl<O, T, const N: usize> AsRef<[T; N]> for Packed<O, [T; N]>

source§

fn as_ref(&self) -> &[T; N]

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

impl<O, P> AsRef<P> for Packed<O, P>
where Packed<O, P>: UintCast<Uint = P>,

source§

fn as_ref(&self) -> &P

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

impl<O, T, const N: usize> AsRef<Packed<O, [T; N]>> for [T; N]

source§

fn as_ref(&self) -> &Packed<O, [T; N]>

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

impl<O> AsRef<Packed<O, u128>> for u128

source§

fn as_ref(&self) -> &Packed<O, u128>

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

impl<O> AsRef<Packed<O, u16>> for u16

source§

fn as_ref(&self) -> &Packed<O, u16>

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

impl<O> AsRef<Packed<O, u32>> for u32

source§

fn as_ref(&self) -> &Packed<O, u32>

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

impl<O> AsRef<Packed<O, u64>> for u64

source§

fn as_ref(&self) -> &Packed<O, u64>

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

impl<O> AsRef<Packed<O, u8>> for u8

source§

fn as_ref(&self) -> &Packed<O, u8>

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

impl<O, P> Clone for Packed<O, P>
where P: Clone,

source§

fn clone(&self) -> Self

Returns a copy 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<O: Debug, P: Debug> Debug for Packed<O, P>

source§

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

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

impl<'a, O, T, const N: usize> From<&'a [T; N]> for &'a Packed<O, [T; N]>

source§

fn from(array: &'a [T; N]) -> Self

Converts to this type from the input type.
source§

impl<'a, O, P> From<&'a P> for &'a Packed<O, P>
where P: AsRef<Packed<O, P>>, Packed<O, P>: UintCast<Uint = P>,

source§

fn from(uint: &'a P) -> Self

Converts to this type from the input type.
source§

impl<'a, O, T, const N: usize> From<&'a Packed<O, [T; N]>> for &'a [T]

source§

fn from(color: &'a Packed<O, [T; N]>) -> Self

Converts to this type from the input type.
source§

impl<'a, O, T, const N: usize> From<&'a Packed<O, [T; N]>> for &'a [T; N]

source§

fn from(color: &'a Packed<O, [T; N]>) -> Self

Converts to this type from the input type.
source§

impl<'a, O> From<&'a Packed<O, u128>> for &'a u128

source§

fn from(color: &'a Packed<O, u128>) -> Self

Converts to this type from the input type.
source§

impl<'a, O> From<&'a Packed<O, u16>> for &'a u16

source§

fn from(color: &'a Packed<O, u16>) -> Self

Converts to this type from the input type.
source§

impl<'a, O> From<&'a Packed<O, u32>> for &'a u32

source§

fn from(color: &'a Packed<O, u32>) -> Self

Converts to this type from the input type.
source§

impl<'a, O> From<&'a Packed<O, u64>> for &'a u64

source§

fn from(color: &'a Packed<O, u64>) -> Self

Converts to this type from the input type.
source§

impl<'a, O> From<&'a Packed<O, u8>> for &'a u8

source§

fn from(color: &'a Packed<O, u8>) -> Self

Converts to this type from the input type.
source§

impl<'a, O, T, const N: usize> From<&'a mut [T; N]> for &'a mut Packed<O, [T; N]>

source§

fn from(array: &'a mut [T; N]) -> Self

Converts to this type from the input type.
source§

impl<'a, O, P> From<&'a mut P> for &'a mut Packed<O, P>
where P: AsMut<Packed<O, P>>, Packed<O, P>: UintCast<Uint = P>,

source§

fn from(uint: &'a mut P) -> Self

Converts to this type from the input type.
source§

impl<'a, O, T, const N: usize> From<&'a mut Packed<O, [T; N]>> for &'a mut [T]

source§

fn from(color: &'a mut Packed<O, [T; N]>) -> Self

Converts to this type from the input type.
source§

impl<'a, O, T, const N: usize> From<&'a mut Packed<O, [T; N]>> for &'a mut [T; N]

source§

fn from(color: &'a mut Packed<O, [T; N]>) -> Self

Converts to this type from the input type.
source§

impl<'a, O> From<&'a mut Packed<O, u128>> for &'a mut u128

source§

fn from(color: &'a mut Packed<O, u128>) -> Self

Converts to this type from the input type.
source§

impl<'a, O> From<&'a mut Packed<O, u16>> for &'a mut u16

source§

fn from(color: &'a mut Packed<O, u16>) -> Self

Converts to this type from the input type.
source§

impl<'a, O> From<&'a mut Packed<O, u32>> for &'a mut u32

source§

fn from(color: &'a mut Packed<O, u32>) -> Self

Converts to this type from the input type.
source§

impl<'a, O> From<&'a mut Packed<O, u64>> for &'a mut u64

source§

fn from(color: &'a mut Packed<O, u64>) -> Self

Converts to this type from the input type.
source§

impl<'a, O> From<&'a mut Packed<O, u8>> for &'a mut u8

source§

fn from(color: &'a mut Packed<O, u8>) -> Self

Converts to this type from the input type.
source§

impl<O, T, const N: usize> From<[T; N]> for Packed<O, [T; N]>

source§

fn from(array: [T; N]) -> Self

Converts to this type from the input type.
source§

impl<S, T, O, P> From<Alpha<Luma<S, T>, T>> for Packed<O, P>
where O: ComponentOrder<Lumaa<S, T>, P>,

source§

fn from(color: Lumaa<S, T>) -> Self

Converts to this type from the input type.
source§

impl<S, T, O, P> From<Alpha<Rgb<S, T>, T>> for Packed<O, P>
where O: ComponentOrder<Rgba<S, T>, P>,

source§

fn from(color: Rgba<S, T>) -> Self

Converts to this type from the input type.
source§

impl<O, T, const N: usize> From<Box<[T; N]>> for Box<Packed<O, [T; N]>>

source§

fn from(array: Box<[T; N]>) -> Self

Converts to this type from the input type.
source§

impl<S, T, P, O> From<Luma<S, T>> for Packed<O, P>
where O: ComponentOrder<Lumaa<S, T>, P>, Lumaa<S, T>: From<Luma<S, T>>,

source§

fn from(color: Luma<S, T>) -> Self

Converts to this type from the input type.
source§

impl<O, P> From<P> for Packed<O, P>
where Packed<O, P>: UintCast<Uint = P>,

source§

fn from(uint: P) -> Self

Converts to this type from the input type.
source§

impl<O, T, const N: usize> From<Packed<O, [T; N]>> for [T; N]

source§

fn from(color: Packed<O, [T; N]>) -> Self

Converts to this type from the input type.
source§

impl<S, T, O, P> From<Packed<O, P>> for Lumaa<S, T>
where O: ComponentOrder<Lumaa<S, T>, P>,

source§

fn from(packed: Packed<O, P>) -> Self

Converts to this type from the input type.
source§

impl<S, T, O, P> From<Packed<O, P>> for Rgba<S, T>
where O: ComponentOrder<Rgba<S, T>, P>,

source§

fn from(packed: Packed<O, P>) -> Self

Converts to this type from the input type.
source§

impl<S, O, P> From<Packed<O, P>> for Luma<S, u8>
where O: ComponentOrder<Lumaa<S, u8>, P>,

source§

fn from(packed: Packed<O, P>) -> Self

Converts to this type from the input type.
source§

impl<S, O, P> From<Packed<O, P>> for Rgb<S, u8>
where O: ComponentOrder<Rgba<S, u8>, P>,

source§

fn from(packed: Packed<O, P>) -> Self

Converts to this type from the input type.
source§

impl<O> From<Packed<O, u128>> for u128

source§

fn from(color: Packed<O, u128>) -> Self

Converts to this type from the input type.
source§

impl<O> From<Packed<O, u16>> for u16

source§

fn from(color: Packed<O, u16>) -> Self

Converts to this type from the input type.
source§

impl<O> From<Packed<O, u32>> for u32

source§

fn from(color: Packed<O, u32>) -> Self

Converts to this type from the input type.
source§

impl<O> From<Packed<O, u64>> for u64

source§

fn from(color: Packed<O, u64>) -> Self

Converts to this type from the input type.
source§

impl<O> From<Packed<O, u8>> for u8

source§

fn from(color: Packed<O, u8>) -> Self

Converts to this type from the input type.
source§

impl<S, T, P, O> From<Rgb<S, T>> for Packed<O, P>
where O: ComponentOrder<Rgba<S, T>, P>, Rgba<S, T>: From<Rgb<S, T>>,

source§

fn from(color: Rgb<S, T>) -> Self

Converts to this type from the input type.
source§

impl<O: PartialEq, P: PartialEq> PartialEq for Packed<O, P>

source§

fn eq(&self, other: &Packed<O, P>) -> 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<'a, O, T, const N: usize> TryFrom<&'a [T]> for &'a Packed<O, [T; N]>

source§

type Error = <&'a [T; N] as TryFrom<&'a [T]>>::Error

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

fn try_from(slice: &'a [T]) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a, O, T, const N: usize> TryFrom<&'a mut [T]> for &'a mut Packed<O, [T; N]>

source§

type Error = <&'a mut [T; N] as TryFrom<&'a mut [T]>>::Error

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

fn try_from(slice: &'a mut [T]) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<O> UintCast for Packed<O, u128>

source§

type Uint = u128

An unsigned integer with the same size as Self.
source§

impl<O> UintCast for Packed<O, u16>

source§

type Uint = u16

An unsigned integer with the same size as Self.
source§

impl<O> UintCast for Packed<O, u32>

source§

type Uint = u32

An unsigned integer with the same size as Self.
source§

impl<O> UintCast for Packed<O, u64>

source§

type Uint = u64

An unsigned integer with the same size as Self.
source§

impl<O> UintCast for Packed<O, u8>

source§

type Uint = u8

An unsigned integer with the same size as Self.
source§

impl<O, P> Copy for Packed<O, P>
where P: Copy,

source§

impl<O: Eq, P: Eq> Eq for Packed<O, P>

source§

impl<O, P> StructuralPartialEq for Packed<O, P>

Auto Trait Implementations§

§

impl<O, P> Freeze for Packed<O, P>
where P: Freeze,

§

impl<O, P> RefUnwindSafe for Packed<O, P>

§

impl<O, P> Send for Packed<O, P>
where P: Send, O: Send,

§

impl<O, P> Sync for Packed<O, P>
where P: Sync, O: Sync,

§

impl<O, P> Unpin for Packed<O, P>
where P: Unpin, O: Unpin,

§

impl<O, P> UnwindSafe for Packed<O, P>
where P: UnwindSafe, O: UnwindSafe,

Blanket Implementations§

source§

impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for S
where T: Real + Zero + Arithmetics + Clone, Swp: WhitePoint<T>, Dwp: WhitePoint<T>, D: AdaptFrom<S, Swp, Dwp, T>,

source§

fn adapt_into_using<M>(self, method: M) -> D
where M: TransformMatrix<T>,

Convert the source color to the destination color using the specified method.
source§

fn adapt_into(self) -> D

Convert the source color to the destination color using the bradford method by default.
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, C> ArraysFrom<C> for T
where C: IntoArrays<T>,

source§

fn arrays_from(colors: C) -> T

Cast a collection of colors into a collection of arrays.
source§

impl<T, C> ArraysInto<C> for T
where C: FromArrays<T>,

source§

fn arrays_into(self) -> C

Cast this collection of arrays into a collection of colors.
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<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for U
where T: FromCam16Unclamped<WpParam, U>,

source§

type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar

The number type that’s used in parameters when converting.
source§

fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T

Converts self into C, using the provided parameters.
source§

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

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

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

impl<T, C> ComponentsFrom<C> for T
where C: IntoComponents<T>,

source§

fn components_from(colors: C) -> T

Cast a collection of colors into a collection of color components.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> FromAngle<T> for T

source§

fn from_angle(angle: T) -> T

Performs a conversion from angle.
source§

impl<T, U> FromStimulus<U> for T
where U: IntoStimulus<T>,

source§

fn from_stimulus(other: U) -> T

Converts other into Self, while performing the appropriate scaling, rounding and clamping.
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> IntoAngle<U> for T
where U: FromAngle<T>,

source§

fn into_angle(self) -> U

Performs a conversion into T.
source§

impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for U
where T: Cam16FromUnclamped<WpParam, U>,

source§

type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar

The number type that’s used in parameters when converting.
source§

fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T

Converts self into C, using the provided parameters.
source§

impl<T, U> IntoColor<U> for T
where U: FromColor<T>,

source§

fn into_color(self) -> U

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

impl<T, U> IntoColorUnclamped<U> for T
where U: FromColorUnclamped<T>,

source§

fn into_color_unclamped(self) -> U

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

impl<T> IntoStimulus<T> for T

source§

fn into_stimulus(self) -> T

Converts self into T, while performing the appropriate scaling, rounding and clamping.
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, C> TryComponentsInto<C> for T
where C: TryFromComponents<T>,

source§

type Error = <C as TryFromComponents<T>>::Error

The error for when try_into_colors fails to cast.
source§

fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>

Try to cast this collection of color components into a collection of colors. 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, U> TryIntoColor<U> for T
where U: TryFromColor<T>,

source§

fn try_into_color(self) -> Result<U, OutOfBounds<U>>

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
source§

impl<C, U> UintsFrom<C> for U
where C: IntoUints<U>,

source§

fn uints_from(colors: C) -> U

Cast a collection of colors into a collection of unsigned integers.
source§

impl<C, U> UintsInto<C> for U
where C: FromUints<U>,

source§

fn uints_into(self) -> C

Cast this collection of unsigned integers into a collection of colors.