commit cb8825df2f6a336334c4e43a97fc0b52846a2129
parent c3f89c32a394a3820cfe4226f95f3f2672fcee26
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Thu, 30 Mar 2023 21:05:02 -0700
cleanup
Diffstat:
1 file changed, 13 insertions(+), 6 deletions(-)
diff --git a/src/lechatphp/mod.rs b/src/lechatphp/mod.rs
@@ -213,7 +213,7 @@ fn solve_path(starting: Letter, letters_map: &HashMap<char, Letter>, img: &Dynam
let mut dest_count = HashMap::<Letter, usize>::new();
for dest in remaining.iter() {
let red = Bresenham::new(letter.center().into(), dest.center().into())
- .filter(|(x, y)| img.get_pixel(*x as u32, *y as u32) == *RED_COLOR)
+ .filter(|(x, y)| is_red(img.get_pixel(*x as u32, *y as u32)))
.count();
dest_count.insert(dest.clone(), red);
}
@@ -317,8 +317,7 @@ fn img_contains_letter(img: &DynamicImage, letter_img: &DynamicImage) -> bool {
let good_letter_color = letter_img.get_pixel(x, y);
let letter_img_color = img.get_pixel(x, y);
// If we find an Off pixel where it's supposed to be On, skip that letter
- if good_letter_color == *ON_COLOR &&
- (letter_img_color != *ON_COLOR && letter_img_color != *RED_COLOR) {
+ if is_on(good_letter_color) && !is_on(letter_img_color) {
return false;
}
}
@@ -326,22 +325,30 @@ fn img_contains_letter(img: &DynamicImage, letter_img: &DynamicImage) -> bool {
true
}
+fn is_on(c: Rgba<u8>) -> bool {
+ c == *ON_COLOR || c == *RED_COLOR
+}
+
+fn is_red(c: Rgba<u8>) -> bool {
+ c == *RED_COLOR
+}
+
fn has_red_in_center_area(letter_img: &DynamicImage) -> bool {
letter_img.view(LETTER_WIDTH/2 - 1, LETTER_HEIGHT/2 - 1, 2, 2)
.pixels()
- .any(|(_, _, c)| c == *RED_COLOR )
+ .any(|(_, _, c)| is_red(c))
}
// Count pixels that are On (either white or red)
fn count_px_on(img: &DynamicImage) -> usize {
img.pixels()
- .filter(|(_, _, c)| *c == *ON_COLOR || *c == *RED_COLOR)
+ .filter(|(_, _, c)| is_on(*c))
.count()
}
// Count pixels that are red
fn count_red_px(img: &DynamicImage) -> usize {
img.pixels()
- .filter(|(_, _, c)| *c == *RED_COLOR)
+ .filter(|(_, _, c)| is_red(*c))
.count()
}
\ No newline at end of file