use crate::Node; use self::color::Color; pub mod color; #[derive(Debug, Clone, Copy)] pub struct GrayscaleNode; impl Node for GrayscaleNode { type Output = Color; fn eval(self, color: Color) -> Color { let avg = (color.r() + color.g() + color.b()) / 3.0; Color::from_rgbaf32_unchecked(avg, avg, avg, color.a()) } } impl<'n> Node for &'n GrayscaleNode { type Output = Color; fn eval(self, color: Color) -> Color { let avg = (color.r() + color.g() + color.b()) / 3.0; Color::from_rgbaf32_unchecked(avg, avg, avg, color.a()) } } pub struct ForEachNode(pub MN); impl<'n, I: Iterator, MN: 'n, S> Node for &'n ForEachNode where &'n MN: Node, { type Output = (); fn eval(self, input: I) -> Self::Output { input.for_each(|x| (&self.0).eval(x)) } } /*pub struct MutWrapper(pub N); impl<'n, T: Clone, N> Node<&'n mut T> for &'n MutWrapper where &'n N: Node, { type Output = (); fn eval(self, value: &'n mut T) { *value = (&self.0).eval(value.clone()); } }*/ #[cfg(test)] mod test { use super::*; #[test] fn map_node() { // let array = &mut [Color::from_rgbaf32(1.0, 0.0, 0.0, 1.0).unwrap()]; (&GrayscaleNode).eval(Color::from_rgbf32_unchecked(1., 0., 0.)); /*let map = ForEachNode(MutWrapper(GrayscaleNode)); (&map).eval(array.iter_mut()); assert_eq!(array[0], Color::from_rgbaf32(0.33333334, 0.33333334, 0.33333334, 1.0).unwrap());*/ } }