8 bit RGB value to 16 bit Colour Value for Nextion Display

Hello,

I am working on a simple project which uses an ESP8266 to receive data from a web server from a colour picker. I have that working and all I need to do now is to send that value to a nextion display. However the nextion display only supports 16 bit colour values ranging from 0 to 65535 whilst the colour picker on my web server sends back a 24 bit RGB value ranging from 0 to 255 for each primary colour (eg, red = 255, green = 67, blue=100).
(Note: Each colour in the RGB value is 8 bits, since there are 3 the total amount is 24 bits)
Is there a way to convert the 24 bit RGB value to the 16bit colour value for the display?

Ryan

techinc1510:
Hello,

I am working on a simple project which uses an ESP8266 to receive data from a web server from a colour picker. I have that working and all I need to do now is to send that value to a nextion display. However the nextion display only supports 16 bit colour values ranging from 0 to 65535 whilst the colour picker on my web server sends back a 24 bit RGB value ranging from 0 to 255 for each primary colour (eg, red = 255, green = 67, blue=100).
(Note: Each colour in the RGB value is 8 bits, since there are 3 the total amount is 24 bits)
Is there a way to convert the 24 bit RGB value to the 16bit colour value for the display?

Ryan

looking at the RGB565 16 Bit Color Code

Arduino-16-bit-tft-color-definition.png

IMHO you could used the map function.

so doing something like this maybe....

uint16_t red_565 = map(red_value, 0, 255, 0, 31);

uint16_t green_565 = map(green_value, 0, 255, 0, 63);

uint16_t blue_565 = map(blue_value, 0, 255, 0, 31);

then combine them to get the 16 bit colour value like:

uint16_t rgb_565 = (red_565<<11)|(green_565<<5)|blue_565;

bear in mind that it is very likely that you will lose some colour tones.

hope that helps...

EDIT:
corrected
uint16_t green_565 = map(green_value, 0, 255, 0, 31); to uint16_t green_565 = map(green_value, 0, 255, 0, 63);

Arduino-16-bit-tft-color-definition.png

// ---------------------------------------------
// rgb565     version 2
// ---------------------------------------------
// Convert a hexadecimal rgb value into a rgb565 format.
// To be able to use HTML RGB color format in a Arduino sketch.
//
// When a text is needed, then the result can converted into 
// a decimal number as text with a sprintf with %u, or with utoa().
// Great HTML color picker tool: 
//   https://www.w3schools.com/colors/colors_picker.asp
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);
}

sherzaad:
looking at the RGB565 16 Bit Color Code

Arduino-16-bit-tft-color-definition.png

IMHO you could used the map function.

so doing something like this maybe....

uint16_t red_565 = map(red_value, 0, 255, 0, 31);

uint16_t green_565 = map(green_value, 0, 255, 0, 31);

uint16_t blue_565 = map(blue_value, 0, 255, 0, 31);




then combine them to get the 16 bit colour value like:



uint16_t rgb_565 = (red_565<<11)|(green_565<<5)|blue_565;




bear in mind that it is very likely that you will lose some colour tones.

hope that helps...

Can confirm this works, thanks for your help

Hope this helps someone else too!

Ryan

techinc1510:
Can confirm this works, thanks for your help

Hope this helps someone else too!

Ryan

That’s good news! just one thing to point out…

uint16_t green_565 = map(green_value, 0, 255, 0, 31); should be

uint16_t green_565 = map(green_value, 0, 255, 0, 63); since 6 bits and not 5 is allocated for green