How to set the color of a rectangle on a TFT display

I'm using the 3.5 TFT display from Adafruit and using their library and want to be able to set the color of a rectangle based on 3 variables, Red, Grn, Blu. There is only one variable for color that is the combined values of the three in one 16 bit number

Red are bits 15 to 11, Green are 10 to 5 and Blue are 4 to 0, with values of 31 (0x1F), 63 (0X3F), 31 (0x1F) respectfully. As shown here:

If my variables are from 0 to 255 I'll assume that I would first map them to those values then somehow combine them. Either by a bit shift or concatenation but not sure if that is the way.

How can this be done?

Thanks
John

     tft.fillRect(x, y, w, h, tft.color565(red, green, blue));

Hint. Google for "Adafruit_GFX Tutorial"

David.

I've gone through that tutorial a hundred times. It's been my bible for the last couple of weeks as I work on my first TFT project. I have not seen that reference, just drawRect(x, y, w, h, color). Not to say that it's not there I'm just not seeing it.

Thanks so much, I'll try that tonight when I get home
John

Sorry, meant to say I'm using the fillRect command not the drawRect

Look at Adafruit's graphicstest.ino example.

It exercises most of the important graphics methods. You will see color565() being used.

There are many other Adafruit_GFX methods e.g. for Fonts, Buttons, Canvases, drawing bitmaps, ...

David.

I've not tested this but something like:

uint8_t blue;
uint8_t green;
uint8_t red;
uint16_t RGB = ((blue >> 3) | ((green & 0x3f) << 5) | (red & 0x1f) << 11);

I think. Or something like that. Unless someone else points out why that's wrong and offers something better. Try it.

Your function is wrong.

God gave you the color565() method. It seems unwise to ignore it.

The convention is for the input colors to use the most significant bits. e.g. red = 255
Output functions produce the relevant colour depth and resolution e.g. 18-bits, 16-bits, 24-bits, ...

David.

Right, sorry, that's wrong. Try

uint8_t blue;
uint8_t green;
uint8_t red;
uint16_t RGB = ((blue >> 3) | ((green & 0xfc) << 5) | (red & 0xf8) << 11);

That might be wrong too, but if it is it will be for a different reason :o

(Well, it gets me closer to 1000 posts! :slight_smile: Do I turn into a pumpkin or something? :o )

[Edit] You beat me to it David ! :slight_smile: Is that better?

This is awesome, always great help on the forum, I'll give them a try tonight.

I've never been much of a believer but just in case I've never wanted to upset him either, I'll go with the one he gave us first.

Thanks so much
John

David

I gave your method , tft.color565(red, green, blue), a try and it work terrific. One question though what is the range of the 3 colors, red, green & blue? I thought they were 31, 61 & 31 because they only have 5 or 6 bits. but if I use those numbers it does display but very dim. If I use 255 for each color I get a nice bright box but it takes 8 bits to write 255.

Thanks for the education
John

It is not my method. It is a regular method in Adafruit_GFX style hardware libraries.

Most methods use a 565-format colour because most libraries use 16-bit pixels.

If you are importing a 24-bit colour e.g. from a .BMP or .JPG image, you want to convert it to the 16-bit 565 format.
Likewise, if you want to create your own colour.

David.

Stumpy_L:
One question though what is the range of the 3 colours, red, green & blue? I thought they were 31, 61 & 31 because they only have 5 or 6 bits. but if I use those numbers it does display but very dim. If I use 255 for each colour I get a nice bright box but it takes 8 bits to write 255.

Because converting from 8 bits to either 5 or 6 bits uses only the top 5 or 6 bits, not the bottom 5 or 6 bits, as David pointed out for my first answer, which made exactly that mistake. So, for example, if you have:
Blue = 1010 1010 then then it becomes 1010 1000, with the lower 3 bits lost. If your input doesn't use the upper 3 bits then you lose at both ends, so if your input is 0001 0101 the output will be 0001 0000. If you want to use 5 or 6 bits then you need to ignore the lower 3 or 2 bits.

David, I meant more your suggestion than method.

To both of you, There's so much that goes on in the background that I don't know, or have any control over, but it's good to know the reasons why so I can make adjustments for it.

Thanks for all the help and explanations
John

Adafruit Tests are well worth studying.

  1. show how to draw a shape
  2. show how to vary the colour
  3. show how to time a function
  4. ...

Likewise, there are two styles of library example:
5. one simple function
6. as many different functions that you can squeeze into a Uno Flash memory.

It is wise to run all the examples.
Observe the behaviour. Investigate the things that might be useful for your project.

Ask about something you don't understand.

David.

That's exactly what I do. Adafruit has great tutorials and code examples. I run those, go through them so I understand them and use those in my code for my projects, no need to reinvent the wheel.

I always ask questions, like here. It's best to understand the why than just the how.

John

Fantastic outcome using the color565() method! I was stumped by this through 3 solid days of reading!