Trait palette::WithAlpha

source ·
pub trait WithAlpha<A>: Sized {
    type Color;
    type WithAlpha: WithAlpha<A, Color = Self::Color, WithAlpha = Self::WithAlpha>;

    // Required methods
    fn with_alpha(self, alpha: A) -> Self::WithAlpha;
    fn without_alpha(self) -> Self::Color;
    fn split(self) -> (Self::Color, A);

    // Provided methods
    fn opaque(self) -> Self::WithAlpha
       where A: Stimulus { ... }
    fn transparent(self) -> Self::WithAlpha
       where A: Zero { ... }
}
Expand description

A trait for color types that can have or be given transparency (alpha channel).

WithAlpha is an interface for adding, removing and setting the alpha channel of a color type. The color type itself doesn’t need to store the transparency value as it can be transformed into or wrapped in a type that has a representation of transparency. This would typically be done by wrapping it in an Alpha instance.

§Deriving

The trait is trivial enough to be automatically derived. If the color type has a field for transparency (an alpha channel), it has to be marked with #[palette(alpha)] to be taken into account.

Derived without an internal alpha channel:

use palette::WithAlpha;

#[derive(WithAlpha)]
struct CustomColor {
    redness: f32,
    glow: f32,
    glitter: f32,
}

let color = CustomColor {
    redness: 0.8,
    glow: 2.5,
    glitter: 1000.0
};
let transparent = color.with_alpha(0.3);

assert_eq!(transparent.alpha, 0.3);

Derived with an internal alpha channel:

use palette::WithAlpha;

#[derive(WithAlpha)]
struct CustomColor {
    redness: f32,
    glow: f32,
    glitter: f32,

    #[palette(alpha)]
    alpha: u8,
}

let color = CustomColor {
    redness: 0.8,
    glow: 2.5,
    glitter: 1000.0,
    alpha: 255
};
let transparent = color.with_alpha(10);

assert_eq!(transparent.alpha, 10);

Required Associated Types§

source

type Color

The opaque color type, without any transparency.

This is typically Self.

source

type WithAlpha: WithAlpha<A, Color = Self::Color, WithAlpha = Self::WithAlpha>

The color type with transparency applied.

This is typically Alpha<Self::Color, A>.

Required Methods§

source

fn with_alpha(self, alpha: A) -> Self::WithAlpha

Transforms the color into a transparent color with the provided alpha value. If Self already has a transparency, it is overwritten.

use palette::{Srgb, WithAlpha};

let color = Srgb::new(255u8, 0, 255);

// This results in an `Alpha<Srgb<u8>, f32>`
let transparent = color.with_alpha(0.3f32);
assert_eq!(transparent.alpha, 0.3);

// This changes the transparency to 0.8
let transparent = transparent.with_alpha(0.8f32);
assert_eq!(transparent.alpha, 0.8);
source

fn without_alpha(self) -> Self::Color

Removes the transparency from the color. If Self::Color has an internal transparency field, that field will be set to A::max_intensity() to make it opaque.

use palette::{Srgba, Srgb, WithAlpha};

let transparent = Srgba::new(255u8, 0, 255, 10);

// This unwraps the color information from the `Alpha` wrapper
let color = transparent.without_alpha();
assert_eq!(transparent.color, color);
source

fn split(self) -> (Self::Color, A)

Splits the color into separate color and transparency values.

A color without any transparency field will return A::max_intensity() instead. If Self::Color has an internal transparency field, that field will be set to A::max_intensity() to make it opaque.

use palette::{Srgba, Srgb, WithAlpha};

let transparent = Srgba::new(255u8, 0, 255, 10);

// This unwraps both the color and alpha from the `Alpha` wrapper
let (color, alpha) = transparent.split();
assert_eq!(transparent.color, color);
assert_eq!(transparent.alpha, alpha);

Provided Methods§

source

fn opaque(self) -> Self::WithAlpha
where A: Stimulus,

Transforms the color into a fully opaque color with a transparency field. If Self already has a transparency, it is overwritten.

use palette::{Srgb, Srgba, WithAlpha};

let color = Srgb::new(255u8, 0, 255);

let opaque: Srgba<u8> = color.opaque();
assert_eq!(opaque.alpha, 255);
source

fn transparent(self) -> Self::WithAlpha
where A: Zero,

Transforms the color into a fully transparent color. If Self already has a transparency, it is overwritten.

use palette::{Srgb, Srgba, WithAlpha};

let color = Srgb::new(255u8, 0, 255);

let transparent: Srgba<u8> = color.transparent();
assert_eq!(transparent.alpha, 0);

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<C, A> WithAlpha<A> for Alpha<C, A>

§

type Color = C

§

type WithAlpha = Alpha<C, A>

source§

impl<S, T, _A> WithAlpha<_A> for Luma<S, T>
where _A: Stimulus,

§

type Color = Luma<S, T>

§

type WithAlpha = Alpha<Luma<S, T>, _A>

source§

impl<S, T, _A> WithAlpha<_A> for Rgb<S, T>
where _A: Stimulus,

§

type Color = Rgb<S, T>

§

type WithAlpha = Alpha<Rgb<S, T>, _A>

source§

