Convert HEX color to RGB565


I'm looking for a small function that converts a HEX color to a RGB565 color.

Some easy examples:
#000000 should return 0x0000 (black)
#FF0000 should return 0xF800 (red)
#00FF00 should return 0x07E0 (green)
#0000FF should return 0x001F (blue)
#FFFFFF should return 0xFFFF (white)

...and so on.

I searched a while, but up til now I didn't get the right answer.

Any Idea to solve this?

Thanks and best regards,

I just put your thread title into Google and got hits.

Did you found a function?

Could you give me a link for such a function?

Thanks and best regards,

Look in any TFT library.

Any Idea to solve this?

something like this:

struct Rgb24{
  uint8_t r;
  uint8_t g;
  uint8_t b;

struct Bitfield{
    uint8_t b : 5;
    uint8_t g : 6;
    uint8_t r : 5;
union Rgb16{
  Bitfield color;
  uint16_t value;

Rgb24 color24 = {0x0, 0xFF, 0x0};

void setup() 
  Rgb16 thisValue = rgb24to16(color24);
  Serial.println(thisValue.color.r, BIN);
  Serial.println(thisValue.color.g, BIN);
  Serial.println(thisValue.color.b, BIN);
  Serial.println(thisValue.value, BIN);

  Serial.println(thisValue.color.r, HEX);
  Serial.println(thisValue.color.g, HEX);
  Serial.println(thisValue.color.b, HEX);
  Serial.println(thisValue.value, HEX);

void loop() 

Rgb16 rgb24to16(Rgb24 color)
  Rgb16 result;
  result.color.r = map(color.r, 0x0, 0xFF, 0x0, 0x1F);
  result.color.g = map(color.g, 0x0, 0xFF, 0x0, 0x3F);
  result.color.b = map(color.b, 0x0, 0xFF, 0x0, 0x1F);
  return result;

Yes, I can Google
I found a lot of online converters.
I can find a matching rgb565 colorcode for a hex code.
I’m no looking for rgb24 converters.
I had a look in several tft libraries.
25 years ago I coded a lot Basic, Pascal and Visual Basic.
But I never coded one line arduino.
This is why I need some help to get started with it.

I have still no answer to my question.

I’m looking for a function, whichs returns “0x07E0” when it gets “#00FF00

The best thing I found up til now is:

uint16_t rgb565( const unsigned long rgb)
  uint16_t R = (rgb >> 16) & 0xFF;
  uint16_t G = (rgb >>  8) & 0xFF;
  uint16_t B = (rgb      ) & 0xFF;

  uint16_t ret  = (R & 0xF8) << 8;  // 5 bits
           ret |= (G & 0xFC) << 3;  // 6 bits
           ret |= (B & 0xF8) >> 3;  // 5 bits
  return( ret);

This is nice short and easy.
But it works not like I need it.
Maybe somebody can adapt this for me, that it returns “0x07E0” when it gets “#00FF00” ?

Thanks and best regards,

This isn't really a coding problem, it's a math problem. Can you write out in math what needs to happen? If you can do that then you can easily code this in any language.

You can process the input string with strtoul(inputCstring, NULL, 16) after discarding the '#'.

I'm sorry, I have no idea how to proceed.
Can you show how the complete code needs to look like?

I'm sorry, I have no idea how to proceed.
Can you show how the complete code needs to look like?

You can post in the "gigs and collaborations" forum if you want someone to code it for you.