# Defining specific colours for RadialStim and GratingStim

**URL:** <https://discourse.psychopy.org/t/defining-specific-colours-for-radialstim-and-gratingstim/6724>\
**Category:** Coding\
**Created:** [February 18, 2019, 6:32am UTC](https://discourse.psychopy.org/t/defining-specific-colours-for-radialstim-and-gratingstim/6724 "2019-02-18T06:32:39Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![angjw.aaron](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/angjw.aaron/32/2574_2.png) [@angjw.aaron](https://discourse.psychopy.org/u/angjw.aaron)\
**Post date:** [February 18, 2019, 6:32am UTC](https://discourse.psychopy.org/t/defining-specific-colours-for-radialstim-and-gratingstim/6724/1 "2019-02-18T06:32:39Z")

</div>

Hi there,

I would actually like to define the colour of the abovementioned visual. functions. But currently, the ‘color’ setting for these functions can only be applied to one of the rings/gratings and the color of the other, the direct opposite. Is there a way that I can specifically control the colour of the second ring/grating?

(ie. If I were to set the color as red, the default opponent color is blue. I would like to make it say, red and green instead.)

Edit: I’ve found the solution for a non-radial stimulus at [Creating a grating with a only one color](https://discourse.psychopy.org/t/creating-a-grating-with-a-only-one-color/2660/2). However, the psychopy.filters.makeGrating function hasn’t got a ‘tex’ setting where I could change it away from the default sin texture. Secondly, I’m still not quite sure how to do the same to a circular radial stimulus?

---

<div class="post-metadata">

**Author:** ![djmannion](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/djmannion/32/37535_2.png) [@djmannion](https://discourse.psychopy.org/u/djmannion)\
**Post date:** [February 18, 2019, 7:19am UTC](https://discourse.psychopy.org/t/defining-specific-colours-for-radialstim-and-gratingstim/6724/2 "2019-02-18T07:19:29Z")

</div>

> [@angjw.aaron](#):
>
> However, the psychopy.filters.makeGrating function hasn’t got a ‘tex’ setting where I could change it away from the default sin texture.

The `gratType` parameter has a few options to generate non-sin textures.

> [@](#):
>
> Secondly, I’m still not quite sure how to do the same to a circular radial stimulus?

You can get an idea of how it works with a radial stimulus by replacing `GratingStim` with `RadialStim` in the linked demo code.

![blue_radial_grating](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/6/a/6a02b6c35d4c566b9edcb0e993c6acb04e2351af.png)

For example, try using `ori=90` in `makeGrating`.

---

<div class="post-metadata">

**Author:** ![angjw.aaron](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/angjw.aaron/32/2574_2.png) [@angjw.aaron](https://discourse.psychopy.org/u/angjw.aaron)\
**Post date:** [February 18, 2019, 8:03am UTC](https://discourse.psychopy.org/t/defining-specific-colours-for-radialstim-and-gratingstim/6724/3 "2019-02-18T08:03:46Z")

</div>

Thanks @djmannion . That worked for a squarish grating, which is one thing that I needed. However for the radial stimulus, is filter.makeGrating able to come out with something like this instead?

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/f/1/f1550e40ddce0eb070ce9de9aac8d8548ce2b145.png)

---

<div class="post-metadata">

**Author:** ![djmannion](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/djmannion/32/37535_2.png) [@djmannion](https://discourse.psychopy.org/u/djmannion)\
**Post date:** [February 18, 2019, 8:11am UTC](https://discourse.psychopy.org/t/defining-specific-colours-for-radialstim-and-gratingstim/6724/4 "2019-02-18T08:11:41Z")

</div>

Yes, just set `ori=90` when making the texture that is drawn by a `RadialStim`.

Or you can get that just with a `RadialStim` by setting `angularCycles=0` and `radialCycles=8` (for example) with `tex="sqrXsqr"`.

---

<div class="post-metadata">

**Author:** ![angjw.aaron](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/angjw.aaron/32/2574_2.png) [@angjw.aaron](https://discourse.psychopy.org/u/angjw.aaron)\
**Post date:** [February 18, 2019, 9:22am UTC](https://discourse.psychopy.org/t/defining-specific-colours-for-radialstim-and-gratingstim/6724/5 "2019-02-18T09:22:09Z")

</div>

Oh yes, thanks, I managed to get it to work now!

One last question, from the linked demo code:

# replace the blue channel with the grating

blue\_grating[…, -1] = grating

I was trying to wrap my head around the massive number of lists within ‘blue\_grating’ here and can’t quite figure out how this actually makes the grating blue. If I want to make this red, green or even maroon, what variable here do I have to manipulate to get it to change to the respective colours?

---

<div class="post-metadata">

**Author:** ![djmannion](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/djmannion/32/37535_2.png) [@djmannion](https://discourse.psychopy.org/u/djmannion)\
**Post date:** [February 18, 2019, 9:50am UTC](https://discourse.psychopy.org/t/defining-specific-colours-for-radialstim-and-gratingstim/6724/6 "2019-02-18T09:50:17Z")

</div>

The `blue_grating` variable is a 256x256x3 array, where the last dimension indexes the red, green, and blue colour channels. The line `blue_grating[…, -1] = grating` is setting the blue channel (the `-1` indexes the last channel) to vary its intensity according to the grating.

To make it be different colours, the RGB space might not be easiest. Instead, you can define your stimulus in HSV space (with the grating in the V channel) and then convert it into RGB. That way, you only need to change the H channel to vary the hue (though other aspects change with hue also).

For example:

```python
import numpy as np

import psychopy.visual
import psychopy.event
import psychopy.colors

win = psychopy.visual.Window()

grating_res = 256

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

hsv_tex = np.ones((grating_res, grating_res, 3))
# set the value channel to the grating
hsv_tex[..., 2] = (grating + 1) / 2.0

stim = psychopy.visual.RadialStim(
    win=win,
    units="pix",
    size=(grating_res, grating_res)
)

# loop over hues
for angle in range(0, 360, 10):

    hsv_tex[..., 0] = angle

    stim.tex = psychopy.colors.hsv2rgb(hsv_tex)

    stim.draw()

    win.flip()

    psychopy.event.waitKeys()

win.close()

```

---

<div class="post-metadata">

**Author:** ![angjw.aaron](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/angjw.aaron/32/2574_2.png) [@angjw.aaron](https://discourse.psychopy.org/u/angjw.aaron)\
**Post date:** [February 18, 2019, 12:59pm UTC](https://discourse.psychopy.org/t/defining-specific-colours-for-radialstim-and-gratingstim/6724/7 "2019-02-18T12:59:23Z")

</div>

I see, thanks for all these. It seems though, that the colour is dependent on having a black background. Does it have to be that way? (Ie. I can’t have a non-black colour pair like yellow and brown?)

---

<div class="post-metadata">

**Author:** ![djmannion](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/djmannion/32/37535_2.png) [@djmannion](https://discourse.psychopy.org/u/djmannion)\
**Post date:** [February 18, 2019, 8:41pm UTC](https://discourse.psychopy.org/t/defining-specific-colours-for-radialstim-and-gratingstim/6724/8 "2019-02-18T20:41:14Z")

</div>

No, it doesn’t have to be that way. You can make the value channel not go all the way to 0 to get yellow-brown. I’d suggest experimenting with the different ways of manipulating `hsv_tex` - you can make it look pretty much however you want.

```python
import numpy as np

import psychopy.visual
import psychopy.event
import psychopy.colors

win = psychopy.visual.Window()

grating_res = 256

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

hsv_tex = np.ones((grating_res, grating_res, 3))
hsv_tex[..., 0] = 50.0
hsv_tex[..., 2] = (grating + 1) / 2.0 * 0.5 + 0.5

stim = psychopy.visual.RadialStim(
    win=win,
    units="pix",
    size=(grating_res, grating_res)
)

stim.tex = psychopy.colors.hsv2rgb(hsv_tex)

stim.draw()

win.flip()

psychopy.event.waitKeys()

win.close()

```

 ![yb](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/f/6/f69bc1afdeaf54eda8b66da5e8072b0c34e8ea74.png)

---

<div class="post-metadata">

**Author:** ![Claudio\_de\_la\_Rosa](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/claudio_de_la_rosa/32/36151_2.png) [@Claudio\_de\_la\_Rosa](https://discourse.psychopy.org/u/Claudio_de_la_Rosa)\
**Post date:** [March 23, 2023, 3:44pm UTC](https://discourse.psychopy.org/t/defining-specific-colours-for-radialstim-and-gratingstim/6724/9 "2023-03-23T15:44:19Z")

</div>

This last solution has to be upgraded for psychopy 3 as follows:

stim.tex = psychopy.tools.colorspacetools.hsv2rgb(hsv\_tex)

Else it wont work.

---

<div class="post-metadata">

**Author:** ![ah5005](https://avatars.discourse-cdn.com/v4/letter/a/8dc957/32.png) [@ah5005](https://discourse.psychopy.org/u/ah5005)\
**Post date:** [March 24, 2023, 3:14am UTC](https://discourse.psychopy.org/t/defining-specific-colours-for-radialstim-and-gratingstim/6724/10 "2023-03-24T03:14:26Z")

</div>

Hello,

I am trying to create a custom grating, using a similar method.

However, I am having trouble creating a grating with two colors, e.g., red-green as the post initially mentions. Right now, my patch is red-black.

Here is a snippet from my code (inspired by the solutions here):

> import numpy as np
> 
> import psychopy.visual  
> import psychopy.filters  
> import psychopy.event  
> import psychopy.colors
> 
> from psychopy import core  
> from psychopy.visual import filters
> 
> grating\_res = 256
> 
> grating1 = psychopy.visual.filters.makeGrating(res=grating\_res, gratType = ‘sqr’,phase=0, cycles=2.0)
> 
> blue\_grating1 = np.ones((grating\_res, grating\_res, 3))\* -1.0 # initialise a ‘black’ texture  
> blue\_grating1[…, 0] = grating1 # replace the red channel with the grating
> 
> stim1 = psychopy.visual.GratingStim(  
> win=win,  
> tex=blue\_grating1,  
> mask=“circle”,  
> phase=0,  
> size=(5, 5)  
> )

Conversely, could I overlay two gabor patches and make one of the colors transparent, but this seems trickier. As soon as I make one color transparent, for example setting the opacity to 0, the entire patch unsurprisingly disappears. (reference: [https://groups.google.com/g/psychopy-users/c/IlhiS-7XMNg](https://groups.google.com/g/psychopy-users/c/IlhiS-7XMNg)).

Please advise, thank you so much!

---

<div class="post-metadata">

**Author:** ![ah5005](https://avatars.discourse-cdn.com/v4/letter/a/8dc957/32.png) [@ah5005](https://discourse.psychopy.org/u/ah5005)\
**Post date:** [March 24, 2023, 6:04pm UTC](https://discourse.psychopy.org/t/defining-specific-colours-for-radialstim-and-gratingstim/6724/11 "2023-03-24T18:04:53Z")

</div>

> [@djmannion](#):
>
> ```auto
> hsv_tex = np.ones((grating_res, grating_res, 3))
> hsv_tex[..., 0] = 50.0
> hsv_tex[..., 2] = (grating + 1) / 2.0 * 0.5 + 0.5
> 
> ```

Hello, how did you determine these values?

I have successfully recreated a grating stimulus with the colors you use, but I don’t understand how to manipulate them to what I need (red-green and blue-yellow).

Ultimately, I hope to keep red (or yellow) a constant luminance and ask the participant to modulate green (or blue) luminance to achieve perceptual isoluminance, but I think I can do this once I know how to manipulate colors using the numpy array.

Thank you so much!

I’m editing this to post my actual code:

> import numpy as np  
> import psychopy.visual  
> import psychopy.filters  
> import psychopy.event  
> import psychopy.colors
> 
> from psychopy import core  
> from psychopy.visual import filters
> 
> pres\_timer = core.Clock() #define a clock  
> grating\_res = 256
> 
> grating1 = psychopy.visual.filters.makeGrating(res=grating\_res, gratType = ‘sqr’,phase=0, cycles=2.0,contr=0.0)
> 
> #initialise a ‘black’ texture  
> blue\_grating1 = np.ones((grating\_res,grating\_res,3))  
> #replace the blue channel with the grating --\>actually make grating yellow-brown here.  
> blue\_grating1[…, 0] = 50.0  
> blue\_grating1[…, 2] = (grating1 + 1) / 2.0 \* 0.5 + 0.5
> 
> stim1 = psychopy.visual.GratingStim(  
> win=win,  
> tex=blue\_grating1,  
> mask=“circle”,  
> phase=0,  
> size=(5, 5)  
> )
> 
> stim1.tex = psychopy.tools.colorspacetools.hsv2rgb(blue\_grating1)
