Fills the 4-connected region around `xy` via `rasops`.
(rasops: &mut R, xy: PixelsXY, fill_color: u8)
| 124 | |
| 125 | /// Fills the 4-connected region around `xy` via `rasops`. |
| 126 | pub fn bucket_fill<R>(rasops: &mut R, xy: PixelsXY, fill_color: u8) -> io::Result<()> |
| 127 | where |
| 128 | R: RasterOps, |
| 129 | { |
| 130 | let seed_color = match rasops.peek_pixel(xy)? { |
| 131 | Some(color) => color, |
| 132 | None => return Ok(()), |
| 133 | }; |
| 134 | if seed_color == fill_color { |
| 135 | return Ok(()); |
| 136 | } |
| 137 | |
| 138 | let mut pending = vec![xy]; |
| 139 | while let Some(xy) = pending.pop() { |
| 140 | if rasops.peek_pixel(xy)? != Some(seed_color) { |
| 141 | continue; |
| 142 | } |
| 143 | |
| 144 | rasops.draw_pixel(xy)?; |
| 145 | |
| 146 | if xy.x > i16::MIN { |
| 147 | pending.push(PixelsXY::new(xy.x - 1, xy.y)); |
| 148 | } |
| 149 | if xy.x < i16::MAX { |
| 150 | pending.push(PixelsXY::new(xy.x + 1, xy.y)); |
| 151 | } |
| 152 | if xy.y > i16::MIN { |
| 153 | pending.push(PixelsXY::new(xy.x, xy.y - 1)); |
| 154 | } |
| 155 | if xy.y < i16::MAX { |
| 156 | pending.push(PixelsXY::new(xy.x, xy.y + 1)); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | Ok(()) |
| 161 | } |
| 162 | |
| 163 | /// Draws a circle via `rasops` with `center` and `radius`. |
| 164 | /// |