psychopy.org | Reference | Downloads | Github

Present Colored Grating w/ Non-Specified Pixels in White NOT Gray

I am presenting a Grating stimulus in a randomly selected color. Additionally, to mask the color of the stimulus, I am specifying a tex array that defines the proportion of color-specific pixels (e.g., red) and those that are not specified. Because of how the texture function operates, the non-specified pixels are typically presented in gray (i.e., given color value multiplied by zero = ‘gray’).

I would like to present these unspecified pixels in ‘white’ instead. Is there a way to do this?

Here is what I’m currently working with:

Screenshot 2021-07-19 165725

import numpy as np
from numpy import random 
from psychopy import visual, event

win = visual.Window(size=(1200, 1080), #fullscr=True 
        allowGUI=True, 
        color="white",
        units='pix',
        colorSpace='rgb'
        ) 

noiseTexture = np.zeros(300*300)
noiseTexture[:round(noiseTexture.size*0.4)] = 1 #0.4:0.2:0.8
np.random.shuffle(noiseTexture)
noiseTexture = noiseTexture.reshape(300,300)

noiseCircle = visual.GratingStim(win=win, 
                                  tex=noiseTexture, 
                                  size=(300,300), 
                                  units='pix',
                                  interpolate=False, 
                                  mask='raisedCos',
                                  maskParams = {'fringeWidth':1},
                                  color = (255,0,0),
                                  colorSpace='rgb255'
                                  )

noiseCircle.draw()
win.flip() 

I’d really recommend you try to get your head around PsychoPy’s default color space, with colors going -1 to +1. When you’ve got there it’s a lot easier to work with than 0-255.

Also note that the color of a grating stimulus is designed to multiply by the texture. That means that in a typical texture with a balance of “black/white” when you multiply the color you get a new neutral color across the stimulus (it should remain the mean grey, for instance if gamma corrected etc). PsychoPy doesn’t have the built-in tools to do the thing you’re looking for because in visual science it’s unusual to want to change the mean luminance/chromaticity at the same time as changing the local texture colors.

That said you could achieve what you’re asking for in 2 ways:

  1. If you want specific colors for difference parts of a texture rather than specifying one color and getting the complement, then you have to specify the rgb values of each pixel in the texture itself and leave the overall stimulus color to be (1,1,1).

  2. In the particular case you’ve got here, where the “non-specified” pixels are going to be the color of the background then you could, instead, create your texture, apply the raised cosine edge and use it as a mask instead of the raised cosine. That way you’re essentially setting the transparency of each pixel and you can apply an arbitrary color to the non-transparent pixels. This would have some slight computational advantages (e.g. not needed an RGB array, just an intensity array) but might not cover all your needs if, for instance, you decide you want red and blue pixels