Skip to content

Instantly share code, notes, and snippets.

@EoinTravers
Last active January 31, 2020 11:59
Show Gist options
  • Save EoinTravers/8987821bb11b97575c8244da3e44a3aa to your computer and use it in GitHub Desktop.
Save EoinTravers/8987821bb11b97575c8244da3e44a3aa to your computer and use it in GitHub Desktop.
Treat a list of psychopy visual components as a single object.
from psychopy import visual
from psychopy.tools.colorspacetools import hsv2rgb
import numpy as np
win = visual.Window(
size=[800, 600], fullscr=False, screen=0,
gammaErrorPolicy='ignore',
color=(1, 1, 1), colorSpace='rgb', units='height')
class CompoundStim():
'''Treat a list of psychopy visual components as a single object.
Example:
x1 = visual.Rect(win, width=.3, height=.3, pos=(-.1, -.1), fillColor=(1,-1,-1))
x2 = visual.Rect(win, width=.3, height=.3, pos=(.1, .1), fillColor=(-1,1,-1))
xs = CompoundStim([x1, x2])
xs.draw(win)
win.flip()
'''
def __init__(self, components):
for c in components:
try:
getattr(c, 'draw')
except AttributeError:
raise Exception('Object passed to CompoundStim does not have a `draw` method')
self.components = components
def draw(self, win):
for c in self.components:
c.draw(win)
x1 = visual.Rect(win, width=.3, height=.3, pos=(-.1, -.1), fillColor=(1,-1,-1))
x2 = visual.Rect(win, width=.3, height=.3, pos=(.1, .1), fillColor=(-1,1,-1))
xs = CompoundStim([x1, x2])
xs.draw(win)
win.flip()
input('Press Enter...')
rainbow = CompoundStim([])
w = .09
for y in np.arange(-.5, .501, .1):
for x in np.arange(-.5, .501, .1):
hue = (x + y + 1) * 180
hsv = [hue, 1, 1]
rgb = hsv2rgb(hsv)
x = visual.Rect(win, width=w, height=w, pos=(x, y), fillColor=rgb)
rainbow.components.append(x)
rainbow.draw(win)
win.flip()
input('Press Enter...')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment