[][src]Trait amethyst_rendy::palette::Pixel

pub unsafe trait Pixel<T> {
    const CHANNELS: usize;
    fn as_raw<P>(&self) -> &P
    where
        P: RawPixel<T> + ?Sized
, { ... }
fn as_raw_mut<P>(&mut self) -> &mut P
    where
        P: RawPixel<T> + ?Sized
, { ... }
fn into_raw<P>(self) -> P
    where
        P: RawPixelSized<T>
, { ... }
fn from_raw<P>(pixel: &P) -> &Self
    where
        P: RawPixel<T> + ?Sized
, { ... }
fn from_raw_mut<P>(pixel: &mut P) -> &mut Self
    where
        P: RawPixel<T> + ?Sized
, { ... }
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

Examples

Basic use:

#[macro_use]
extern crate palette;

use palette::Pixel;

#[derive(PartialEq, Debug, Pixel)]
#[repr(C)]
struct MyCmyk {
    cyan: f32,
    magenta: f32,
    yellow: f32,
    key: f32,
}

fn main() {
    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:

#[macro_use]
extern crate palette;

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,
}

fn main() {
    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

const CHANNELS: usize

The number of color channels.

Loading content...

Provided methods

fn as_raw<P>(&self) -> &P where
    P: RawPixel<T> + ?Sized

Cast as a reference to raw color components.

fn as_raw_mut<P>(&mut self) -> &mut P where
    P: RawPixel<T> + ?Sized

Cast as a mutable reference to raw color components.

fn into_raw<P>(self) -> P where
    P: RawPixelSized<T>, 

Convert from raw color components.

fn from_raw<P>(pixel: &P) -> &Self where
    P: RawPixel<T> + ?Sized

Cast from a reference to raw color components.

fn from_raw_mut<P>(pixel: &mut P) -> &mut Self where
    P: RawPixel<T> + ?Sized

Cast from a mutable reference to raw color components.

fn from_raw_slice(slice: &[T]) -> &[Self]

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]

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]

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]

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);
Loading content...

Implementors

impl<S, T> Pixel<T> for Luma<S, T> where
    S: LumaStandard,
    T: Component
[src]

impl<S, T> Pixel<T> for Rgb<S, T> where
    S: RgbStandard,
    T: Component
[src]

impl<S, T> Pixel<T> for Hsl<S, T> where
    S: RgbSpace,
    T: Component + Float
[src]

impl<S, T> Pixel<T> for Hsv<S, T> where
    S: RgbSpace,
    T: Component + Float
[src]

impl<S, T> Pixel<T> for Hwb<S, T> where
    S: RgbSpace,
    T: Component + Float
[src]

impl<T, C> Pixel<T> for PreAlpha<C, T> where
    C: Pixel<T>,
    T: Float
[src]

impl<T, C> Pixel<T> for Alpha<C, T> where
    C: Pixel<T>, 
[src]

impl<Wp, T> Pixel<T> for Lab<Wp, T> where
    T: Component + Float,
    Wp: WhitePoint
[src]

impl<Wp, T> Pixel<T> for Lch<Wp, T> where
    T: Component + Float,
    Wp: WhitePoint
[src]

impl<Wp, T> Pixel<T> for Xyz<Wp, T> where
    T: Component + Float,
    Wp: WhitePoint
[src]

impl<Wp, T> Pixel<T> for Yxy<Wp, T> where
    T: Component + Float,
    Wp: WhitePoint
[src]

Loading content...