MCPcopy Create free account
hub / github.com/TASEmulators/fceux / gui_colour_rgb

Function gui_colour_rgb

src/lua-engine.cpp:3710–3736  ·  view source on GitHub ↗

* Returns an index approximating an RGB colour. * TODO: This is easily improvable in terms of speed and probably * quality of matches. (gd overlay & transparency code call it a lot.) * With effort we could also cheat and map indices 0x08 .. 0x3F * ourselves. */

Source from the content-addressed store, hash-verified

3708 * ourselves.
3709 */
3710static uint8 gui_colour_rgb(uint8 r, uint8 g, uint8 b) {
3711 static uint8 index_lookup[1 << (3+3+3)];
3712 int k;
3713
3714 if (!gui_saw_current_palette)
3715 {
3716 memset(index_lookup, GUI_COLOUR_CLEAR, sizeof(index_lookup));
3717 gui_saw_current_palette = TRUE;
3718 }
3719
3720 k = ((r & 0xE0) << 1) | ((g & 0xE0) >> 2) | ((b & 0xE0) >> 5);
3721 uint16 test, best = GUI_COLOUR_CLEAR;
3722 uint32 best_score = 0xffffffffu, test_score;
3723 if (index_lookup[k] != GUI_COLOUR_CLEAR) return index_lookup[k];
3724 for (test = 0; test < 0xff; test++)
3725 {
3726 uint8 tr, tg, tb;
3727 if (test == GUI_COLOUR_CLEAR) continue;
3728 FCEUD_GetPalette(test, &tr, &tg, &tb);
3729 test_score = abs(r - tr) * 66 +
3730 abs(g - tg) * 129 +
3731 abs(b - tb) * 25;
3732 if (test_score < best_score) best_score = test_score, best = test;
3733 }
3734 index_lookup[k] = best;
3735 return best;
3736}
3737
3738void FCEU_LuaUpdatePalette()
3739{

Callers 1

FCEU_LuaGuiFunction · 0.85

Calls 1

FCEUD_GetPaletteFunction · 0.50

Tested by

no test coverage detected