Skip to content

Commit 9babc8d

Browse files
fix(st7789): fix backlight dimming accuracy and remove color/brightness bias
Original code expanded RGB565 to 8-bit channels, multiplied with truncating division (/256), then shifted back — causing consistent darkening and minor hue errors, especially noticeable at 40–80% brightness. Improved version: - Extracts components in native precision: 5-bit red/blue, 6-bit green - Scales with proper rounding: (val * backlight + 127) >> 8 - Packs directly back to RGB565 format Improves visual quality dramatically: - Grays/whites stay neutral instead of going too dark or greenish - Better preservation of shadow detail - More natural fade-to-black behavior overall
1 parent 56de0e8 commit 9babc8d

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

hw/display/st7789v.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,15 @@ static void st7789_update_display(void *opaque) {
334334
for(int x=0;x<c->width;x++) {
335335
uint16_t rgb565=c->fb_data[(y+c->y_offset)*320+x+c->x_offset];
336336
if(c->backlight<255) {
337-
uint32_t r=(rgb565>>8) & 0xf8;
338-
uint32_t g=(rgb565>>3) & 0xfc;
339-
uint32_t b=(rgb565<<2) & 0xf8;
340-
r=(r*c->backlight)/256;
341-
g=(g*c->backlight)/256;
342-
b=(b*c->backlight)/256;
343-
rgb565=(uint16_t)(((r >> 3) << 11) | ((g >> 2) << 5) | ( b >> 3));
337+
// Extract native-bit counts
338+
uint32_t r = (rgb565 >> 11) & 0x1F;
339+
uint32_t g = (rgb565 >> 5) & 0x3F;
340+
uint32_t b = rgb565 & 0x1F;
341+
// Scale with rounding-to-nearest (add half LSB before shift)
342+
r = (r * c->backlight + 127) >> 8;
343+
g = (g * c->backlight + 127) >> 8;
344+
b = (b * c->backlight + 127) >> 8;
345+
rgb565=(uint16_t)((r << 11) | (g << 5) | b);
344346
}
345347
uint32_t index=(y+c->skin_y_offset)*c->skin_width+x+c->skin_x_offset;
346348
if(index<320*320)

0 commit comments

Comments
 (0)