colr 0.3.1

A general purpose, extensible color type unifying color models and their operations at the type level.
Documentation
//! `Color::decode` method impls. Applies an EOTF to stored RGB/luma values.

use crate::BackingStore;
use crate::ChannelMap;
use crate::Color;
use crate::model::{Luma, LumaAlpha, Rgb};
use crate::primaries::Primaries;
use crate::transfer::Linear;
pub use colr_types::decode::Decode;

impl<P, TF> Color<f32, Luma<P, TF>>
where
    P: Primaries,
    TF: Decode<f32>,
{
    /// Decode encoded luma to linear light.
    #[inline(always)]
    pub fn decode(self) -> Color<f32, Luma<P, Linear>> {
        Color::new(TF::decode(self.inner()))
    }
}

impl<P, TF, A> Color<[f32; 2], LumaAlpha<P, TF, A>>
where
    P: Primaries,
    TF: Decode<f32>,
    A: crate::AlphaState,
{
    /// Decode encoded luma to linear light. Alpha passes through unchanged.
    #[inline(always)]
    pub fn decode(self) -> Color<[f32; 2], LumaAlpha<P, Linear, A>> {
        let [y, a] = self.inner();
        Color::new([TF::decode(y), a])
    }
}

impl<P, TF, L> Color<[f32; 3], Rgb<P, TF, L>>
where
    P: Primaries,
    TF: Decode<[f32; 3]>,
    L: BackingStore<[f32; 3]> + ChannelMap<3>,
{
    /// Decode the stored signal to linear light.
    #[inline(always)]
    pub fn decode(self) -> Color<[f32; 3], Rgb<P, Linear, L>> {
        let s = self.inner();
        let [ri, gi, bi] = L::INDICES;
        let linear = TF::decode([s[ri], s[gi], s[bi]]);
        let mut out = [0.0f32; 3];
        out[ri] = linear[0];
        out[gi] = linear[1];
        out[bi] = linear[2];
        Color::new(out)
    }
}

impl<P, TF, L> Color<[f32; 4], Rgb<P, TF, L>>
where
    P: Primaries,
    TF: Decode<[f32; 3]>,
    L: BackingStore<[f32; 4]> + ChannelMap<4>,
{
    /// Decode the stored signal to linear light. Alpha passes through unchanged.
    #[inline(always)]
    pub fn decode(self) -> Color<[f32; 4], Rgb<P, Linear, L>> {
        let s = self.inner();
        let [ri, gi, bi, ai] = L::INDICES;
        let linear = TF::decode([s[ri], s[gi], s[bi]]);
        let mut out = [0.0f32; 4];
        out[ri] = linear[0];
        out[gi] = linear[1];
        out[bi] = linear[2];
        out[ai] = s[ai];
        Color::new(out)
    }
}