colr 0.2.0

A general purpose, extensible color type unifying color models and their operations at the type level.
//! `Color::tonemap` method impls. Applies tone mapping to linear colors.

use colr_types::tonemap::Tonemap;

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

impl<P, L> Color<[f32; 3], Rgb<P, Linear, L>>
where
    P: Primaries,
    L: BackingStore<[f32; 3]> + ChannelMap<3>,
{
    /// Apply a tone mapping operator to unbounded scene-linear light,
    /// returning bounded display-linear light.
    #[inline(always)]
    pub fn tonemap<TMO: Tonemap<[f32; 3]>>(self) -> Self {
        let s = self.inner();
        let [ri, gi, bi] = L::INDICES;
        let mapped = TMO::tonemap([s[ri], s[gi], s[bi]]);
        let mut out = [0.0f32; 3];
        out[ri] = mapped[0];
        out[gi] = mapped[1];
        out[bi] = mapped[2];
        Color::new(out)
    }
}

impl<P, L> Color<[f32; 4], Rgb<P, Linear, L>>
where
    P: Primaries,
    L: BackingStore<[f32; 4]> + ChannelMap<4>,
{
    /// Apply a tone mapping operator to unbounded scene-linear light.
    /// Alpha passes through unchanged.
    #[inline(always)]
    pub fn tonemap<TMO: Tonemap<[f32; 3]>>(self) -> Self {
        let s = self.inner();
        let [ri, gi, bi, ai] = L::INDICES;
        let mapped = TMO::tonemap([s[ri], s[gi], s[bi]]);
        let mut out = [0.0f32; 4];
        out[ri] = mapped[0];
        out[gi] = mapped[1];
        out[bi] = mapped[2];
        out[ai] = s[ai];
        Color::new(out)
    }
}