From c2bba93238d0ffcc4cff12a2707a44cf7444dc25 Mon Sep 17 00:00:00 2001 From: warmCabin Date: Sun, 10 Aug 2025 00:56:16 -0400 Subject: [PATCH 1/5] comments --- src/lua-engine.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/lua-engine.cpp b/src/lua-engine.cpp index b337513b3..69d2541ac 100644 --- a/src/lua-engine.cpp +++ b/src/lua-engine.cpp @@ -3746,7 +3746,6 @@ enum */ static uint8 gui_colour_rgb(uint8 r, uint8 g, uint8 b) { static uint8 index_lookup[1 << (3+3+3)]; - int k; if (!gui_saw_current_palette) { @@ -3754,15 +3753,17 @@ 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 3 bits of r, g, and b + int k = ((r & 0xE0) << 1) | ((g & 0xE0) >> 2) | ((b & 0xE0) >> 5); + 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); + // Weights based on luminance formula? test_score = abs(r - tr) * 66 + abs(g - tg) * 129 + abs(b - tb) * 25; From 9ca2036bb22b4962320586f65764c53b3a6f66d2 Mon Sep 17 00:00:00 2001 From: warmCabin Date: Sun, 10 Aug 2025 00:58:43 -0400 Subject: [PATCH 2/5] comment out index_lookup (for now) Just want to see what happens. If the performance sucks, I'll try it with 5 or 6 bits. If the difference is negligible, I'll remove it properly. --- src/lua-engine.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lua-engine.cpp b/src/lua-engine.cpp index 69d2541ac..001185935 100644 --- a/src/lua-engine.cpp +++ b/src/lua-engine.cpp @@ -3755,7 +3755,7 @@ static uint8 gui_colour_rgb(uint8 r, uint8 g, uint8 b) { // Cache based on upper 3 bits of r, g, and b int k = ((r & 0xE0) << 1) | ((g & 0xE0) >> 2) | ((b & 0xE0) >> 5); - if (index_lookup[k] != GUI_COLOUR_CLEAR) return index_lookup[k]; + // if (index_lookup[k] != GUI_COLOUR_CLEAR) return index_lookup[k]; uint16 test, best = GUI_COLOUR_CLEAR; uint32 test_score, best_score = 0xffffffffu; for (test = 0; test < 0xff; test++) From adb1c9cc9f5dd14830090cb30de841a4840d3e17 Mon Sep 17 00:00:00 2001 From: warmCabin Date: Sun, 10 Aug 2025 01:22:05 -0400 Subject: [PATCH 3/5] change scoring to basic distance squared --- src/lua-engine.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lua-engine.cpp b/src/lua-engine.cpp index 001185935..6c21f3887 100644 --- a/src/lua-engine.cpp +++ b/src/lua-engine.cpp @@ -3763,10 +3763,10 @@ static uint8 gui_colour_rgb(uint8 r, uint8 g, uint8 b) { uint8 tr, tg, tb; if (test == GUI_COLOUR_CLEAR) continue; FCEUD_GetPalette(test, &tr, &tg, &tb); - // Weights based on luminance formula? - 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; From d4c0ecec91fbf06371f9ec1cc30457af057c8100 Mon Sep 17 00:00:00 2001 From: warmCabin Date: Mon, 11 Aug 2025 01:53:43 -0400 Subject: [PATCH 4/5] reenable index_lookup, for science --- src/lua-engine.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lua-engine.cpp b/src/lua-engine.cpp index 6c21f3887..d94fc9d0a 100644 --- a/src/lua-engine.cpp +++ b/src/lua-engine.cpp @@ -3755,7 +3755,7 @@ static uint8 gui_colour_rgb(uint8 r, uint8 g, uint8 b) { // Cache based on upper 3 bits of r, g, and b int k = ((r & 0xE0) << 1) | ((g & 0xE0) >> 2) | ((b & 0xE0) >> 5); - // if (index_lookup[k] != GUI_COLOUR_CLEAR) return index_lookup[k]; + if (index_lookup[k] != GUI_COLOUR_CLEAR) return index_lookup[k]; uint16 test, best = GUI_COLOUR_CLEAR; uint32 test_score, best_score = 0xffffffffu; for (test = 0; test < 0xff; test++) From 2152c04a5d1ff7e16967b1b20f6ed0a1327a022a Mon Sep 17 00:00:00 2001 From: warmCabin Date: Mon, 11 Aug 2025 02:22:39 -0400 Subject: [PATCH 5/5] do a 5-bit lookup table --- src/lua-engine.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/lua-engine.cpp b/src/lua-engine.cpp index d94fc9d0a..44f88346e 100644 --- a/src/lua-engine.cpp +++ b/src/lua-engine.cpp @@ -3745,7 +3745,7 @@ enum * ourselves. */ static uint8 gui_colour_rgb(uint8 r, uint8 g, uint8 b) { - static uint8 index_lookup[1 << (3+3+3)]; + static uint8 index_lookup[1 << (5+5+5)]; if (!gui_saw_current_palette) { @@ -3753,8 +3753,8 @@ static uint8 gui_colour_rgb(uint8 r, uint8 g, uint8 b) { gui_saw_current_palette = TRUE; } - // Cache based on upper 3 bits of r, g, and b - int 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 test_score, best_score = 0xffffffffu; @@ -3769,8 +3769,7 @@ static uint8 gui_colour_rgb(uint8 r, uint8 g, uint8 b) { (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()