From 3ce85fcb5eda8fcb871bdc20552e06dcb652313d Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Mon, 20 Jul 2020 21:05:25 +0100 Subject: [PATCH] Image.invert() --- src/image.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/image.rs b/src/image.rs index 3090a69..c9679e7 100644 --- a/src/image.rs +++ b/src/image.rs @@ -4,6 +4,10 @@ pub struct Image { } impl Image { + pub fn invert(&mut self) { + self.pixels = self.pixels.iter().map(|pixel| 1 - pixel).collect(); + } + /// flip image vertically pub fn flip(&mut self) { let mut pixels = Vec::with_capacity(64); @@ -159,6 +163,14 @@ mod test { assert_eq!(output, expected); } + #[test] + fn invert() { + let mut output = crate::mock::image::chequers_1(); + output.invert(); + let expected = crate::mock::image::chequers_2(); + assert_eq!(output, expected); + } + #[test] fn flip() { let mut image = crate::mock::image::asymmetrical();