The GammaCorrection library implements functions to perform gamma correction. The relationship between the intensity applied through PWM to an LED and the perceived illumination is non-linear. With the GammaCorrection library, we can correct the values so that the behavior is correct.
User Manual
The GammaCorrection library contains static functions for gamma correction. Therefore, it is not necessary to instantiate an object, simply call the necessary functions.
There are two methods. Correct8 uses a formula with integer arithmetic to approximate the result. The CorrectTable8 function uses a table for correction. Therefore, the Correct8 method occupies less memory in the program, while the CorrectTable8 function is slightly faster and more accurate. By default, it is recommended to use the Correct8 function unless a large number of calculations are required.
For its use, we indicate the desired value of perceived illumination, and the library returns the necessary PWM. For example, to decrease the intensity by 80% we would enter 204 (255 * 0.8) in the correction functions, and GammaCorrection would return the value to enter in the PWM (approx. 137 in this example).
Using GammaCorrection
static uint8_t Correct8(uint8_t value);
static uint8_t CorrectTable8(uint8_t value);
Examples
The GammaCorrection library includes the following examples to illustrate its use.
- GammaCorrection: Example that shows the use of Correct8.
#include "GammaCorrectionLib.h"
void setup()
{
Serial.begin(115200);
pinMode(9, OUTPUT);
auto value = 0;
for (auto index = 0; index <= 255; index++)
{
value = GammaCorrection::Correct8(index);
analogWrite(9, value);
Serial.print(index);
Serial.print(" ");
Serial.println(value);
}
}
void loop()
{
}
- GammaCorrectionTable: Example that shows the use of CorrectTable8.
#include "GammaCorrectionLib.h"
void setup()
{
Serial.begin(115200);
pinMode(9, OUTPUT);
auto value = 0;
for (auto index = 0; index <= 255; index++)
{
value = GammaCorrection::CorrectTable8(index);
analogWrite(9, value);
Serial.print(index);
Serial.print(" ");
Serial.println(value);
}
}
void loop()
{
}
Installation
- Download the latest version from GitHub
- Unzip the file
- Copy into your libraries folder (usually My Documents\Arduino\libraries)
- Restart the Arduino IDE