[][src]Trait palette::IntoColor

pub trait IntoColor<Wp = D65, T = f32>: Sized where
    T: Component + Float,
    Wp: WhitePoint
{ fn into_xyz(self) -> Xyz<Wp, T>; fn into_yxy(self) -> Yxy<Wp, T> { ... }
fn into_lab(self) -> Lab<Wp, T> { ... }
fn into_lch(self) -> Lch<Wp, T> { ... }
fn into_rgb<S: RgbSpace<WhitePoint = Wp>>(self) -> Rgb<Linear<S>, T> { ... }
fn into_hsl<S: RgbSpace<WhitePoint = Wp>>(self) -> Hsl<S, T> { ... }
fn into_hsv<S: RgbSpace<WhitePoint = Wp>>(self) -> Hsv<S, T> { ... }
fn into_hwb<S: RgbSpace<WhitePoint = Wp>>(self) -> Hwb<S, T> { ... }
fn into_luma(self) -> Luma<Linear<Wp>, T> { ... } }

IntoColor provides conversion to the colors.

It requires into_xyz, when implemented manually, and derives conversion to other colors as a default from this. These defaults must be overridden when direct conversion exists between colors.

Deriving

IntoColor can be derived in a mostly automatic way. The strength of deriving it is that it will also derive Into implementations for all of the palette color types. The minimum requirement is to implement Into<Xyz>, but it can also be customized to make use of generics and have other manual implementations.

Item Attributes

Field Attributes

Examples

Minimum requirements implementation:

#[macro_use]
extern crate palette;

use palette::{Srgb, Xyz};

/// A custom version of Xyz that stores integer values from 0 to 100.
#[derive(PartialEq, Debug, IntoColor)]
struct Xyz100 {
    x: u8,
    y: u8,
    z: u8,
}

// We have to at least implement conversion into Xyz if we don't
// specify anything else, using the `palette_manual_into` attribute.
impl Into<Xyz> for Xyz100 {
    fn into(self) -> Xyz {
        Xyz::new(
            self.x as f32 / 100.0,
            self.y as f32 / 100.0,
            self.z as f32 / 100.0,
        )
    }
}

fn main() {
    // Start with an Xyz100 color.
    let xyz = Xyz100 {
        x: 59,
        y: 75,
        z: 42,
    };

    // Convert the color to sRGB.
    let rgb: Srgb = xyz.into();

    assert_eq!(rgb.into_format(), Srgb::new(195u8, 237, 154));
}

With generic components:

#[macro_use]
extern crate palette;
extern crate num_traits;
#[macro_use]
extern crate approx;

use palette::{Component, Hsv, IntoColor, Pixel, Srgb};
use palette::rgb::{Rgb, RgbSpace};
use palette::encoding::Linear;
use palette::white_point::D65;
use num_traits::Float;

/// sRGB, but with a reversed memory layout.
#[derive(Copy, Clone, IntoColor, Pixel)]
#[palette_manual_into(Rgb = "into_rgb_internal")]
#[palette_component = "T"]
#[repr(C)] // Makes sure the memory layout is as we want it.
struct Bgr<T> {
    blue: T,
    green: T,
    red: T,
}

// Rgb is a bit more complex than other colors, so we are
// implementing a private conversion function and letting it
// derive `Into` automatically.
impl<T: Component + Float> Bgr<T> {
    // It converts from any linear Rgb type that has the D65
    // white point, which is the default if we don't specify
    // anything else with the `palette_white_point` attribute.
    fn into_rgb_internal<S>(self) -> Rgb<Linear<S>, T>
    where
        S: RgbSpace<WhitePoint = D65>,
    {
        Srgb::new(self.red, self.green, self.blue).into_rgb()
    }
}

fn main() {
    let buffer = vec![
        0.0f64,
        0.0,
        0.0,
        0.0,
        0.7353569830524495,
        0.5370987304831942,
    ];
    let hsv = Bgr::from_raw_slice(&buffer)[1].into();

    assert_relative_eq!(hsv, Hsv::new(90.0, 1.0, 0.5));
}

With alpha component:

#[macro_use]
extern crate palette;
#[macro_use]
extern crate approx;

use palette::{IntoColor, LinSrgba, Srgb};
use palette::rgb::{Rgb, RgbSpace};
use palette::encoding::Linear;
use palette::white_point::D65;

/// CSS style sRGB.
#[derive(PartialEq, Debug, IntoColor)]
#[palette_manual_into(Rgb = "into_rgb_internal")]
struct CssRgb {
    red: u8,
    green: u8,
    blue: u8,
    #[palette_alpha]
    alpha: f32,
}

// We will write a conversion function for opaque RGB and derive
// will take care of preserving the transparency for us.
impl CssRgb {
    fn into_rgb_internal<S>(self) -> Rgb<Linear<S>, f32>
    where
        S: RgbSpace<WhitePoint = D65>,
    {
        Srgb::new(self.red, self.green, self.blue)
            .into_format()
            .into_rgb()
    }
}

fn main() {
    let css_color = CssRgb {
        red: 187,
        green: 0,
        blue: 255,
        alpha: 0.3,
    };
    let color = css_color.into();

    assert_relative_eq!(color, LinSrgba::new(0.496933, 0.0, 1.0, 0.3));
}

Required methods

fn into_xyz(self) -> Xyz<Wp, T>

Convert into XYZ space

Loading content...

Provided methods

fn into_yxy(self) -> Yxy<Wp, T>

Convert into Yxy color space

fn into_lab(self) -> Lab<Wp, T>

Convert into L*a*b* color space

fn into_lch(self) -> Lch<Wp, T>

Convert into L*C*h° color space

fn into_rgb<S: RgbSpace<WhitePoint = Wp>>(self) -> Rgb<Linear<S>, T>

Convert into RGB color space.

fn into_hsl<S: RgbSpace<WhitePoint = Wp>>(self) -> Hsl<S, T>

Convert into HSL color space

fn into_hsv<S: RgbSpace<WhitePoint = Wp>>(self) -> Hsv<S, T>

Convert into HSV color space

fn into_hwb<S: RgbSpace<WhitePoint = Wp>>(self) -> Hwb<S, T>

Convert into HWB color space

fn into_luma(self) -> Luma<Linear<Wp>, T>

Convert into Luma

Loading content...

Implementors

impl<S, T, Wp> IntoColor<Wp, T> for Rgb<S, T> where
    S: RgbStandard,
    T: Component + Float,
    Wp: WhitePoint,
    S::Space: RgbSpace<WhitePoint = Wp>, 
[src]

impl<S, Wp, T> IntoColor<Wp, T> for Luma<S, T> where
    S: LumaStandard<WhitePoint = Wp>,
    T: Component + Float,
    Wp: WhitePoint
[src]

impl<S, Wp, T> IntoColor<Wp, T> for Hsl<S, T> where
    T: Component + Float,
    Wp: WhitePoint,
    S: RgbSpace<WhitePoint = Wp>, 
[src]

impl<S, Wp, T> IntoColor<Wp, T> for Hsv<S, T> where
    T: Component + Float,
    Wp: WhitePoint,
    S: RgbSpace<WhitePoint = Wp>, 
[src]

impl<S, Wp, T> IntoColor<Wp, T> for Hwb<S, T> where
    T: Component + Float,
    Wp: WhitePoint,
    S: RgbSpace<WhitePoint = Wp>, 
[src]

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

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

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

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

Loading content...