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

Function str2colour

src/lua-engine.cpp:3785–3832  ·  view source on GitHub ↗

* Converts an integer or a string on the stack at the given * offset to a RGB32 colour. Several encodings are supported. * The user may construct their own RGB value, given a simple colour name, * or an HTML-style "#09abcd" colour. 16 bit reduction doesn't occur at this time. * NES palettes added with notation "P00" to "P3F". "P40" to "P7F" denote LUA palettes. */

Source from the content-addressed store, hash-verified

3783 * NES palettes added with notation "P00" to "P3F". "P40" to "P7F" denote LUA palettes.
3784 */
3785static inline bool str2colour(uint32 *colour, lua_State *L, const char *str) {
3786 if (str[0] == '#') {
3787 int color;
3788 sscanf(str+1, "%X", &color);
3789 int len = strlen(str+1);
3790 int missing = std::max<int>(0, 8-len);
3791 color <<= missing << 2;
3792 if(missing >= 2) color |= 0xFF;
3793 *colour = color;
3794 return true;
3795 }
3796 else if (str[0] == 'P') {
3797 uint8 palette;
3798 uint8 tr, tg, tb;
3799
3800 if (strlen(str+1) == 2) {
3801 palette = ((hex2int(L, str[1]) * 0x10) + hex2int(L, str[2]));
3802 } else if (strlen(str+1) == 1) {
3803 palette = (hex2int(L, str[1]));
3804 } else {
3805 luaL_error(L, "palettes are defined with P## hex notion");
3806 return false;
3807 }
3808
3809 if (palette > 0x7F) {
3810 luaL_error(L, "palettes range from P00 to P7F");
3811 return false;
3812 }
3813
3814 FCEUD_GetPalette(palette + 0x80, &tr, &tg, &tb);
3815 // Feeding it RGBA, because it will spit out the right value for me
3816 *colour = LUA_BUILD_PIXEL(tr, tg, tb, 0xFF);
3817 return true;
3818 }
3819 else {
3820 if(!strnicmp(str, "rand", 4)) {
3821 *colour = ((rand()*255/RAND_MAX) << 8) | ((rand()*255/RAND_MAX) << 16) | ((rand()*255/RAND_MAX) << 24) | 0xFF;
3822 return true;
3823 }
3824 for(size_t i = 0; i < sizeof(s_colorMapping)/sizeof(*s_colorMapping); i++) {
3825 if(!stricmp(str,s_colorMapping[i].name)) {
3826 *colour = s_colorMapping[i].value;
3827 return true;
3828 }
3829 }
3830 }
3831 return false;
3832}
3833static inline uint32 gui_getcolour_wrapped(lua_State *L, int offset, bool hasDefaultValue, uint32 defaultColour) {
3834 switch (lua_type(L,offset)) {
3835 case LUA_TSTRING:

Callers 1

gui_getcolour_wrappedFunction · 0.85

Calls 3

hex2intFunction · 0.85
luaL_errorFunction · 0.85
FCEUD_GetPaletteFunction · 0.50

Tested by

no test coverage detected