colr 0.3.1

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

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

impl<P> Color<f32, Luma<P, Linear>>
where
    P: Primaries,
{
    /// Encode linear luma to the target transfer function `TF2`.
    #[inline(always)]
    pub fn encode<TF2: Encode<f32>>(self) -> Color<f32, Luma<P, TF2>> {
        Color::new(TF2::encode(self.inner()))
    }
}

impl<P, A> Color<[f32; 2], LumaAlpha<P, Linear, A>>
where
    P: Primaries,
    A: crate::AlphaState,
{
    /// Encode linear luma to the target transfer function `TF2`.
    /// Alpha passes through unchanged.
    #[inline(always)]
    pub fn encode<TF2: Encode<f32>>(self) -> Color<[f32; 2], LumaAlpha<P, TF2, A>> {
        let [y, a] = self.inner();
        Color::new([TF2::encode(y), a])
    }
}

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

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