pub trait ComponentsAs<C: ?Sized> {
// Required method
fn components_as(&self) -> &C;
}
Expand description
Trait for casting a reference to collection of color components into a reference to 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::ComponentsAs, Srgb};
let array: [_; 6] = [64, 139, 10, 93, 18, 214];
let slice: &[_] = &[64, 139, 10, 93, 18, 214];
let vec: Vec<_> = vec![64, 139, 10, 93, 18, 214];
let colors: &[Srgb<u8>] = array.components_as();
assert_eq!(colors, &[Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)]);
let colors: &[Srgb<u8>] = slice.components_as();
assert_eq!(colors, &[Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)]);
let colors: &[Srgb<u8>] = vec.components_as();
assert_eq!(colors, &[Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)]);
This panics:
ⓘ
use palette::{cast::ComponentsAs, Srgb};
let components = [64, 139, 10, 93, 18, 214, 0, 123]; // Not a multiple of 3
let colors: &[Srgb<u8>] = components.components_as();
Required Methods§
sourcefn components_as(&self) -> &C
fn components_as(&self) -> &C
Cast this collection of color components into a reference to 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.