#[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>
impl<O, P> Packed<O, P>
sourcepub fn pack<C>(color: C) -> Selfwhere
O: ComponentOrder<C, P>,
pub fn pack<C>(color: C) -> Selfwhere
O: ComponentOrder<C, P>,
Transform a color value into a packed memory representation.
sourcepub fn unpack<C>(self) -> Cwhere
O: ComponentOrder<C, P>,
pub fn unpack<C>(self) -> Cwhere
O: ComponentOrder<C, P>,
Transform a packed color into a regular color value.
Trait Implementations§
source§impl<S, T, O, P> From<Alpha<Luma<S, T>, T>> for Packed<O, P>where
O: ComponentOrder<Lumaa<S, T>, P>,
impl<S, T, O, P> From<Alpha<Luma<S, T>, T>> for Packed<O, P>where
O: ComponentOrder<Lumaa<S, T>, P>,
source§impl<S, T, O, P> From<Alpha<Rgb<S, T>, T>> for Packed<O, P>where
O: ComponentOrder<Rgba<S, T>, P>,
impl<S, T, O, P> From<Alpha<Rgb<S, T>, T>> for Packed<O, P>where
O: ComponentOrder<Rgba<S, T>, P>,
impl<O, P> Copy for Packed<O, P>where
P: Copy,
impl<O: Eq, P: Eq> Eq for Packed<O, P>
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>where
P: RefUnwindSafe,
O: RefUnwindSafe,
impl<O, P> Send for Packed<O, P>
impl<O, P> Sync for Packed<O, P>
impl<O, P> Unpin for Packed<O, P>
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 Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
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) -> Dwhere
M: TransformMatrix<T>,
fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
source§fn adapt_into(self) -> D
fn adapt_into(self) -> D
source§impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
source§fn arrays_from(colors: C) -> T
fn arrays_from(colors: C) -> T
source§impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
source§fn arrays_into(self) -> C
fn arrays_into(self) -> C
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
source§type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
parameters
when converting.source§fn cam16_into_unclamped(
self,
parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>,
) -> T
fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T
self
into C
, using the provided parameters.source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
source§fn components_from(colors: C) -> T
fn components_from(colors: C) -> T
source§impl<T> FromAngle<T> for T
impl<T> FromAngle<T> for T
source§fn from_angle(angle: T) -> T
fn from_angle(angle: T) -> T
angle
.source§impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
source§fn from_stimulus(other: U) -> T
fn from_stimulus(other: U) -> T
other
into Self
, while performing the appropriate scaling,
rounding and clamping.source§impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
source§fn into_angle(self) -> U
fn into_angle(self) -> U
T
.source§impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
source§type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
parameters
when converting.source§fn into_cam16_unclamped(
self,
parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>,
) -> T
fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T
self
into C
, using the provided parameters.source§impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
source§fn into_color(self) -> U
fn into_color(self) -> U
source§impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
source§fn into_color_unclamped(self) -> U
fn into_color_unclamped(self) -> U
source§impl<T> IntoStimulus<T> for T
impl<T> IntoStimulus<T> for T
source§fn into_stimulus(self) -> T
fn into_stimulus(self) -> T
self
into T
, while performing the appropriate scaling,
rounding and clamping.source§impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
source§type Error = <C as TryFromComponents<T>>::Error
type Error = <C as TryFromComponents<T>>::Error
try_into_colors
fails to cast.source§fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
source§impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
source§fn try_into_color(self) -> Result<U, OutOfBounds<U>>
fn try_into_color(self) -> Result<U, OutOfBounds<U>>
OutOfBounds
error is returned which contains
the unclamped color. Read more