# Getting the Color You Want

Many of the Arduino compatible TFT displays available can display up to 65,536 colors .   That’s a lot of color and it takes 16 bits of information to tell our display what color to show.

This guide will help you to understand the sixteen bit color code,  but more importantly,  it will help you to select the color you want and put it to immediate use right now.

# The RGB565 16 Bit Color Code

RGB stands for Red, Green and Blue.   Given infinite resolution in defining how much Red, Green, and Blue light is blended together, it it is possible to define every color there is.

The sixteen bit RGB color code has finite resolution and therefore only allows us to come up with 65,536 of those colors.  Still, the colors that can be displayed reach across the color spectrum with sufficient resolution to give us the ability to display nice color images. The color resolution is not as good as a modern computer display, but it is good enough for most projects.

The graphic below shows how these colors are defined.

Lets take the five bits assigned to the red values as an example:

Binary ‘00000’ (decimal 0) says to use no red.

Binary ‘00001’ (decimal 1) indicates that the least amount red is to be used.

Binary ‘11111’ (decimal 32) tells the display to use as much red as possible.

The same approach is used for the green and blue values.

However, pay attention to the green.   You will note that the green portion of the color code allows for six bits instead of five.  That gives us 64 possible selections.

That’s important,  because if you want half Red or Blue, you’re specifying 15.  On the other hand,  a half Green means we’re using 31.

# Specifying and Choosing Your Color

Specifying the TFT display colors within an Arduino sketch is frequently done by using the hexadecimal (or decimal) equivalent of the 16 bit binary color code.

How you determine what value to choose can be a pretty daunting task if you don’t use a compatible tool or utility.   Most online tools or utilities these days use the 24 bit color format popular with computer displays and that’s a problem for the Arduino TFT displays .

The key therefore is to use one that returns a sixteen bit format.

There a few of these websites around and THIS ONE  is one of my favorites.   Simply visit the page and use the utility to select your color as illustrated below:

# Bit Map Images and Color

Many bit map images these days also use the 24 bit color format (AKA RGB888).  As alluded to above,  that is a problem for your Arduino.    Some of the TFT graphics libraries (like Adafruit’s) handle this for you and others don’t.

While its beyond the scope of this article to go into this in depth,   utilities like the one found here can help.

# RGB565 SAMPLE SKETCH

In this sketch,  I am using the Adafruit_GX.h library and I am also using an ILI9163 display.  If you want to mimic the set up exactly,  you can read this article here.

However, that should not be necessary.   Instead, all you should really need to is review the sketch below to see how color is specified in the context of a sketch.

All I’m really doing here is creating a small filled rectangle in the upper left hand corner of the screen that I have painted white.

```// Henry's Bench
//  RGB565 Tutorial

#include <SPI.h>
#include <TFT_ILI9163C.h>

// Definition of WHITE - You Could Create a Pallete here
#define WHITE   0xFFFF
#define RECTCOLOR 0x257B

#define CS 10
#define DC 9

// Declare an instance of the ILI9163
TFT_ILI9163C tft = TFT_ILI9163C(CS, 8, DC);

void setup() {
tft.begin();
tft.fillScreen(WHITE);
}

void loop(){
tft.fillRect(0, 0, 50, 50,  RECTCOLOR);
delay(500);
}

```