Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,24 +445,12 @@ impl<T: $($bound+)*> Pixel for $ident<T> {
pix
}

fn map<F>(& self, f: F) -> $ident<T> where F: FnMut(T) -> T {
let mut this = (*self).clone();
this.apply(f);
this
}

fn apply<F>(&mut self, mut f: F) where F: FnMut(T) -> T {
for v in &mut self.0 {
*v = f(*v)
}
}

fn map_with_alpha<F, G>(&self, f: F, g: G) -> $ident<T> where F: FnMut(T) -> T, G: FnMut(T) -> T {
let mut this = (*self).clone();
this.apply_with_alpha(f, g);
this
}

fn apply_with_alpha<F, G>(&mut self, mut f: F, mut g: G) where F: FnMut(T) -> T, G: FnMut(T) -> T {
const ALPHA: usize = $channels - $alphas;
for v in self.0[..ALPHA].iter_mut() {
Expand All @@ -475,12 +463,6 @@ impl<T: $($bound+)*> Pixel for $ident<T> {
}
}

fn map2<F>(&self, other: &Self, f: F) -> $ident<T> where F: FnMut(T, T) -> T {
let mut this = (*self).clone();
this.apply2(other, f);
this
}

fn apply2<F>(&mut self, other: &$ident<T>, mut f: F) where F: FnMut(T, T) -> T {
for (a, &b) in self.0.iter_mut().zip(other.0.iter()) {
*a = f(*a, b)
Expand Down
25 changes: 19 additions & 6 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,12 @@ pub trait Pixel: Copy + Clone {
/// Apply the function ```f``` to each channel of this pixel.
fn map<F>(&self, f: F) -> Self
where
F: FnMut(Self::Subpixel) -> Self::Subpixel;
F: FnMut(Self::Subpixel) -> Self::Subpixel,
{
let mut this = *self;
this.apply(f);
this
}

/// Apply the function ```f``` to each channel of this pixel.
fn apply<F>(&mut self, f: F)
Expand All @@ -520,7 +525,12 @@ pub trait Pixel: Copy + Clone {
fn map_with_alpha<F, G>(&self, f: F, g: G) -> Self
where
F: FnMut(Self::Subpixel) -> Self::Subpixel,
G: FnMut(Self::Subpixel) -> Self::Subpixel;
G: FnMut(Self::Subpixel) -> Self::Subpixel,
{
let mut this = *self;
this.apply_with_alpha(f, g);
this
}

/// Apply the function ```f``` to each channel except the alpha channel.
/// Apply the function ```g``` to the alpha channel. Works in-place.
Expand All @@ -534,9 +544,7 @@ pub trait Pixel: Copy + Clone {
where
F: FnMut(Self::Subpixel) -> Self::Subpixel,
{
let mut this = *self;
this.apply_with_alpha(f, |x| x);
this
self.map_with_alpha(f, |x| x)
}

/// Apply the function ```f``` to each channel except the alpha channel.
Expand All @@ -552,7 +560,12 @@ pub trait Pixel: Copy + Clone {
/// ```other``` pairwise.
fn map2<F>(&self, other: &Self, f: F) -> Self
where
F: FnMut(Self::Subpixel, Self::Subpixel) -> Self::Subpixel;
F: FnMut(Self::Subpixel, Self::Subpixel) -> Self::Subpixel,
{
let mut this = *self;
this.apply2(other, f);
this
}

/// Apply the function ```f``` to each channel of this pixel and
/// ```other``` pairwise. Works in-place.
Expand Down
Loading