Edzelf / ESP32-Radio

Internet radio based on ESP32, VS1053 and a TFT screen.
GNU General Public License v3.0
988 stars 229 forks source link

Custom colours #40

Open koskee opened 7 years ago

koskee commented 7 years ago

So the TFT library uses 16-bit colours (which I personally have never even run into before, other than the few times I've said UGHHH when using safe mode on an old Win95 or 98 machine). I certainly didn't know how to work with the raw hex values, and so I went about finding tables of colours and their associated 16-bit hex values.

Then I found something better, and so much simpler.

Thought maybe you might want to add it for the benefit of people wanting to define their own colors for the tft display. In fact, anyone reading can add this super easily to their sketch and start to customize their radio displays to taste. This simple function allows you to use the much more familiar 24-bit colour defines that are common in HTML and pretty much anywhere I've ever seen colours defined on computers. (and because it doesn't require hexadecimal number format, its even easier: just 0-255 for each color. more is more, less is less.)

uint16_t getColor(uint8_t red, uint8_t green, uint8_t blue) { red >>= 3; green >>= 2; blue >>= 3; return (red << 11) | (green << 5) | blue; } DONE.

Here's the link for more info: link

koskee commented 7 years ago

oops, it looks like the colours need to be in the exact opposite order to end up displaying properly --> BGR instead of RGB.

so, if we modify the function slightly..

uint16_t getColor(uint8_t red, uint8_t green, uint8_t blue)
{
red >>= 3;
green >>= 2;
blue >>= 3;
return (blue << 11) | (green << 5) | red;
}

This should work, i'm going to test it out now.

EDIT : Yes, that function works just fine on my display, although I'm not sure if every display will be exactly the same. (but, it should be fairly obvious to most people how to re-arrange the colors. Simply compare the differences between the two functions listed above )