# 5 - Gamma Correction

### Video

{% embed url="<https://youtu.be/YpCGXescnBw>" %}

### Gamma Correction

<figure><img src="https://2808519483-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FI2jtNl20mw4WYRE8MWgw%2Fuploads%2FikpTSfMKI9qwqntYaPVy%2Fimage.png?alt=media&#x26;token=64ccb317-0b89-4a62-a176-b3633ee0710a" alt=""><figcaption><p><a href="https://learn.adafruit.com/led-tricks-gamma-correction">https://learn.adafruit.com/led-tricks-gamma-correction</a></p></figcaption></figure>

<figure><img src="https://2808519483-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FI2jtNl20mw4WYRE8MWgw%2Fuploads%2FTYwZ7rIgNsGJtGE9dkte%2Fimage.png?alt=media&#x26;token=d9443641-5f86-4af6-94dc-7548d172bcba" alt=""><figcaption><p><a href="https://learn.adafruit.com/led-tricks-gamma-correction">https://learn.adafruit.com/led-tricks-gamma-correction</a></p></figcaption></figure>

### Code

```python
"""
Pico Slice 1 - RGB Mixer
Tutorial 5 - Gamma Correction
"""

import time
import board
from pwmio import PWMOut
from analogio import AnalogIn

# A funcion to handle gamma correction
def Gamma_Correct (linear_input):
    gamma = 3
    maximum = 65535
    output = int(((linear_input/maximum)**gamma)*maximum)
    return output

# Represent the Pot in code as an AnalogIn object.
r_pot = AnalogIn(board.A2)

# Represent the Red LED as a PWMOut object.
r_led = PWMOut(board.GP2, duty_cycle=0, frequency=5000, variable_frequency=False)


while True:                                     # Loop Forever:
    input_val = r_pot.value                         # Read the potentiometer's current value (16-bit)
    r_led.duty_cycle = Gamma_Correct(input_val)     # Set the red LED's duty-cycle (16-bit) to the gamma corrected pot value
    time.sleep(0.1)                                 # Do nothing for 100 milliseconds

```
