Skip to content
Open
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
33 changes: 33 additions & 0 deletions src/bitmap/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,15 @@ impl BitMap {
}
}

///
/// Invert the colors of the image
///
pub fn invert(&mut self) {
for c in &mut self.pixels {
c.invert();
}
}

///
/// Find all the pixels that are the same as the from color and convert them
/// all to the "to" color.
Expand Down Expand Up @@ -1147,4 +1156,28 @@ mod test {
assert!(pixel.is_black())
}
}

#[test]
fn invert_test() {
//create a some images of B/W
let originally_white = BitMap::new(10, 10);
let mut originally_black = BitMap::new(10, 10);
originally_black.clear_image(Rgba::black());

//clone them
let mut make_white = originally_black.clone();
let mut make_black = originally_white.clone();

//sanity
assert_eq!(make_black, originally_white);
assert_eq!(make_white, originally_black);

//invert them
make_white.invert();
make_black.invert();

//test
assert_eq!(make_black, originally_black);
assert_eq!(make_white, originally_white);
}
}
9 changes: 9 additions & 0 deletions src/bitmap/rgba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@ impl Rgba {
self.set_gray_scale_pixel(pixel_gray);
}

///
/// Invert the pixel color
///
pub fn invert(&mut self) {
self.red = 255 - self.red.clamp(0, 255);
self.blue = 255 - self.blue.clamp(0, 255);
self.green = 255 - self.green.clamp(0, 255);
}

///
/// set all the colors to the same color
///
Expand Down