Creating a grating with a only one color

Hi,

I want to present only blue shades on my screen.
I want to create a grating that goes from blue to black.
How can I do that?

Thanks,
Boaz

It is probably easiest to generate your own texture (see here for this and other options), such as:

import numpy as np

import psychopy.visual
import psychopy.filters
import psychopy.event

win = psychopy.visual.Window(
    size=[400, 400],
    fullscr=False,
    units="pix"
)

grating_res = 256

grating = psychopy.filters.makeGrating(res=grating_res, cycles=5.0)

# initialise a 'black' texture
blue_grating = np.ones((grating_res, grating_res, 3)) * -1.0
# replace the blue channel with the grating
blue_grating[..., -1] = grating

stim = psychopy.visual.GratingStim(
    win=win,
    tex=blue_grating,
    mask="circle",
    size=(grating_res, grating_res)
)

stim.draw()

win.flip()

psychopy.event.waitKeys()

win.close()
1 Like

Hi!

I am trying to implement this solution to create a black grating on the grey surface. I would still like to use the GratingStim function to be able to manipulate the spatial frequency, but I am basically trying to draw black stripes masked by a circle. I am struggling to understand which parts of the demo to change to have a black-grey combination instead of black-blue. Thanks!

You could try removing the references to blue_grating in the example and instead modifying the grating via:

grating = np.clip(a=grating, a_min=-1, a_max=0)

This will make all the values above zero (i.e., above mid-grey) to be zero.

If you want the black-grey variation to be square rather than sinusoidal, you could instead do:

grating = np.sign(np.clip(a=grating, a_min=-1, a_max=0))

The sign function will convert all values below zero to -1 (i.e., black).

1 Like

I just came across this thread and thought I’d provide a bit of background, for others that come across the topic.

The use of color pairs in PsychoPy is by design. Vision scientists nearly always want a balanced display - a stimulus where the mean luminance (and ideally the mean chromaticity) of the screen is constant for all conditions. So we use a grey screen and then a grating that averages to the same grey as the background screen. So, if you set your grating colour to white you get black in the other bars, if you set it to red, you get green etc. and the average is always mid-grey. The mean luminance of these stimuli (on a gamma-calibrated display) doesn’t change if you alter size or contrast or colour! This is also the reason we use a colour space from -1 to +1 (rather than 0-1 or 0-255) because we want values to represent the colors relative to that mean grey.

If you do need a single-colour grating you can create the raw texture as above and use that in your grating stimulus (also, if you set the grating to 1 cycle, rather than 5 in the example above, then you can control spatial frequency using the standard PsychoPy stimulus controls). Just be aware that the visual composition of that stimulus will alter luminance and pupil size and everythgin that goes along with it, which might not matter to you.

1 Like