colr 0.3.1

A general purpose, extensible color type unifying color models and their operations at the type level.
Documentation
//! Element-wise channel clamping.

use crate::{BackingStore, Color};

impl<M> Color<f32, M>
where
    M: BackingStore<f32>,
{
    /// Clamp the channel to `[min, max]`.
    #[inline(always)]
    pub fn clamp(self, min: f32, max: f32) -> Self {
        Color::new(self.inner().clamp(min, max))
    }
}

macro_rules! impl_clamp_array {
    ($n:literal) => {
        impl<M> Color<[f32; $n], M>
        where
            M: BackingStore<[f32; $n]>,
        {
            /// Clamp every channel to `[min, max]`.
            #[inline(always)]
            pub fn clamp(self, min: f32, max: f32) -> Self {
                Color::new(self.inner().map(|v| v.clamp(min, max)))
            }
        }
    };
}

impl_clamp_array!(2);
impl_clamp_array!(3);
impl_clamp_array!(4);

#[cfg(feature = "glam")]
macro_rules! impl_clamp_vec {
    ($vec:ty) => {
        impl<M> Color<$vec, M>
        where
            M: BackingStore<$vec>,
        {
            /// Clamp every channel to `[min, max]`.
            #[inline(always)]
            pub fn clamp(self, min: f32, max: f32) -> Self {
                Color::new(
                    self.inner()
                        .clamp(<$vec>::splat(min), <$vec>::splat(max)),
                )
            }
        }
    };
}

#[cfg(feature = "glam")]
impl_clamp_vec!(glam::Vec2);
#[cfg(feature = "glam")]
impl_clamp_vec!(glam::Vec3);
#[cfg(feature = "glam")]
impl_clamp_vec!(glam::Vec3A);
#[cfg(feature = "glam")]
impl_clamp_vec!(glam::Vec4);