Trait palette::encoding::pixel::Pixel [−][src]
pub unsafe trait Pixel<T>: Sized { const CHANNELS: usize; fn as_raw<P: RawPixel<T> + ?Sized>(&self) -> &P { ... } fn as_raw_mut<P: RawPixel<T> + ?Sized>(&mut self) -> &mut P { ... } fn into_raw<P: RawPixelSized<T>>(self) -> P { ... } fn from_raw<P: RawPixel<T> + ?Sized>(pixel: &P) -> &Self { ... } fn from_raw_mut<P: RawPixel<T> + ?Sized>(pixel: &mut P) -> &mut Self { ... } fn from_raw_slice(slice: &[T]) -> &[Self] { ... } fn from_raw_slice_mut(slice: &mut [T]) -> &mut [Self] { ... } fn into_raw_slice(slice: &[Self]) -> &[T] { ... } fn into_raw_slice_mut(slice: &mut [Self]) -> &mut [T] { ... } }
Represents colors that can be serialized and deserialized from raw color components.
This uses bit by bit conversion, so make sure that anything that implements
it can be represented as a contiguous sequence of a single type T
. This is
most safely done using #[derive(Pixel)]
.
Deriving
Pixel
can be automatically derived. The only requirements are that the
type is a struct
, that it has a #[repr(C)]
attribute, and that all of
its fields have the same types. It stays on the conservative side and will
show an error if any of those requirements are not fulfilled. If some fields
have different types, but the same memory layout, or are zero-sized, they
can be marked with attributes to show that their types are safe to use.
Field Attributes
-
#[palette_unsafe_same_layout_as = "SomeType"]
: Mark the field as having the same memory layout asSomeType
.Unsafety: corrupt data and undefined behavior may occur if this is not true!
-
#[palette_unsafe_zero_sized]
: Mark the field as being zero-sized, and thus not taking up any memory space. This means that it can be ignored.Unsafety: corrupt data and undefined behavior may occur if this is not true!
Examples
Basic use:
use palette::Pixel; #[derive(PartialEq, Debug, Pixel)] #[repr(C)] struct MyCmyk { cyan: f32, magenta: f32, yellow: f32, key: f32, } let buffer = [0.1, 0.2, 0.3, 0.4]; let color = MyCmyk::from_raw(&buffer); assert_eq!( color, &MyCmyk { cyan: 0.1, magenta: 0.2, yellow: 0.3, key: 0.4, } );
Heterogenous field types:
use std::marker::PhantomData; use palette::{Pixel, RgbHue}; use palette::rgb::RgbStandard; use palette::encoding::Srgb; #[derive(PartialEq, Debug, Pixel)] #[repr(C)] struct MyCoolColor<S: RgbStandard> { #[palette(unsafe_zero_sized)] standard: PhantomData<S>, // RgbHue is a wrapper with `#[repr(C)]`, so it can safely // be converted straight from `f32`. #[palette(unsafe_same_layout_as = "f32")] hue: RgbHue<f32>, lumen: f32, chroma: f32, } let buffer = [172.0, 100.0, 0.3]; let color = MyCoolColor::<Srgb>::from_raw(&buffer); assert_eq!( color, &MyCoolColor { hue: 172.0.into(), lumen: 100.0, chroma: 0.3, standard: PhantomData, } );
Associated Constants
Loading content...Provided methods
fn as_raw<P: RawPixel<T> + ?Sized>(&self) -> &P
[src]
Cast as a reference to raw color components.
fn as_raw_mut<P: RawPixel<T> + ?Sized>(&mut self) -> &mut P
[src]
Cast as a mutable reference to raw color components.
fn into_raw<P: RawPixelSized<T>>(self) -> P
[src]
Convert into raw color components.
fn from_raw<P: RawPixel<T> + ?Sized>(pixel: &P) -> &Self
[src]
Cast from a reference to raw color components.
fn from_raw_mut<P: RawPixel<T> + ?Sized>(pixel: &mut P) -> &mut Self
[src]
Cast from a mutable reference to raw color components.
fn from_raw_slice(slice: &[T]) -> &[Self]
[src]
Cast a slice of raw color components to a slice of colors.
use palette::{Pixel, Srgb}; let raw = &[255u8, 128, 64, 10, 20, 30]; let colors = Srgb::from_raw_slice(raw); assert_eq!(colors.len(), 2); assert_eq!(colors[0].blue, 64); assert_eq!(colors[1].red, 10);
fn from_raw_slice_mut(slice: &mut [T]) -> &mut [Self]
[src]
Cast a mutable slice of raw color components to a mutable slice of colors.
use palette::{Pixel, Srgb}; let raw = &mut [255u8, 128, 64, 10, 20, 30]; { let colors = Srgb::from_raw_slice_mut(raw); assert_eq!(colors.len(), 2); // These changes affects the raw slice, since they are the same data colors[0].blue = 100; colors[1].red = 200; } // Notice the two values in the middle: assert_eq!(raw, &[255, 128, 100, 200, 20, 30]);
fn into_raw_slice(slice: &[Self]) -> &[T]
[src]
Cast a slice of colors to a slice of raw color components.
use palette::{Pixel, Srgb}; let colors = &[Srgb::new(255u8, 128, 64), Srgb::new(10, 20, 30)]; let raw = Srgb::into_raw_slice(colors); assert_eq!(raw.len(), 6); assert_eq!(raw, &[255u8, 128, 64, 10, 20, 30]);
fn into_raw_slice_mut(slice: &mut [Self]) -> &mut [T]
[src]
Cast a mutable slice of colors to a mutable slice of raw color components.
use palette::{Pixel, Srgb}; let colors = &mut [Srgb::new(255u8, 128, 64), Srgb::new(10, 20, 30)]; { let raw = Srgb::into_raw_slice_mut(colors); assert_eq!(raw.len(), 6); // These changes affects the color slice, since they are the same data raw[2] = 100; raw[3] = 200; } assert_eq!(colors[0].blue, 100); assert_eq!(colors[1].red, 200);
Implementors
impl<S, T> Pixel<T> for Luma<S, T> where
T: Component,
S: LumaStandard,
[src]
impl<S, T> Pixel<T> for Luma<S, T> where
T: Component,
S: LumaStandard,
[src]impl<S, T> Pixel<T> for Hsl<S, T> where
T: FloatComponent,
S: RgbStandard,
[src]
impl<S, T> Pixel<T> for Hsl<S, T> where
T: FloatComponent,
S: RgbStandard,
[src]impl<S, T> Pixel<T> for Hsv<S, T> where
T: FloatComponent,
S: RgbStandard,
[src]
impl<S, T> Pixel<T> for Hsv<S, T> where
T: FloatComponent,
S: RgbStandard,
[src]impl<S, T> Pixel<T> for Hwb<S, T> where
T: FloatComponent,
S: RgbStandard,
[src]
impl<S, T> Pixel<T> for Hwb<S, T> where
T: FloatComponent,
S: RgbStandard,
[src]impl<Wp, T> Pixel<T> for Hsluv<Wp, T> where
T: FloatComponent,
Wp: WhitePoint,
[src]
impl<Wp, T> Pixel<T> for Hsluv<Wp, T> where
T: FloatComponent,
Wp: WhitePoint,
[src]impl<Wp, T> Pixel<T> for Lab<Wp, T> where
T: FloatComponent,
Wp: WhitePoint,
[src]
impl<Wp, T> Pixel<T> for Lab<Wp, T> where
T: FloatComponent,
Wp: WhitePoint,
[src]impl<Wp, T> Pixel<T> for Lch<Wp, T> where
T: FloatComponent,
Wp: WhitePoint,
[src]
impl<Wp, T> Pixel<T> for Lch<Wp, T> where
T: FloatComponent,
Wp: WhitePoint,
[src]impl<Wp, T> Pixel<T> for Lchuv<Wp, T> where
T: FloatComponent,
Wp: WhitePoint,
[src]
impl<Wp, T> Pixel<T> for Lchuv<Wp, T> where
T: FloatComponent,
Wp: WhitePoint,
[src]impl<Wp, T> Pixel<T> for Luv<Wp, T> where
T: FloatComponent,
Wp: WhitePoint,
[src]
impl<Wp, T> Pixel<T> for Luv<Wp, T> where
T: FloatComponent,
Wp: WhitePoint,
[src]impl<Wp, T> Pixel<T> for Xyz<Wp, T> where
T: FloatComponent,
Wp: WhitePoint,
[src]
impl<Wp, T> Pixel<T> for Xyz<Wp, T> where
T: FloatComponent,
Wp: WhitePoint,
[src]impl<Wp, T> Pixel<T> for Yxy<Wp, T> where
T: FloatComponent,
Wp: WhitePoint,
[src]
impl<Wp, T> Pixel<T> for Yxy<Wp, T> where
T: FloatComponent,
Wp: WhitePoint,
[src]