Skip to content

Commit 51d822b

Browse files
authored
🔀 Merge pull request #11 from davep/clear-pixel
Add `clear_pixels` and `cear_pixel`
2 parents b3336d4 + 371777e commit 51d822b

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
- Added `set_pen`. ([#7](https://github.com/davep/textual-canvas/pull/7))
2020
- Fixed off-by-one issue with `draw_rectangle`.
2121
([#10](https://github.com/davep/textual-canvas/pull/10))
22+
- Added `Canvas.clear_pixels`.
23+
([#11](https://github.com/davep/textual-canvas/pull/11))
24+
- Added `Canvas.clear_pixel`.
25+
([#11](https://github.com/davep/textual-canvas/pull/11))
2226

2327
## v0.2.0
2428

src/textual_canvas/canvas.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,28 @@ def set_pixels(
199199
self.refresh()
200200
return self
201201

202+
def clear_pixels(self, locations: Iterable[tuple[int, int]]) -> Self:
203+
"""Clear the colour of a collection of pixels on the canvas.
204+
205+
Args:
206+
locations: An iterable of tuples of x and y location.
207+
208+
Returns:
209+
The canvas.
210+
211+
Raises:
212+
CanvasError: If any pixel location is not within the canvas.
213+
214+
Note:
215+
The origin of the canvas is the top left corner.
216+
"""
217+
color = self._canvas_colour or self.styles.background
218+
for x, y in locations:
219+
self._pixel_check(x, y)
220+
self._canvas[y][x] = color
221+
self.refresh()
222+
return self
223+
202224
def set_pixel(self, x: int, y: int, color: Color | None = None) -> Self:
203225
"""Set the colour of a specific pixel on the canvas.
204226
@@ -215,6 +237,21 @@ def set_pixel(self, x: int, y: int, color: Color | None = None) -> Self:
215237
"""
216238
return self.set_pixels(((x, y),), self._pen_colour if color is None else color)
217239

240+
def clear_pixel(self, x: int, y: int) -> Self:
241+
"""Clear the colour of a specific pixel on the canvas.
242+
243+
Args:
244+
x: The horizontal location of the pixel.
245+
y: The vertical location of the pixel.
246+
247+
Raises:
248+
CanvasError: If the pixel location is not within the canvas.
249+
250+
Note:
251+
The origin of the canvas is the top left corner.
252+
"""
253+
return self.clear_pixels(((x, y),))
254+
218255
def get_pixel(self, x: int, y: int) -> Color:
219256
"""Get the pixel at the given location.
220257

0 commit comments

Comments
 (0)