pub fn try_from_component_vec<T>(
values: Vec<<T::Array as ArrayExt>::Item>,
) -> Result<Vec<T>, VecCastError<<T::Array as ArrayExt>::Item>>where
T: ArrayCast,
Expand description
Cast from a Vec
of color components to a Vec
of colors.
§Errors
The cast will return an error if the length or capacity of the input Vec
is not a multiple of the color’s array length.
§Examples
use palette::{cast, Srgb};
let components = vec![64, 139, 10, 93, 18, 214];
assert_eq!(
cast::try_from_component_vec::<Srgb<u8>>(components),
Ok(vec![Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)])
)
This produces an error due to the incorrect length:
use palette::{cast, Srgb};
// Not a multiple of 3:
let components = vec![64, 139, 10, 93, 18];
if let Err(error) = cast::try_from_component_vec::<Srgb<u8>>(components) {
// We get the original values back on error:
assert_eq!(error.values, vec![64, 139, 10, 93, 18]);
} else {
unreachable!();
}
This produces an error due to the incorrect capacity:
use palette::{cast, Srgb};
let mut components = vec![64, 139, 10, 93, 18, 214];
components.reserve_exact(2); // Not a multiple of 3
if let Err(error) = cast::try_from_component_vec::<Srgb<u8>>(components) {
// We get the original values back on error:
assert_eq!(error.values, vec![64, 139, 10, 93, 18, 214]);
assert_eq!(error.values.capacity(), 8);
} else {
unreachable!();
}