pub trait FromComponents<C> {
// Required method
fn from_components(components: C) -> Self;
}
Expand description
Trait for casting a collection of colors from a collection of color components 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::FromComponents, 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];
assert_eq!(
<[Srgb<u8>; 2]>::from_components(array),
[Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)]
);
assert_eq!(
<&[Srgb<u8>]>::from_components(slice),
[Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)]
);
assert_eq!(
<&mut [Srgb<u8>]>::from_components(slice_mut),
[Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)]
);
assert_eq!(
Vec::<Srgb<u8>>::from_components(vec),
vec![Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)]
);
Owning types can be cast as slices, too:
use palette::{cast::FromComponents, Srgb};
let array: [_; 6] = [64, 139, 10, 93, 18, 214];
let mut vec: Vec<_> = vec![64, 139, 10, 93, 18, 214];
assert_eq!(
<&[Srgb<u8>]>::from_components(&array),
[Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)]
);
assert_eq!(
<&mut [Srgb<u8>]>::from_components(&mut vec),
[Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)]
);
This panics:
ⓘ
use palette::{cast::FromComponents, Srgb};
let components = &[64, 139, 10, 93, 18, 214, 0, 123]; // Not a multiple of 3
<&[Srgb<u8>]>::from_components(components);
Required Methods§
sourcefn from_components(components: C) -> Self
fn from_components(components: C) -> Self
Cast a collection of color components into an collection of colors.
§Panics
If the conversion can’t be done, such as when the number of items in
components
isn’t a multiple of the number of components in the color
type.
Object Safety§
This trait is not object safe.