跳转至

显示屏 — 颜色

M5GFX 源码: lgfx/v1/LGFXBase.hpp
返回: 显示屏概览

预定义颜色常量、颜色转换函数以及当前绘图颜色。


预定义颜色

通过 <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

简写: 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);

将 8 位 RGB 分量转换为 16 位压缩颜色(RGB565 格式)。

参数 类型 范围 说明
r uint8_t 0–255 红色分量
g uint8_t 0–255 绿色分量
b uint8_t 0–255 蓝色分量
返回值 说明
uint16_t 16 位 5-6-5 压缩格式
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);

将 8 位 RGB 转换为 32 位压缩颜色(RGB888 格式)。

参数 类型 范围 说明
r uint8_t 0–255
g uint8_t 0–255 绿
b uint8_t 0–255
返回值 说明
uint32_t 32 位 8-8-8 格式
uint32_t purple = M5Cardputer.Display.color888(128, 0, 128);

setColor() — RGB 分量

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

通过 RGB 分量设置当前绘图颜色。后续所有绘图操作均使用此颜色。

参数 类型 范围 说明
r uint8_t 0–255
g uint8_t 0–255 绿
b uint8_t 0–255
M5Cardputer.Display.setColor(0, 255, 0);  // 绿色
M5Cardputer.Display.fillRect(10, 10, 100, 100);

setColor() — 压缩值

void setColor(uint16_t color);

通过压缩颜色值设置当前绘图颜色。

参数 类型 说明
color uint16_t 16 位压缩颜色 (RGB565)
M5Cardputer.Display.setColor(TFT_CYAN);
M5Cardputer.Display.drawCircle(160, 120, 50);

// 自定义压缩颜色
M5Cardputer.Display.setColor(M5Cardputer.Display.color565(200, 100, 50));

setBaseColor()

void setBaseColor(uint16_t color);

设置 fillScreen() 无参调用或 clear() 使用的背景色。

M5Cardputer.Display.setBaseColor(TFT_NAVY);
M5Cardputer.Display.fillScreen();  // 填充 TFT_NAVY

getRawColor()

uint32_t getRawColor() const;

返回当前绘图颜色的原始格式。

uint32_t current = M5Cardputer.Display.getRawColor();

getColorDepth()

color_depth_t getColorDepth() const;

返回当前色深(每像素位数)。M5Cardputer 通常为 16

color_depth_t bpp = M5Cardputer.Display.getColorDepth();
M5Cardputer.Display.printf("当前色深: %d bpp\n", bpp);