def level_indicator(value, text, scaling, width=20, text_size=14, text_gap=20, position=(0, 0), thresholds=None, colour=constants.C_EXPYRIMENT_ORANGE): """make an level indicator in for of an Expyriment stimulus text_gap: gap between indicator and text scaling: Scaling object Returns -------- expyriment.Canvas """ value = scaling.trim(value) # indicator height = scaling.pixel_max - scaling.pixel_min indicator = Canvas(size=[width + 2, height + 2], colour=(30, 30, 30)) zero = scaling.data2pixel(0) px_bar_height = scaling.data2pixel(value) - zero bar = Rectangle(size=(width, abs(px_bar_height)), position=(0, zero + int((px_bar_height + 1) / 2)), colour=colour) bar.plot(indicator) # levels & horizontal lines try: px_horizontal_lines = scaling.data2pixel( values=np.array(thresholds.thresholds)) except: px_horizontal_lines = None if px_horizontal_lines is not None: for px in px_horizontal_lines: level = Rectangle(size=(width + 6, 2), position=(0, px), colour=constants.C_WHITE) level.plot(indicator) # text labels txt = TextLine(text=text, text_size=text_size, position=(0, -1 * (int(height / 2.0) + text_gap)), text_colour=constants.C_YELLOW) # make return canvas w = max(txt.surface_size[0], indicator.size[0]) h = height + 2 * (txt.surface_size[1]) + text_gap rtn = Canvas(size=(w, h), colour=(0, 0, 0), position=position) indicator.plot(rtn) txt.plot(rtn) return rtn
def Kanisza_square(width, height, radius, backgroung_color=GRAY, square_color=GRAY, color_tl=BLACK, color_tr=BLACK, color_bl=BLACK, color_br=BLACK): """ Creates a surface with Kanisza illusory square (see https://openi.nlm.nih.gov/detailedresult?img=PMC4211395_fnhum-08-00854-g0001&req=4 ) """ surface = BlankScreen(backgroung_color) # coordinates of the corners of the rectangle (also that of the circles' centers) left = - width // 2 right = width // 2 top = height // 2 bottom = - height // 2 c_tl = Circle(radius, color_tl, position = (left, top), anti_aliasing = 10) c_tr = Circle(radius, color_tr, position = (right, top), anti_aliasing = 10) c_bl = Circle(radius, color_bl, position = (left, bottom), anti_aliasing = 10) c_br = Circle(radius, color_br, position = (right, bottom), anti_aliasing = 10) rect = Rectangle((width, height), square_color, position = (0, 0), corner_anti_aliasing = True) c_tl.plot(surface) c_tr.plot(surface) c_bl.plot(surface) c_br.plot(surface) rect.plot(surface) return surface
def prepare(self): self._stim = Rectangle(position=self.to_xy(self.x + self.w // 2, self.y + self.h // 2), size=(self.w, self.h), line_width=0 if self.fill else self.penwidth, colour=self.color.backend_color) self._stim.preload()
def _create_surface(self): """Create the surface of the stimulus.""" surface = pygame.surface.Surface( (self._size[0] + self._frame_line_width, self._size[1] + self._frame_line_width), pygame.SRCALPHA).convert_alpha() if self._gap_colour is not None: surface.fill(self._gap_colour) parts = [] width = self._size[0] - self._frame_line_width - \ 2 * self._frame_line_width % 2 height = self._size[1] - self._frame_line_width - \ 2 * self._frame_line_width % 2 + 1 s_height = int(height - (self._nr_segments + 1) * self._gap) // self._nr_segments for x in range(self._nr_segments): if x < self._state / 100.0 * self._nr_segments: colour = self._active_colour else: colour = self._inactive_colour s = Rectangle( (width - self._gap * 2, s_height), colour=colour, position=(0, -height // 2 + s_height // 2 + x * height // self.nr_segments + self._gap)) parts.append(s) parts.append( Rectangle(self._size, colour=self._frame_colour, line_width=self._frame_line_width, position=self._position)) parts.append( Rectangle((width - self._gap, height - self._gap * 2), colour=self._gap_colour, line_width=self._gap, position=self._position)) if self._goal is not None: x = int(round(self._goal / 100.0 * self._nr_segments)) - 1 current_y_pos = -height // 2 + s_height // 2 + \ x * height // self._nr_segments + self._gap above_y_pos = -height // 2 + s_height // 2 + \ (x + 1) * height // self._nr_segments + self._gap g1 = Rectangle( (self._frame_line_width * 1.25, self._frame_line_width * 1.25), colour=self._goal_colour, position=(-self._size[0] // 2 - self._frame_line_width // 2, (current_y_pos + above_y_pos) // 2)) g2 = Rectangle( (self._frame_line_width * 1.25, self._frame_line_width * 1.25), colour=self._goal_colour, position=(self._size[0] // 2 + self._frame_line_width // 2, (current_y_pos + above_y_pos) // 2)) g1.rotate(45) g2.rotate(45) parts.append(g1) parts.append(g2) for stim in parts: stim.rect = pygame.Rect((0, 0), stim.surface_size) surface_size = surface.get_size() stim.rect.center = [ stim.position[0] + surface_size[0] // 2, -stim.position[1] + surface_size[1] // 2 ] surface.blit(stim._get_surface(), stim.rect) return surface