diff --git a/src/lua-engine.cpp b/src/lua-engine.cpp index b337513b3..44f88346e 100644 --- a/src/lua-engine.cpp +++ b/src/lua-engine.cpp @@ -3745,8 +3745,7 @@ 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) { @@ -3754,22 +3753,23 @@ static uint8 gui_colour_rgb(uint8 r, uint8 g, uint8 b) { 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()