I love Adafruit. I especially love their little 1.8 inch TFT screen that you can hook up directly to an Arduino. It comes with a library, but one thing the library is missing is a method for generating arbitrary colors. The colors are in an odd packed 16 bit binary format, with 5 bits for red, 6 bits for green, and 5 bits for blue. They give you a few color constants to start out with, but I wanted arbitrary colors. So I re-familiarized myself with C's bit operators, wrote some code, and now you don't have to.
The first function assumes you're passing a value from 0 to 255. The second function lets you specify the mapping, so if you wanted to create a color based on values read from the analog pins, you could do that by specifying 0 and 1023 as the minimum and maximum values.
Sorry for the lack of formatting, I can't get the formatting to look right. It should cut and paste just fine.
uint16_t makeColor(int r, int g, int b)
{
return makeColor(r, g, b, 0, 255);
}
uint16_t makeColor(int r, int g, int b, int minimum, int maximum)
{
int red = map(r, minimum, maximum, 0, 32);
int green = map(g, minimum, maximum, 0, 64);
int blue = map(b, minimum, maximum, 0, 32);
uint16_t color = 0;
color = color | red;
color = color << 6;
color = color | green;
color = color << 5;
color = color | blue;
return color;
}