Skip to content

Instantly share code, notes, and snippets.

@Quit
Created September 26, 2012 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Quit/3787245 to your computer and use it in GitHub Desktop.
Save Quit/3787245 to your computer and use it in GitHub Desktop.
#include "lgColor.hpp"
namespace gle {
namespace lgColor {
COLOR_CLASS& checkColor(lua_State* L, int idx) {
return *gle::getObject<COLOR_CLASS>(L, idx).object();
}
COLOR_CLASS optColor(lua_State* L, int idx, Gosu::Color defaultColor) {
if (lua_isnoneornil(L, idx))
return defaultColor;
return checkColor(L, idx);
}
void pushColor(lua_State* L, Gosu::Color& color, bool weak) {
gle::pushObject(L, color, weak);
}
LUA_FUNCTION(Color) {
int r = luaL_checkint(L, 1), g = luaL_checkint(L, 2), b = luaL_checkint(L, 3), a = luaL_optint(L, 4, 255);
gle::pushObject(L, *new Gosu::Color(a & 255, r & 255, g & 255, b & 255), false);
return 1;
}
LUA_FUNCTION(LOM__index) {
Gosu::Color& color = checkColor(L, 1);
const char* key = lua_tostring(L, 2);
if (!strcmp(key, "r") || !strcmp(key, "red"))
lua_pushinteger(L, color.red());
else if (!strcmp(key, "b") || !strcmp(key, "blue"))
lua_pushinteger(L, color.blue());
else if (!strcmp(key, "g") || !strcmp(key, "green"))
lua_pushinteger(L, color.green());
else if (!strcmp(key, "a") || !strcmp(key, "alpha"))
lua_pushinteger(L, color.alpha());
else if (!strcmp(key, "h") || !strcmp(key, "hue"))
lua_pushnumber(L, color.hue());
else if (!strcmp(key, "s") || !strcmp(key, "saturation"))
lua_pushnumber(L, color.saturation());
else if (!strcmp(key, "v") || !strcmp(key, "value"))
lua_pushnumber(L, color.value());
else
// Otherwise, let normal magic happen.
return gle::LOM__index<Gosu::Color>(L);
// We pushed the result of whatever was asked.
return 1;
}
LUA_FUNCTION(LOM__newindex) {
Gosu::Color& color = checkColor(L, 1);
const char* key = lua_tostring(L, 2);
if (!strcmp(key, "r") || !strcmp(key, "red"))
color.setRed(luaL_checkint(L, 3) & 255);
else if (!strcmp(key, "b") || !strcmp(key, "blue"))
color.setBlue(luaL_checkint(L, 3) & 255);
else if (!strcmp(key, "g") || !strcmp(key, "green"))
color.setGreen(luaL_checkint(L, 3) & 255);
else if (!strcmp(key, "a") || !strcmp(key, "alpha"))
color.setAlpha(luaL_checkint(L, 3) & 255);
else if (!strcmp(key, "h") || !strcmp(key, "hue"))
color.setHue(fmod(luaL_checknumber(L, 3), 360));
else if (!strcmp(key, "s") || !strcmp(key, "saturation"))
color.setSaturation(fmod(luaL_checknumber(L, 3), 360));
else if (!strcmp(key, "v") || !strcmp(key, "value"))
color.setValue(fmod(luaL_checknumber(L, 3), 360));
// If none of these cases happened, then we use the normal function.
else
return gle::LOM__newindex<Gosu::Color>(L);
// Otherwise, we did our job and can return 0.
return 0;
}
LUA_FUNCTION(ColorHSV) {
gle::pushObject(L, *new Gosu::Color(Gosu::Color::fromAHSV(luaL_optint(L, 4, 255) % 255, fmod(luaL_checknumber(L, 1), 360), fmod(luaL_checknumber(L, 2), 360), fmod(luaL_checknumber(L, 3), 360))), false);
return 1;
}
const luaL_Reg reg[] = {
{ NULL, NULL }
};
}
void registerColor(lua_State* L) {
registerObject<COLOR_CLASS>(L, lgColor::reg);
LUA_OBJECT_ADD_TOSTRING(L, COLOR_CLASS);
lua_pushcfunction(L, lgColor::LOM__index);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lgColor::LOM__newindex);
lua_setfield(L, -2, "__newindex");
lua_pop(L, 1);
// Global color functions
lua_register(L, "Color", lgColor::Color);
lua_register(L, "ColorHSV", lgColor::ColorHSV);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment