Skip to content

Commit 4d6fbb2

Browse files
committed
added gamma correction
1 parent dd73c16 commit 4d6fbb2

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ setHex KEYWORD2
1515
getColorHex KEYWORD2
1616
setHSV KEYWORD2
1717
setCMYK KEYWORD2
18+
setGammaCorrection KEYWORD2
1819
off KEYWORD2
1920
setWhite KEYWORD2
2021
setPink KEYWORD2

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=LCD_BacklightRGB
2-
version=1.5.7
2+
version=1.6.0
33
author=Felix Ochoa
44
maintainer=Felix Ochoa
55
sentence=An Arduino library for an 18-pin RGB LCD Display

src/BacklightRGB.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ BacklightRGB::BacklightRGB(uint8_t redPin, uint8_t greenPin, uint8_t bluePin) {
1919
_redPin = redPin; _greenPin = greenPin; _bluePin = bluePin;
2020
_COMMON_ANODE = true;
2121
_brightness = 255;
22+
_gammaEnabled = false;
2223
_currentColor[0] = _currentColor[1] = _currentColor[2] = 0;
2324
}
2425

2526
BacklightRGB::BacklightRGB(uint8_t redPin, uint8_t greenPin, uint8_t bluePin, bool COMMON_ANODE) {
2627
_redPin = redPin; _greenPin = greenPin; _bluePin = bluePin;
2728
_COMMON_ANODE = COMMON_ANODE;
2829
_brightness = 255;
30+
_gammaEnabled = false;
2931
_currentColor[0] = _currentColor[1] = _currentColor[2] = 0;
3032
}
3133

@@ -104,6 +106,9 @@ void BacklightRGB::setHex(uint32_t hexColor, uint8_t brightness) {
104106
uint8_t BacklightRGB::setColor(uint8_t color) {
105107
color = constrain(color, 0, 255);
106108
color = color * _brightness / 255;
109+
if (_gammaEnabled) {
110+
color = _gammaTable[color];
111+
}
107112
if (_COMMON_ANODE) {
108113
color = 255 - color;
109114
}
@@ -176,3 +181,26 @@ void BacklightRGB::setCMYK(float cyan, float magenta, float yellow, float key) {
176181
int blue = roundf((1 - yellow) * (1 - black) * 255);
177182
setRGB(red, green, blue);
178183
}
184+
185+
void BacklightRGB::setGammaCorrection(bool enabled) {
186+
_gammaEnabled = enabled;
187+
}
188+
189+
const uint8_t BacklightRGB::_gammaTable[256] = {
190+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
191+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
192+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
193+
2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5,
194+
5, 6, 6, 6, 7, 7, 7, 8, 8, 9, 9, 10, 10, 10, 11, 11,
195+
12, 12, 13, 13, 14, 15, 15, 16, 16, 17, 18, 19, 19, 20, 21, 22,
196+
23, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
197+
38, 40, 41, 42, 43, 44, 46, 47, 48, 50, 51, 52, 54, 55, 56, 58,
198+
59, 61, 62, 64, 65, 67, 68, 70, 71, 73, 74, 76, 78, 79, 81, 83,
199+
84, 86, 88, 90, 91, 93, 95, 97, 99,100,102,104,106,108,110,112,
200+
114,116,118,120,122,124,126,128,130,132,134,136,138,141,143,145,
201+
147,149,152,154,156,159,161,163,166,168,170,173,175,178,180,183,
202+
185,188,190,193,195,198,201,203,206,209,211,214,217,220,222,225,
203+
228,231,234,237,239,242,245,248,251,254,255,255,255,255,255,255,
204+
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
205+
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
206+
};

src/BacklightRGB.h

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919
*/
2020
class BacklightRGB {
2121
private:
22-
bool _COMMON_ANODE; ///< True if common anode RGB LED
22+
bool _COMMON_ANODE; ///< True if common anode RGB LED
2323
uint8_t _redPin; ///< Pin connected to the red LED.
2424
uint8_t _greenPin; ///< Pin connected to the green LED.
2525
uint8_t _bluePin; ///< Pin connected to the blue LED.
26-
uint8_t _brightness; ///< Brightness value (0 to 255).
26+
uint8_t _brightness; ///< Brightness value (0 to 255).
2727
uint8_t _currentColor[3]; ///< Array storing the current RGB values.
28+
bool _gammaEnabled; ///< Set true to enable gamma correction.
2829

2930
/**
3031
* @brief Adjusts the intensity of a color component based on brightness.
@@ -41,6 +42,20 @@ class BacklightRGB {
4142
*/
4243
inline void showRGB(uint8_t red, uint8_t green, uint8_t blue);
4344

45+
/**
46+
* @brief Precomputed gamma correction lookup table.
47+
*
48+
* This table maps 8-bit linear brightness values (0–255) to perceptual values
49+
* corrected for human vision using a gamma of approximately 2.2.
50+
*
51+
* When gamma correction is enabled via setGammaCorrection(true), this table is
52+
* used to adjust each color channel before output.
53+
*
54+
* Index: input brightness (0–255)
55+
* Value: corrected brightness (0–255)
56+
*/
57+
static const uint8_t _gammaTable[256];
58+
4459
public:
4560
/**
4661
* @brief Constructor to initialize RGB pins.
@@ -139,7 +154,6 @@ class BacklightRGB {
139154
//const int* getRGB() const;
140155
const uint8_t* getRGB() const;
141156

142-
143157
/**
144158
* @brief Gets the current color as a 24-bit hexadecimal value.
145159
* @return 24-bit integer representing the color (0xRRGGBB).
@@ -163,6 +177,12 @@ class BacklightRGB {
163177
*/
164178
void setCMYK(float cyan, float magenta, float yellow, float key);
165179

180+
/**
181+
* @brief Set whether to apply gamma correction to RGB output.
182+
*
183+
* @param enabled True to apply gamma correction using a lookup table.
184+
*/
185+
void setGammaCorrection(bool enabled);
166186
};
167187

168188
#endif

0 commit comments

Comments
 (0)