palette

Trait 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>

source§

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

source§

type Color = Luma<S, T>

source§

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

source§

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

source§

type Color = Rgb<S, T>

source§

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

source§

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

source§

type Color = Hsl<S, T>

source§

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

source§

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

source§

type Color = Hsv<S, T>

source§

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

source§

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

source§

type Color = Hwb<S, T>

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

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,

source§

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

source§

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

source§

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

source§

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

source§

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

source§

type Color = Hsluv<Wp, T>

source§

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

source§

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

source§

type Color = Lab<Wp, T>

source§

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

source§

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

source§

type Color = Lch<Wp, T>

source§

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

source§

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

source§

type Color = Lchuv<Wp, T>

source§

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

source§

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

source§

type Color = Luv<Wp, T>

source§

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

source§

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

source§

type Color = Xyz<Wp, T>

source§

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

source§

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

source§

type Color = Yxy<Wp, T>

source§

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