Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/lua-engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3745,31 +3745,31 @@ enum
* ourselves.
*/
static uint8 gui_colour_rgb(uint8 r, uint8 g, uint8 b) {
static uint8 index_lookup[1 << (3+3+3)];
int k;
static uint8 index_lookup[1 << (5+5+5)];

if (!gui_saw_current_palette)
{
memset(index_lookup, GUI_COLOUR_CLEAR, sizeof(index_lookup));
gui_saw_current_palette = TRUE;
}

k = ((r & 0xE0) << 1) | ((g & 0xE0) >> 2) | ((b & 0xE0) >> 5);
// Cache based on upper 5 bits of r, g, and b
int k = ((r & 0xF8) << 7) | ((g & 0xF8) << 2) | ((b & 0xF8) >> 3);
if (index_lookup[k] != GUI_COLOUR_CLEAR) return index_lookup[k];
uint16 test, best = GUI_COLOUR_CLEAR;
uint32 best_score = 0xffffffffu, test_score;
if (index_lookup[k] != GUI_COLOUR_CLEAR) return index_lookup[k];
uint32 test_score, best_score = 0xffffffffu;
for (test = 0; test < 0xff; test++)
{
uint8 tr, tg, tb;
if (test == GUI_COLOUR_CLEAR) continue;
FCEUD_GetPalette(test, &tr, &tg, &tb);
test_score = abs(r - tr) * 66 +
abs(g - tg) * 129 +
abs(b - tb) * 25;
// Basic distance squared
test_score = (r - tr) * (r - tr) +
(g - tg) * (g - tg) +
(b - tb) * (b - tb);
if (test_score < best_score) best_score = test_score, best = test;
}
index_lookup[k] = best;
return best;
return index_lookup[k] = best;
}

void FCEU_LuaUpdatePalette()
Expand Down