Trait cosmic::cosmic_theme::palette::cast::ComponentsInto

source ·
pub trait ComponentsInto<C> {
    // Required method
    fn components_into(self) -> C;
}
Expand description

Trait for casting a collection of color components from a collection of colors without copying.

This trait is meant as a more convenient alternative to the free functions in cast, to allow method chaining among other things.

§Panics

The cast will panic if the cast fails, such as when the length of the input is not a multiple of the color’s array length.

§Examples

use palette::{cast::ComponentsInto, Srgb};

let array: [_; 6] = [64, 139, 10, 93, 18, 214];
let slice: &[_] = &[64, 139, 10, 93, 18, 214];
let slice_mut: &mut [_] = &mut [64, 139, 10, 93, 18, 214];
let vec: Vec<_> = vec![64, 139, 10, 93, 18, 214];

let colors: [Srgb<u8>; 2] = array.components_into();
assert_eq!(colors, [Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)]);

let colors: &[Srgb<u8>] = slice.components_into();
assert_eq!(colors, [Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)]);

let colors: &mut [Srgb<u8>] = slice_mut.components_into();
assert_eq!(colors, [Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)]);

let colors: Vec<Srgb<u8>> = vec.components_into();
assert_eq!(colors, vec![Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)]);

Owning types can be cast as slices, too:

use palette::{cast::ComponentsInto, Srgb};

let array: [_; 6] = [64, 139, 10, 93, 18, 214];
let mut vec: Vec<_> = vec![64, 139, 10, 93, 18, 214];

let colors: &[Srgb<u8>] = (&array).components_into();
assert_eq!(colors, [Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)]);

let colors: &mut [Srgb<u8>] = (&mut vec).components_into();
assert_eq!(colors, [Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)]);

This panics:

use palette::{cast::ComponentsInto, Srgb};

let components = &[64, 139, 10, 93, 18, 214, 0, 123]; // Not a multiple of 3
let colors: &[Srgb<u8>] = components.components_into();

Required Methods§

source

fn components_into(self) -> C

Cast this collection of color components into a collection of colors.

§Panics

If the conversion can’t be done, such as when the number of items in self isn’t a multiple of the number of components in the color type.

Implementors§

source§

impl<T, C> ComponentsInto<C> for T