Change polygon line color on mouse click

PsychoPy version: 2021.1.4

I have 4 polygons and I want their border to change with a mouse click.
This works fine so far. Now I want them to change back on a second click.

What did you try to make it work?:

based on a discussion in this forum I tried the following:
(I also defined the necessary variables in the code component)

for stim in [Prac_Resp_1, Prac_Resp_2, Prac_Resp_3, Prac_Resp_4 ]:
   if mouse.isPressedIn(stim):
       thisClickTime = clickClock.getTime()
       if (thisClickTime - lastClickTime) > bufferTime:
           if stim.lineColor =='grey':
               stim.lineColor = 'black'
           else:
               stim.lineColor = 'grey'
       lastClickTime = thisClickTime

However, the color does not change back.
In a next step I tried the following:

for stim in [Prac_Resp_1, Prac_Resp_2, Prac_Resp_3, Prac_Resp_4 ]:
  if mouse.isPressedIn(stim):
       if stim.lineColor == 'back': (a condition that is true)
            stim.lineColor = 'grey';

This did not work either. So my guess is, that there must be a problem with the evaluation of the condition, as it is not evaluated correctly.

Does anyone have an idea how to get this to work?

Thanks in advance!

Best,

Kathrin

Hi Kathrin,

This looks good to me! Just to rule this out first, should stim.lineColor == 'back': read stim.lineColor == 'black':

Thanks,
Becca

Hi Becca,

that was actually just a typo here in the post. It doesn’t work unfortunately. :frowning:

Best,

Kathrin

The issue (at least when I just tried it) is that the line colour is stored as a list even though you set it as a string.

You could compare the colour as an array (e.g. if all(stim.lineColor) == [-1,-1,-1] but I might have got the syntax wrong there. I decided to store the colour separately.

Here’s the demo I just put together.

polygon-clicks.psyexp (8.0 KB)

The way colours work is that, when you set it, behind the scenes a Color object gets made, which has a value for the colour in various different colour spaces. When you then ask for the colour, it gives you it in the colour space of the Polygon. So to compare it against "black", you’d need to either:

  • set the colour space of the Polygon to be "named"
  • compare it against [-1, -1, -1] (aka black in RGB) instead
  • compare the Color object (stim._borderColor) instead of the colour value, as the object will be able to tell if it’s the same colour regardless of colour space

Thanks a lot! :slight_smile: It works now.