summaryrefslogtreecommitdiffstats
path: root/libdimension-python
diff options
context:
space:
mode:
Diffstat (limited to 'libdimension-python')
-rw-r--r--libdimension-python/dimension.pxd4
-rw-r--r--libdimension-python/dimension.pyx32
2 files changed, 36 insertions, 0 deletions
diff --git a/libdimension-python/dimension.pxd b/libdimension-python/dimension.pxd
index 411be3f..e1bcc1e 100644
--- a/libdimension-python/dimension.pxd
+++ b/libdimension-python/dimension.pxd
@@ -184,6 +184,10 @@ cdef extern from "../libdimension/dimension.h":
dmnsn_canvas *dmnsn_new_canvas(size_t width, size_t height)
void dmnsn_delete_canvas(dmnsn_canvas *canvas)
+ dmnsn_color dmnsn_get_pixel(dmnsn_canvas *canvas, size_t x, size_t y)
+ void dmnsn_set_pixel(dmnsn_canvas *canvas, size_t x, size_t y,
+ dmnsn_color color)
+
void dmnsn_clear_canvas(dmnsn_canvas *canvas, dmnsn_color color)
int dmnsn_png_optimize_canvas(dmnsn_canvas *canvas)
diff --git a/libdimension-python/dimension.pyx b/libdimension-python/dimension.pyx
index baafcd6..a8f9cd8 100644
--- a/libdimension-python/dimension.pyx
+++ b/libdimension-python/dimension.pyx
@@ -484,6 +484,7 @@ cdef class Canvas:
height -- the height of the canvas
"""
self._canvas = dmnsn_new_canvas(width, height)
+ self.clear(Black)
def __dealloc__(self):
dmnsn_delete_canvas(self._canvas)
@@ -497,6 +498,15 @@ cdef class Canvas:
def __get__(self):
return self._canvas.height
+ def __len__(self):
+ """The width of the canvas."""
+ return self.width
+ def __getitem__(self, int x):
+ """Get a column of the canvas."""
+ if x < 0 or x >= self.width:
+ raise IndexError("x coordinate out of bounds.")
+ return _CanvasProxy(self, x)
+
def optimize_PNG(self):
"""Optimize a canvas for PNG output."""
if dmnsn_png_optimize_canvas(self._canvas) != 0:
@@ -529,6 +539,28 @@ cdef class Canvas:
if dmnsn_gl_write_canvas(self._canvas) != 0:
raise OSError(errno, os.strerror(errno))
+cdef class _CanvasProxy:
+ cdef dmnsn_canvas *_canvas
+ cdef int _x
+
+ def __init__(self, Canvas canvas not None, int x):
+ self._canvas = canvas._canvas
+ self._x = x
+
+ def __len__(self):
+ """The height of the canvas."""
+ return self._canvas.height
+ def __getitem__(self, int y):
+ self._bounds_check(y)
+ return _Color(dmnsn_get_pixel(self._canvas, self._x, y))
+ def __setitem__(self, int y, color):
+ self._bounds_check(y)
+ dmnsn_set_pixel(self._canvas, self._x, y, Color(color)._c)
+
+ def _bounds_check(self, int y):
+ if y < 0 or y >= self._canvas.height:
+ raise IndexError("y coordinate out of bounds.")
+
############
# Patterns #
############