From 1de5081e846ff4beb88bd610be4f6d5181fcad3e Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Fri, 04 Nov 2011 16:54:49 +0000 Subject: not sure how an old version of get_pixel got here --- diff --git a/TurtleArt/sprites.py b/TurtleArt/sprites.py index 1e398a0..6f5695a 100644 --- a/TurtleArt/sprites.py +++ b/TurtleArt/sprites.py @@ -430,22 +430,51 @@ class Sprite: ''' Return the upper-left corner of the label safe zone ''' return(self._margins[0], self._margins[1]) - def get_pixel(self, pos, i=0): - ''' Return the pixl at (x, y) ''' + def get_pixel(self, pos, i=0, mode='888'): + ''' Return the pixel at (x, y) ''' x, y = pos x = x - self.rect.x y = y - self.rect.y - if y > self.images[i].get_height() - 1: - return(-1, -1, -1, -1) - try: + if isinstance(self.images[i], gtk.gdk.Pixbuf): + if y > self.images[i].get_height() - 1: + return(-1, -1, -1, -1) array = self.images[i].get_pixels() if array is not None: - offset = (y * self.images[i].get_width() + x) * 4 - r, g, b, a = ord(array[offset]), ord(array[offset + 1]),\ - ord(array[offset + 2]), ord(array[offset + 3]) - return(r, g, b, a) - else: + try: + if self.images[i].get_has_alpha(): + offset = (y * self.images[i].get_width() + x) * 4 + a = ord(array[offset + 3]) + else: + offset = (y * self.images[i].get_width() + x) * 3 + a = 255 + r = ord(array[offset]) + g = ord(array[offset + 1]) + b = ord(array[offset + 2]) + return(r, g, b, a) + except IndexError: + """ + print "Index Error: %d %d (%d, %d) (w: %d, h: %d) (%dx%d)"\ + % (len(array), offset, x, y, + self.images[i].get_width(), + self.images[i].get_height(), + self.rect.width, self.rect.height) + """ + pass return(-1, -1, -1, -1) - except IndexError: - print "Index Error: %d %d" % (len(array), offset) - return(-1, -1, -1, -1) + else: + w, h = self.images[i].get_size() + if x < 0 or x > (w - 1) or y < 0 or y > (h - 1): + return(-1, -1, -1, -1) + image = self.images[i].get_image(x, y, 1, 1) + pixel = image.get_pixel(0, 0) + visual = self.images[i].get_visual() + r = int((pixel & visual.red_mask) >> visual.red_shift) + g = int((pixel & visual.green_mask) >> visual.green_shift) + b = int((pixel & visual.blue_mask) >> visual.blue_shift) + # Rescale to 8 bits + if mode == '565': + r = r << 3 + g = g << 2 + b = b << 3 + return(r, g, b, 0) + -- cgit v0.9.1