Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/TurtleArt
diff options
context:
space:
mode:
authorWalter Bender <walter.bender@gmail.com>2011-11-12 16:21:54 (GMT)
committer Walter Bender <walter.bender@gmail.com>2011-11-12 16:21:54 (GMT)
commitf0f663ea89eff871214ccc4a8348e572603d2a16 (patch)
treee45c3b22ac937b95edffb896997cb20307bd7ae0 /TurtleArt
parent30028d772bdcc33bd8ee444e18b769ecca7f880a (diff)
more robust get_pixel code as per cscott
Diffstat (limited to 'TurtleArt')
-rw-r--r--TurtleArt/sprites.py13
-rw-r--r--TurtleArt/tacanvas.py20
2 files changed, 23 insertions, 10 deletions
diff --git a/TurtleArt/sprites.py b/TurtleArt/sprites.py
index ace99c9..412e3bd 100644
--- a/TurtleArt/sprites.py
+++ b/TurtleArt/sprites.py
@@ -440,6 +440,18 @@ class Sprite:
y < 0 or y > (self.rect.height - 1):
return(-1, -1, -1, -1)
+ # create a new 1x1 cairo surface
+ cs = cairo.ImageSurface(cairo.FORMAT_RGB24, 1, 1);
+ cr = cairo.Context(cs)
+ cr.set_source_surface(self.cached_surfaces[i], -x, -y)
+ cr.rectangle(0,0,1,1)
+ cr.set_operator(cairo.OPERATOR_SOURCE)
+ cr.fill()
+ cs.flush() # ensure all writing is done
+ # Read the pixel
+ pixels = cs.get_data()
+ return (ord(pixels[0]), ord(pixels[1]), ord(pixels[2]), 0)
+ '''
# Map the cairo surface onto a pixmap
pixmap = gtk.gdk.Pixmap(None, self.rect.width, self.rect.height, 24)
cr = pixmap.cairo_create()
@@ -450,3 +462,4 @@ class Sprite:
return(int((pixel & 0xFF0000) >> 16),
int((pixel & 0x00FF00) >> 8),
int((pixel & 0x0000FF) >> 0), 0)
+ '''
diff --git a/TurtleArt/tacanvas.py b/TurtleArt/tacanvas.py
index f846d70..49f0e8a 100644
--- a/TurtleArt/tacanvas.py
+++ b/TurtleArt/tacanvas.py
@@ -701,7 +701,6 @@ class TurtleGraphics:
def get_pixel(self):
''' Read the pixel at x, y '''
- # Fix me: Is there a more efficient way of doing this?
if self.tw.interactive_mode:
x, y = self.turtle_to_screen_coordinates(self.xcor, self.ycor)
x = int(x)
@@ -710,16 +709,17 @@ class TurtleGraphics:
h = self.tw.turtle_canvas.get_height()
if x < 0 or x > (w - 1) or y < 0 or y > (h - 1):
return(-1, -1, -1, -1)
- # Map the cairo surface onto a pixmap
- pixmap = gtk.gdk.Pixmap(None, w, h, 24)
- cr = pixmap.cairo_create()
- cr.set_source_surface(self.tw.turtle_canvas, 0, 0)
- cr.paint()
+ # create a new 1x1 cairo surface
+ cs = cairo.ImageSurface(cairo.FORMAT_RGB24, 1, 1);
+ cr = cairo.Context(cs)
+ cr.set_source_surface(self.tw.turtle_canvas, -x, -y)
+ cr.rectangle(0,0,1,1)
+ cr.set_operator(cairo.OPERATOR_SOURCE)
+ cr.fill()
+ cs.flush() # ensure all writing is done
# Read the pixel
- pixel = pixmap.get_image(x, y, 1, 1).get_pixel(0, 0)
- return(int((pixel & 0xFF0000) >> 16),
- int((pixel & 0x00FF00) >> 8),
- int((pixel & 0x0000FF) >> 0), 0)
+ pixels = cs.get_data()
+ return (ord(pixels[0]), ord(pixels[1]), ord(pixels[2]), 0)
else:
return(-1, -1, -1, -1)