impl<S, T, _A> WithAlpha<_A> for Hsl<S, T>
where _A: Stimulus,

§

type Color = Hsl<S, T>

§

type WithAlpha = Alpha<Hsl<S, T>, _A>

source§

impl<S, T, _A> WithAlpha<_A> for Hsv<S, T>
where _A: Stimulus,

§

type Color = Hsv<S, T>

§

type WithAlpha = Alpha<Hsv<S, T>, _A>

source§

impl<S, T, _A> WithAlpha<_A> for Hwb<S, T>
where _A: Stimulus,

§

type Color = Hwb<S, T>

§

type WithAlpha = Alpha<Hwb<S, T>, _A>

source§

impl<T, _A> WithAlpha<_A> for Cam16Jch<T>
where _A: Stimulus,

§

type Color = Cam16Jch<T>

§

type WithAlpha = Alpha<Cam16Jch<T>, _A>

source§

impl<T, _A> WithAlpha<_A> for Cam16Jmh<T>
where _A: Stimulus,

§

type Color = Cam16Jmh<T>

§

type WithAlpha = Alpha<Cam16Jmh<T>, _A>

source§

impl<T, _A> WithAlpha<_A> for Cam16Jsh<T>
where _A: Stimulus,

§

type Color = Cam16Jsh<T>

§

type WithAlpha = Alpha<Cam16Jsh<T>, _A>

source§

impl<T, _A> WithAlpha<_A> for Cam16Qch<T>
where _A: Stimulus,

§

type Color = Cam16Qch<T>

§

type WithAlpha = Alpha<Cam16Qch<T>, _A>

source§

impl<T, _A> WithAlpha<_A> for Cam16Qmh<T>
where _A: Stimulus,

§

type Color = Cam16Qmh<T>

§

type WithAlpha = Alpha<Cam16Qmh<T>, _A>

source§

impl<T, _A> WithAlpha<_A> for Cam16Qsh<T>
where _A: Stimulus,

§

type Color = Cam16Qsh<T>

§

type WithAlpha = Alpha<Cam16Qsh<T>, _A>

source§

impl<T, _A> WithAlpha<_A> for Cam16<T>
where _A: Stimulus,

§

type Color = Cam16<T>

§

type WithAlpha = Alpha<Cam16<T>, _A>

source§

impl<T, _A> WithAlpha<_A> for Cam16UcsJab<T>
where _A: Stimulus,

source§

impl<T, _A> WithAlpha<_A> for Cam16UcsJmh<T>
where _A: Stimulus,

source§

impl<T, _A> WithAlpha<_A> for Okhsl<T>
where _A: Stimulus,

§

type Color = Okhsl<T>

§

type WithAlpha = Alpha<Okhsl<T>, _A>

source§

impl<T, _A> WithAlpha<_A> for Okhsv<T>
where _A: Stimulus,

§

type Color = Okhsv<T>

§

type WithAlpha = Alpha<Okhsv<T>, _A>

source§

impl<T, _A> WithAlpha<_A> for Okhwb<T>
where _A: Stimulus,

§

type Color = Okhwb<T>

§

type WithAlpha = Alpha<Okhwb<T>, _A>

source§

impl<T, _A> WithAlpha<_A> for Oklab<T>
where _A: Stimulus,

§

type Color = Oklab<T>

§

type WithAlpha = Alpha<Oklab<T>, _A>

source§

impl<T, _A> WithAlpha<_A> for Oklch<T>
where _A: Stimulus,

§

type Color = Oklch<T>

§

type WithAlpha = Alpha<Oklch<T>, _A>

source§

impl<Wp, T, _A> WithAlpha<_A> for Hsluv<Wp, T>
where _A: Stimulus,

§

type Color = Hsluv<Wp, T>

§

type WithAlpha = Alpha<Hsluv<Wp, T>, _A>

source§

impl<Wp, T, _A> WithAlpha<_A> for Lab<Wp, T>
where _A: Stimulus,

§

type Color = Lab<Wp, T>

§

type WithAlpha = Alpha<Lab<Wp, T>, _A>

source§

impl<Wp, T, _A> WithAlpha<_A> for Lch<Wp, T>
where _A: Stimulus,

§

type Color = Lch<Wp, T>

§

type WithAlpha = Alpha<Lch<Wp, T>, _A>

source§

impl<Wp, T, _A> WithAlpha<_A> for Lchuv<Wp, T>
where _A: Stimulus,

§

type Color = Lchuv<Wp, T>

§

type WithAlpha = Alpha<Lchuv<Wp, T>, _A>

source§

impl<Wp, T, _A> WithAlpha<_A> for Luv<Wp, T>
where _A: Stimulus,

§

type Color = Luv<Wp, T>

§

type WithAlpha = Alpha<Luv<Wp, T>, _A>

source§

impl<Wp, T, _A> WithAlpha<_A> for Xyz<Wp, T>
where _A: Stimulus,

§

type Color = Xyz<Wp, T>

§

type WithAlpha = Alpha<Xyz<Wp, T>, _A>

source§

impl<Wp, T, _A> WithAlpha<_A> for Yxy<Wp, T>
where _A: Stimulus,

§

type Color = Yxy<Wp, T>

§

type WithAlpha = Alpha<Yxy<Wp, T>, _A>