# New to PTB - Gamma correction with gratings stimuli

**URL:** <https://psychtoolbox.discourse.group/t/new-to-ptb-gamma-correction-with-gratings-stimuli/3239>\
**Category:** Programming Help\
**Created:** [May 28, 2020, 1:02pm UTC](https://psychtoolbox.discourse.group/t/new-to-ptb-gamma-correction-with-gratings-stimuli/3239 "2020-05-28T13:02:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hyma2194](https://avatars.discourse-cdn.com/v4/letter/h/b4bc9f/32.png) [@hyma2194](https://psychtoolbox.discourse.group/u/hyma2194)\
**Post date:** [May 28, 2020, 1:02pm UTC](https://psychtoolbox.discourse.group/t/new-to-ptb-gamma-correction-with-gratings-stimuli/3239/1 "2020-05-28T13:02:03Z")

</div>

Hello team,  
I apologize for the trivial question but I cannot find a definitive answer otherwise. I am trying to understand how to use gamma correction in the context of having constant luminance when I shift from a black and white gratings stimuli to a grey background and back for a visual experiment. The idea is to ensure a linear gradient and constant total luminance when shifting between the gratings and grey background.  
I have coded the paradigm already but I am not sure how to apply gamma correction with the available function options in PTB.

Thanks  
Hyma

---

<div class="post-metadata">

**Author:** ![xiangrui](https://avatars.discourse-cdn.com/v4/letter/x/ec9cab/32.png) [@xiangrui](https://psychtoolbox.discourse.group/u/xiangrui)\
**Post date:** [May 28, 2020, 1:28pm UTC](https://psychtoolbox.discourse.group/t/new-to-ptb-gamma-correction-with-gratings-stimuli/3239/2 "2020-05-28T13:28:54Z")

</div>

You could compute the correction by yourself, but I found the easy way is to use PsychImaing pipeline:

```
PsychImaging('AddTask', 'FinalFormatting', 'DisplayColorCorrection', 'SimpleGamma');
...
PsychColorCorrection('SetEncodingGamma', win, 1/gamma);

```

For details, you may check one of the demos like `BitsPlusCSFDemo.m`

---

<div class="post-metadata">

**Author:** ![hyma2194](https://avatars.discourse-cdn.com/v4/letter/h/b4bc9f/32.png) [@hyma2194](https://psychtoolbox.discourse.group/u/hyma2194)\
**Post date:** [May 28, 2020, 4:58pm UTC](https://psychtoolbox.discourse.group/t/new-to-ptb-gamma-correction-with-gratings-stimuli/3239/3 "2020-05-28T16:58:04Z")

</div>

Thank you so much! Worked well for me!

---

<div class="post-metadata">

**Author:** ![Ian-Max-Andolina](https://yyz2.discourse-cdn.com/free1/user_avatar/psychtoolbox.discourse.group/ian-max-andolina/32/4_2.png) [@Ian-Max-Andolina](https://psychtoolbox.discourse.group/u/Ian-Max-Andolina)\
**Post date:** [May 29, 2020, 12:56am UTC](https://psychtoolbox.discourse.group/t/new-to-ptb-gamma-correction-with-gratings-stimuli/3239/4 "2020-05-29T00:56:44Z")

</div>

Note if you want more precise correction, you need to measure the exact gamma curve with a light meter. You can then fit a curve more precisely to the data, and then test that the result is fully linear with the same light meter. If you have access to a SpectroCal / ColorCal / i1Pro then I have a class to automate this here:

> <https://github.com/iandol/opticka/blob/master/calibration/calibrateLuminance.m>
