fix(st7789): improve PWM backlight dimming color accuracy - #4
Merged
dzo merged 1 commit intoApr 2, 2026
Merged
Conversation
…ss 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
Author
|
Again, this work was done by the https://micropythonos.com/ team to allow running a normal MicroPythonOS build (which works on many boards, including the LilyGo T-Display-S3) on an emulated esp32s3. Thank you for all your work on QEMU! |
dzo
approved these changes
Apr 2, 2026
dzo
left a comment
Collaborator
There was a problem hiding this comment.
Thanks, I hadn't noticed this.
ThomasFarstrike
added a commit
to ThomasFarstrike/retro-go
that referenced
this pull request
Jun 9, 2026
The path VBE_setPalette -> SDL_SetColors converted Duke3D's 6-bit palette values through an unnecessary 8-bit intermediate, creating an asymmetric precision issue analogous to the QEMU st7789 backlight fix (a159x36/qemu#4): Old: r6/b6 -> 8-bit via (v<<2)|(v>>4) -> >>3 -> r5 (net: v6>>1, floor) g6 -> 8-bit via (v<<2)|(v>>4) -> >>2 -> g6 (net: identity, lossless) For any palette entry with odd source values, R5=B5=floor(v/2)=0 while G6=v remains non-zero — producing a pure-green pixel instead of near-black. This is visible as a greenish cast in dark/shadowed areas. Fix: add SDL_SetPalette565() which converts directly from the 6-bit source, zeroing the green LSB to give it the same effective 5-bit precision as R/B: r5 = v6 >> 1 g6 = (v6 >> 1) << 1 // 5-bit precision in the 6-bit RGB565 green field b5 = v6 >> 1 For neutral input (r=g=b=val): r5/31 == (g6>>1)/31 == b5/31 -> neutral gray. At full brightness (val=63): r5=31, g6=62, b5=31 — green is 1/63 (~1.6%) shy of maximum, which is perceptually indistinguishable. Also removes the 1KB static SDL_Color fmt_swap[256] intermediate buffer and the SDL_SetColors() call, reducing per-palette-update work by ~50%.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
Improves visual quality dramatically:
Before:
at 90% brightness, the gray and blue turned greenish
After:

at 90% brightness, all the colors look as they should