def image(self, fname, center=True, x=None, y=None, scale=None): """see openexp._canvas.legacy""" try: surface = pygame.image.load(fname) except pygame.error as e: raise openexp.exceptions.canvas_error( "'%s' is not a supported image format" % fname) if scale != None: surface = pygame.transform.smoothscale( surface, (int(surface.get_width() * scale), int(surface.get_height() * scale))) size = surface.get_size() if x == None: x = self.xcenter() if y == None: y = self.ycenter() if center: x -= size[0] / 2 y -= size[1] / 2 self.showables.append((libopengl.LowImage(surface), (x, y)))
def gabor(self, x, y, orient, freq, env="gaussian", size=96, stdev=12, phase=0, col1="white", col2="black", bgmode="avg"): """ Draws a Gabor patch. This function is derived from the online Gabor patch generator <http://www.cogsci.nl/software/online-gabor-patch-generator> orient: orientation in degrees [0 .. 360] freq: frequency in cycles/px env: envelope (gaussian/ linear/ circular/ rectangle) size: size in px stdev: standard deviation of the gaussian (only applicable if env == gaussian) phase: phase [0 .. 1] col1: color of tops col2: color of troughs bgmode: color of the background (avg/ col2) """ surface = openexp._canvas.legacy._gabor(orient, freq, env, size, stdev, phase, col1, col2, bgmode) self.showables.append( (libopengl.LowImage(surface), (x - 0.5 * size, y - 0.5 * size)))
def ellipse(self, x, y, w, h, fill=False, color=None): """see openexp._canvas.legacy""" if color == None: color = self.fgcolor x = int(x) y = int(y) w = int(w) h = int(h) if fill: surface = pygame.Surface((w, h), SRCALPHA) pygame.draw.ellipse(surface, color, (0, 0, w, h), 0) loc = (x, y) else: # Because the default way of drawing thick lines gives ugly results # for ellipses, we draw thick circles manually i = self.penwidth / 2. j = self.penwidth - i fgrect = (self.penwidth - i, self.penwidth - i, self.penwidth + w + 2 * i, self.penwidth + h + 2 * i) bgrect = (self.penwidth + j, self.penwidth + j, self.penwidth + w - 2 * j, self.penwidth + h - 2 * j) loc = (x - self.penwidth - i, y - self.penwidth - j) surface = pygame.Surface( (w + 2 * self.penwidth + 2, h + 2 * self.penwidth + 2), SRCALPHA) pygame.draw.ellipse(surface, color, fgrect, 0) pygame.draw.ellipse(surface, self.bgcolor, bgrect, 0) self.showables.append((libopengl.LowImage(surface), loc))
def line(self, sx, sy, ex, ey, color=None): """see openexp._canvas.legacy""" if color == None: color = self.fgcolor dy = abs(ey - sy) + 2 * self.penwidth + 1 dx = abs(ex - sx) + 2 * self.penwidth + 1 surface = pygame.Surface((dx, dy), SRCALPHA) if sx < ex: s_sx = self.penwidth s_ex = dx - self.penwidth - 1 else: s_sx = dx - self.penwidth - 1 s_ex = self.penwidth if sy < ey: s_sy = self.penwidth s_ey = dy - self.penwidth - 1 else: s_sy = dy - self.penwidth - 1 s_ey = self.penwidth pygame.draw.line(surface, color, (s_sx, s_sy), (s_ex, s_ey), self.penwidth) self.showables.append( (libopengl.LowImage(surface, interpolate=False), (min(sx, ex) - self.penwidth, min(sy, ey) - self.penwidth)))
def noise_patch(self, x, y, env = "gaussian", size = 96, stdev = 12, col1 = "white", col2 = "black", bgmode = "avg"): """ Draws a patch of noise, with an envelope applied over it. """ surface = openexp._canvas.legacy._noise_patch(env, size, stdev, col1, col2, bgmode) self.showables.append((libopengl.LowImage(surface), (x - 0.5 * size, y - 0.5 * size)))
def clear(self, color=None): """see openexp._canvas.legacy""" if color is None: color = self.bgcolor else: color = self.color(color) # clear the showable list self.showables = [] surface = pygame.Surface(self.experiment.resolution) surface.fill(color) self.showables.append((libopengl.LowImage(surface), (0, 0)))
def arrow(self, sx, sy, ex, ey, arrow_size = 5, color = None): """see openexp._canvas.legacy""" if color == None: color = self.fgcolor color = self.color(color) dy = abs(ey - sy) + 2*arrow_size dx = abs(ex - sx) + 2*arrow_size surface = pygame.Surface((dx,dy), SRCALPHA) if sx < ex: s_sx = arrow_size s_ex = dx - arrow_size else: s_sx = dx - arrow_size s_ex = arrow_size if sy < ey: s_sy = arrow_size s_ey = dy - arrow_size else: s_sy = dy - arrow_size s_ey = arrow_size pygame.draw.line(surface, color, (s_sx, s_sy), (s_ex, s_ey), self.penwidth) a = math.atan2(s_ey - s_sy, s_ex - s_sx) _sx = s_ex + arrow_size * math.cos(a + math.radians(135)) _sy = s_ey + arrow_size * math.sin(a + math.radians(135)) pygame.draw.line(surface, color, (_sx, _sy), (s_ex, s_ey), self.penwidth) _sx = s_ex + arrow_size * math.cos(a + math.radians(225)) _sy = s_ey + arrow_size * math.sin(a + math.radians(225)) pygame.draw.line(surface, color, (_sx, _sy), (s_ex, s_ey), self.penwidth) self.showables.append((libopengl.LowImage(surface), (min(sx,ex)+arrow_size, min(sy,ey)+arrow_size)))
def rect(self, x, y, w, h, fill=False, color=None): """see openexp._canvas.legacy""" if color == None: color = self.fgcolor if fill: surface = pygame.Surface((w, h), SRCALPHA) pygame.draw.rect(surface, color, (0, 0, w, h), 0) loc = (x, y) else: surface = pygame.Surface( (w + 2 * self.penwidth, h + 2 * self.penwidth), SRCALPHA) pygame.draw.rect(surface, color, (self.penwidth, self.penwidth, w, h), self.penwidth) loc = (x - self.penwidth, y - self.penwidth) self.showables.append((libopengl.LowImage(surface), loc))
def text(self, text, center=True, x=None, y=None, color=None): """see openexp._canvas.legacy""" if color == None: color = self.fgcolor surface = self.font.render(text, self.antialias, color) size = self.font.size(text) if x == None: x = self.xcenter() if y == None: y = self.ycenter() if center: x -= size[0] / 2 y -= size[1] / 2 self.showables.append((libopengl.LowImage(surface), (x, y)))
def fixdot(self, x=None, y=None, color=None): """see openexp._canvas.legacy""" if color == None: color = self.fgcolor if x == None: x = self.xcenter() if y == None: y = self.ycenter() r1 = 8 r2 = 2 surface = pygame.Surface((r1 * 2, r1 * 2), SRCALPHA) pygame.draw.circle(surface, color, (r1, r1), r1, 0) pygame.draw.circle(surface, self.bgcolor, (r1, r1), r2, 0) self.showables.append((libopengl.LowImage(surface), (x - r1, y - r1)))