From f0f663ea89eff871214ccc4a8348e572603d2a16 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Sat, 12 Nov 2011 16:21:54 +0000 Subject: more robust get_pixel code as per cscott --- (limited to 'TurtleArt') 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) -- cgit v0.9.1