Setting colour for premade objects from code

I have created a stimuli which we have named “elements”. It is a basic polygon.

I have two lists, one for positions and a second for colours. Below is the working code for creating the element and for setting the position

for Idx in range(maxElements):
    elements.append(visual.ShapeStim(
    win=win, name='elements'+str(Idx),
    size=(0.08, 0.08), vertices=100,
    ori=0.0, pos=(0, 0), anchor='center',
    lineWidth=1.0,colorSpace='rgb',  lineColor='black', fillColor='black',
    opacity=None, depth=-1.0, interpolate=True))

if nDistractors > 1:
    for Idx in range(1,nDistractors):
        elements[Idx].setPos([positions[Idx][0], positions[Idx][1]])
        elements[Idx].setAutoDraw(True)

This works as intended. This issue comes when we try to change the colour. We have a list called colourChoice, with a list of colours as strings. I have tried setting colour using:

elements[0].fillColor([colourChoice[0]])

And have done the same for [Idx] where it’s needed instead, but I get the error that numpy is not callable.

If I set the fill Colour in the element directly, then it doesn’t change with each repeat. It also sets all the circles to one colour, where I would like each circle to be a different colour from the list.

Any help would be great.

Hello,

if you set the fill-colour directly, you also need to set it to set every repeat from constant. The easiest way is to use a variable that refers to a column in an Excel-file.

Best wishes Jens

Hello,

That won’t work here as the items are drawn from code rather than created using component

I have figured out how to make it work. Instead of fillColor I used setColor and that has made it run as expected. So:

elements[0].setColor(colourChoice[0])

Now gives me a changing colourscape with each shape being a different colour.

1 Like