Skip to content

More accurate 8-bit <=> 5-bit RGB color conversion#1827

Merged
Rangi42 merged 2 commits intogbdev:masterfrom
Rangi42:rgb
Sep 8, 2025
Merged

More accurate 8-bit <=> 5-bit RGB color conversion#1827
Rangi42 merged 2 commits intogbdev:masterfrom
Rangi42:rgb

Conversation

@Rangi42
Copy link
Copy Markdown
Contributor

@Rangi42 Rangi42 commented Sep 7, 2025

Fixes #1823

The old formulas:

  • 8-to-5: c >> 3 (just truncates the low 3 bits, which biases by rounding down)
  • 5-to-8: (c << 3) | (c >> 2) (repeats the high 3 bits, which is not quite as accurate as true rounding)

The new formulas:

  • 8-to-5: (c * 31 + 127) / 255
  • 5-to-8: (c * 255 + 15) / 31
  • General A-to-B: (c * (2^B - 1) + (2^(A - 1) - 1)) / (2^A - 1)
image

@Rangi42 Rangi42 added this to the 1.0.0 milestone Sep 7, 2025
@Rangi42 Rangi42 added enhancement Typically new features; lesser priority than bugs rgbgfx This affects RGBGFX labels Sep 7, 2025
@Rangi42 Rangi42 force-pushed the rgb branch 3 times, most recently from 954d833 to bfb0de5 Compare September 7, 2025 20:20
@Rangi42
Copy link
Copy Markdown
Contributor Author

Rangi42 commented Sep 7, 2025

The four full_*.png tests that use the same image are failing. That's because the colors of the actual image don't match the colors specified in their input palettes. The plaintext GPL palette, compared to the verbose "Image colors:" output, makes this clear:

  0   0   0 000000 == 000000
252 252 252 fcfcfc != ffffff
196 199 238 c4c7ee != c6c6ef
154 143 224 9a8fe0 != 9c8ce7
99  93  150 635d96 != 635a94
41  47  101 292f65 != 292963
27  29  52  1b1d34 != 181831
255 227 174 ffe3ae != ffe7ad
205 187 171 cdbbab != cebdad
166 133 143 a6858f != a5848c
207 93  139 cf5d8b != ce5a8c
150 73  104 964968 != 944a6b
255 180 130 ffb482 != ffb584
221 134 125 dd867d != de847b
178 105 111 b2696f != b56b6b
246 198 94  f6c65e != f7c65a
228 144 87  e49057 != e79452
196 104 51  c46833 != c66b31
176 208 126 b0d07e != b5d67b
102 170 93  66aa5d != 63ad5a
82  181 171 52b5ab != 52b5ad
42  131 121 2a8379 != 29847b
28  86  89  1c5659 != 18525a
123 225 246 7be1f6 != 7be7f7
88  159 252 589ffc != 5a9cff
80  105 228 5069e4 != 526be7
46  68  174 2e44ae != 2942ad
128 86  212 8056d4 != 8452d6
90  59  150 5a3b96 != 5a3994
255 186 225 ffbae1 != ffbde7
230 135 197 e687c5 != e784c6
167 89  185 a759b9 != a55abd

Comment thread include/gfx/rgba.hpp
@Rangi42 Rangi42 merged commit 223b3d1 into gbdev:master Sep 8, 2025
24 checks passed
@Rangi42 Rangi42 deleted the rgb branch September 8, 2025 19:13
Rangi42 added a commit to Rangi42/rgbds that referenced this pull request Oct 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Typically new features; lesser priority than bugs rgbgfx This affects RGBGFX

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use accurate RGB 8-to-5 conversion

2 participants