Hi I am using a TFT screen and the Adafruit gfx library. I want to use a potentiometer to scroll through all 60,000+ possible colors in real time for background color on the screen. What is the best way to to do so? Im stuck between using the 16 bit 565 hex way (0xFFFF) or somehow converting to RGB(255,255,255). Any thoughts, been searching the internet for days. Was thinking of using a potentiometer to scroll through standard RGB values (255,255,255) then converting that number to 16bit hex but i cant figure that out either.
Anything helps, thanks guys!
Hello,
Use map() to map the value of the potentiometer to a value between 0 and 0xFFFF
Here are some functions for RGB888 <-> RGB565 conversions
uint16_t RGB888toRGB565( const uint32_t rgb888 )
{
uint8_t r, g, b;
unpackRGB888( rgb888, r, g, b );
return packRGB888toRGB565( r, g, b );
}
uint32_t RGB565toRGB888( const uint16_t rgb565 )
{
uint8_t r, g, b;
unpackRGB565( rgb565, r, g, b);
return packRGB565toRGB888( r, g, b );
}
void unpackRGB888( const uint32_t color, uint8_t &r, uint8_t &g, uint8_t &b )
{
r = (uint8_t)((color & 0xFF0000) >> 16);
g = (uint8_t)((color & 0x00FF00) >> 8);
b = (uint8_t)((color & 0x0000FF));
}
uint16_t packRGB888toRGB565( const uint8_t r, const uint8_t g, const uint8_t b )
{
return (uint16_t)(((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3));
}
void unpackRGB565( const uint16_t color, uint8_t &r, uint8_t &g, uint8_t &b )
{
r = (uint8_t)((color >> 11) & 0x1F);
g = (uint8_t)((color >> 5) & 0x3F);
b = (uint8_t)(color & 0x1F);
}
uint32_t packRGB565toRGB888( const uint8_t r, const uint8_t g, const uint8_t b )
{
return (uint32_t)((((r * 527 + 23) >> 6) << 16) | (((g * 259 + 33) >> 6) << 8) | ((b * 527 + 23) >> 6));
}
Example usage:
uint32_t magenta888 = 0xFF00FF;
uint16_t magenta565 = RGB888toRGB565( magenta888 );
Serial.println( magenta565, HEX ); // F81F
magenta888 = RGB565toRGB888( magenta565 );
Serial.println( magenta888, HEX ); // FF00FF