dithor/
square.rs

1use crate::color_finder::ColorInfo;
2use crate::coordinates::Coordinates;
3use image::Rgba;
4use image::RgbaImage;
5
6pub struct Square {
7    xoffset: u32,
8    yoffset: u32,
9    size: u32,
10    filling_coordinates: Coordinates,
11    base_color: Rgba<u8>,
12    filling_color: Rgba<u8>,
13}
14
15impl Square {
16    pub fn new(xoffset: u32, yoffset: u32, size: u32, colors_info: ColorInfo) -> Self {
17        let filling_coordinates = Coordinates::new(xoffset, yoffset, colors_info.level, size);
18        Self {
19            xoffset,
20            yoffset,
21            size,
22            filling_coordinates,
23            base_color: colors_info.base_color,
24            filling_color: colors_info.filling_color,
25        }
26    }
27
28    pub fn colorize(&self, image: RgbaImage) -> RgbaImage {
29        let base_image = self.write_base(image);
30        let colorized_image = self.write_filling(base_image);
31        colorized_image
32    }
33
34    fn write_base(&self, mut image: RgbaImage) -> RgbaImage {
35        for x in 0..self.size {
36            for y in 0..self.size {
37                image.put_pixel(self.xoffset + x, self.yoffset + y, self.base_color);
38            }
39        }
40        image
41    }
42
43    fn write_filling(&self, mut image: RgbaImage) -> RgbaImage {
44        for coordinates in self.filling_coordinates.coordinates.iter() {
45            image.put_pixel(coordinates.0, coordinates.1, self.filling_color);
46        }
47        image
48    }
49}