palette::cast

Function try_from_component_slice_box

source
pub fn try_from_component_slice_box<T>(
    values: Box<[<T::Array as ArrayExt>::Item]>,
) -> Result<Box<[T]>, BoxedSliceCastError<<T::Array as ArrayExt>::Item>>
where T: ArrayCast,
Expand description

Cast from a boxed slice of color components to a boxed 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 = vec![64, 139, 10, 93, 18, 214].into_boxed_slice();
assert_eq!(
    cast::try_from_component_slice_box::<Srgb<u8>>(components),
    Ok(vec![Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)].into_boxed_slice())
)

This produces an error:

use palette::{cast, Srgb};

// Not a multiple of 3:
let components = vec![64, 139, 10, 93, 18].into_boxed_slice();

if let Err(error) = cast::try_from_component_slice_box::<Srgb<u8>>(components) {
    // We get the original values back on error:
    assert_eq!(
        error.values,
        vec![64, 139, 10, 93, 18].into_boxed_slice()
    );
} else {
    unreachable!();
}