How to get custom colors on SSD1331 RGB OLED Display

I'm using a SSD1331 RGB OLED Display such as this one.

In the example code, the colors are preset using 4-digit hex code like this:

#define	BLACK           0x0000
#define	BLUE            0x001F
#define	RED             0xF800
#define	GREEN           0x07E0
#define CYAN            0x07FF
#define MAGENTA         0xF81F
#define YELLOW          0xFFE0
#define WHITE           0xFFFF

How do I use a 6-digit hex code for the color instead of 4-digit? Is it possible? Do I need a different library instead of the Adafruit GFX one? Thanks!

You don't quote the library name. I don't want to guess.
There will be an existing library function.

Your 6 digit hex code in binary

RRRRRRRRGGGGGGGGBBBBBBBB  24bits
RRRRR---GGGGGG--BBBBB---  16bits

Ok, so binary works! Thank you for the help!

Go on. Is it really that difficult to say which library you are using?

Then you could use the proper library function.

I believe it's the Adafruit GFX library. Is there an existing library function for using a 6-digit hex?

Adafruit color display libraries have a method for converting to 16-bit color e.g. from https://github.com/adafruit/Adafruit-GFX-Library/blob/master/Adafruit_SPITFT.cpp

/*!
    @brief   Given 8-bit red, green and blue values, return a 'packed'
             16-bit color value in '565' RGB format (5 bits red, 6 bits
             green, 5 bits blue). This is just a mathematical operation,
             no hardware is touched.
    @param   red    8-bit red brightnesss (0 = off, 255 = max).
    @param   green  8-bit green brightnesss (0 = off, 255 = max).
    @param   blue   8-bit blue brightnesss (0 = off, 255 = max).
    @return  'Packed' 16-bit color value (565 format).
*/
uint16_t Adafruit_SPITFT::color565(uint8_t red, uint8_t green, uint8_t blue) {
  return ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | (blue >> 3);
}

The arguments are individual 8-bit R, G, B.
You would extract each 8-bit field from your 24-bit "6-digit hex"

Adafruit_SPITFT class gets built whenever you inherit from Adafruit_GFX.

Most third party libraries will inherit graphics from Adafruit_GFX. But they will probably avoid the mash-up that is Adafruit_SPITFT. Hence they include their own color565(r, g, b) method.

David.

Ok, thank you for the information.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.