Skip to content

Display — Colors

M5GFX source: lgfx/v1/LGFXBase.hpp
Back to: Display overview

Color constants, conversion functions, and the current drawing color.


Named Colors

Global constants available via <M5GFX.h>:

TFT_BLACK      TFT_NAVY       TFT_DARKGREEN   TFT_DARKCYAN
TFT_MAROON     TFT_PURPLE     TFT_OLIVE       TFT_LIGHTGREY
TFT_DARKGREY   TFT_BLUE       TFT_GREEN       TFT_CYAN
TFT_RED        TFT_MAGENTA    TFT_YELLOW      TFT_WHITE
TFT_ORANGE     TFT_GREENYELLOW TFT_PINK       TFT_BROWN
TFT_GOLD       TFT_SILVER     TFT_SKYBLUE     TFT_VIOLET

Shorthand: BLACK, WHITE, RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA.

M5Cardputer.Display.fillScreen(TFT_BLACK);
M5Cardputer.Display.fillRect(10, 10, 50, 50, TFT_RED);
M5Cardputer.Display.drawString("OK", 10, 70);

color565()

static constexpr uint16_t color565(uint8_t r, uint8_t g, uint8_t b);

Converts 8-bit RGB components to a 16-bit packed color (RGB565).

Parameter Type Range Description
r uint8_t 0–255 Red component
g uint8_t 0–255 Green component
b uint8_t 0–255 Blue component
Return Description
uint16_t 16-bit packed 5-6-5 format
uint16_t orange = M5Cardputer.Display.color565(255, 165, 0);
M5Cardputer.Display.fillScreen(orange);

color888()

static constexpr uint32_t color888(uint8_t r, uint8_t g, uint8_t b);

Converts 8-bit RGB to 32-bit packed color (RGB888).

Parameter Type Range Description
r uint8_t 0–255 Red
g uint8_t 0–255 Green
b uint8_t 0–255 Blue
Return Description
uint32_t 32-bit packed 8-8-8 format
uint32_t purple = M5Cardputer.Display.color888(128, 0, 128);

setColor() — RGB

void setColor(uint8_t r, uint8_t g, uint8_t b);

Sets the current drawing color from RGB components. All subsequent drawing operations use this color.

Parameter Type Range Description
r uint8_t 0–255 Red
g uint8_t 0–255 Green
b uint8_t 0–255 Blue
M5Cardputer.Display.setColor(0, 255, 0);  // Green
M5Cardputer.Display.fillRect(10, 10, 100, 100);

setColor() — Packed

void setColor(uint16_t color);

Sets the current drawing color from a packed color value.

Parameter Type Description
color uint16_t 16-bit packed color (RGB565)
M5Cardputer.Display.setColor(TFT_CYAN);
M5Cardputer.Display.drawCircle(160, 120, 50);

// Custom packed color
M5Cardputer.Display.setColor(M5Cardputer.Display.color565(200, 100, 50));

setBaseColor()

void setBaseColor(uint16_t color);

Sets the background color used by fillScreen() when called without arguments, or by clear().

M5Cardputer.Display.setBaseColor(TFT_NAVY);
M5Cardputer.Display.fillScreen();  // Fills with TFT_NAVY

getRawColor()

uint32_t getRawColor() const;

Returns the current drawing color in raw format.

uint32_t current = M5Cardputer.Display.getRawColor();

getColorDepth()

color_depth_t getColorDepth() const;

Returns the current color depth (bits per pixel). For M5Cardputer this is typically 16.

color_depth_t bpp = M5Cardputer.Display.getColorDepth();
M5Cardputer.Display.printf("Color depth: %d bpp\n", bpp);