colr 0.2.0

A general purpose, extensible color type unifying color models and their operations at the type level.
//! Channel layout permutation for RGB colors.

use crate::BackingStore;
use crate::ChannelMap;
use crate::Color;
use crate::model::Rgb;
use crate::primaries::Primaries;
use crate::transfer::TransferFunction;

impl<P, TF, L1> Color<[f32; 4], Rgb<P, TF, L1>>
where
    P: Primaries,
    TF: TransferFunction,
    L1: BackingStore<[f32; 4]> + ChannelMap<4>,
{
    /// Permute channel layout to `L2`, keeping primaries and transfer function.
    ///
    /// Pure index reorder with no arithmetic. Valid for any transfer function.
    #[inline(always)]
    pub fn swizzle<L2>(self) -> Color<[f32; 4], Rgb<P, TF, L2>>
    where
        L2: BackingStore<[f32; 4]> + ChannelMap<4>,
    {
        let s = self.inner();
        let [r1, g1, b1, a1] = L1::INDICES;
        let [r2, g2, b2, a2] = L2::INDICES;
        let mut out = [0.0f32; 4];
        out[r2] = s[r1];
        out[g2] = s[g1];
        out[b2] = s[b1];
        out[a2] = s[a1];
        Color::new(out)
    }
}

impl<P, TF, L1> Color<[f32; 3], Rgb<P, TF, L1>>
where
    P: Primaries,
    TF: TransferFunction,
    L1: BackingStore<[f32; 3]> + ChannelMap<3>,
{
    /// Permute channel layout to `L2`, keeping primaries and transfer function.
    ///
    /// Pure index reorder with no arithmetic. Valid for any transfer function.
    #[inline(always)]
    pub fn swizzle<L2>(self) -> Color<[f32; 3], Rgb<P, TF, L2>>
    where
        L2: BackingStore<[f32; 3]> + ChannelMap<3>,
    {
        let s = self.inner();
        let [r1, g1, b1] = L1::INDICES;
        let [r2, g2, b2] = L2::INDICES;
        let mut out = [0.0f32; 3];
        out[r2] = s[r1];
        out[g2] = s[g1];
        out[b2] = s[b1];
        Color::new(out)
    }
}