use crate::image::Number;
pub trait Pixel<T: Number> {
fn alpha(&self) -> T;
fn channels_without_alpha(&self) -> &[T];
fn map_all<S: Number, F>(&self, f: F) -> Vec<S>
where F: Fn(T) -> S;
fn map_alpha<S: Number, F, G>(&self, f: F, g: G) -> Vec<S>
where F: Fn(T) -> S,
G: Fn(T) -> S;
fn apply<F>(&mut self, f: F)
where F: Fn(T) -> T;
fn apply_alpha<F, G>(&mut self, f: F, g: G)
where F: Fn(T) -> T,
G: Fn(T) -> T;
fn is_black(&self) -> bool;
fn is_black_alpha(&self) -> bool;
}
impl<T: Number> Pixel<T> for [T] {
fn alpha(&self) -> T {
self[self.len()-1]
}
fn channels_without_alpha(&self) -> &[T] {
&self[..(self.len()-1)]
}
fn map_all<S: Number, F>(&self, f: F) -> Vec<S>
where F: Fn(T) -> S {
let mut channels_out = Vec::new();
for channel in self.iter() {
channels_out.push(f(*channel));
}
channels_out
}
fn map_alpha<S: Number, F, G>(&self, f: F, g: G) -> Vec<S>
where F: Fn(T) -> S,
G: Fn(T) -> S {
let mut channels_out = Vec::new();
for channel in self.channels_without_alpha().iter() {
channels_out.push(f(*channel));
}
channels_out.push(g(self.alpha()));
channels_out
}
fn apply<F>(&mut self, f: F)
where F: Fn(T) -> T {
for i in 0..self.len() {
self[i] = f(self[i]);
}
}
fn apply_alpha<F, G>(&mut self, f: F, g: G)
where F: Fn(T) -> T,
G: Fn(T) -> T {
for i in 0..(self.len() - 1) {
self[i] = f(self[i]);
}
self[self.len()-1] = g(self.alpha());
}
fn is_black(&self) -> bool {
for channel in self.iter() {
if *channel != 0.into() {
return false;
}
}
true
}
fn is_black_alpha(&self) -> bool {
for channel in self.channels_without_alpha().iter() {
if *channel != 0.into() {
return false;
}
}
true
}
}