palette::cast

Function try_from_component_slice

source
pub fn try_from_component_slice<T>(
    values: &[<T::Array as ArrayExt>::Item],
) -> Result<&[T], SliceCastError>
where T: ArrayCast,
Expand description

Cast from a slice of color components to a slice of colors.

§Errors

The cast will return an error if the length of the input slice is not a multiple of the color’s array length.

§Examples

use palette::{cast, Srgb};

let components = &[64, 139, 10, 93, 18, 214];
assert_eq!(
    cast::try_from_component_slice::<Srgb<u8>>(components),
    Ok([Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)].as_ref())
)

This produces an error:

use palette::{cast, Srgb};

let components = &[64, 139, 10, 93, 18]; // Not a multiple of 3
assert!(cast::try_from_component_slice::<Srgb<u8>>(components).is_err());