#include <unistd.h>
#include <pspdisplay.h>
#include <oslib/oslib.h>
#include "share.h"

static int new(lua_State *L)
{
	unsigned int r = CLAMP(luaL_checkint(L, 1), 0, 255); 
	unsigned int g = CLAMP(luaL_checkint(L, 2), 0, 255); 
	unsigned int b = CLAMP(luaL_checkint(L, 3), 0, 255);
	unsigned int a = 255;
	if(lua_gettop(L) == 4){
		a = CLAMP(luaL_checkint(L, 4), 0, 255);
	}
	if(r<16){
		r = 16;
	}
	if(g<16){
		g = 16;
	}
	if(b<16){
		b = 16;
	}
	if(a<16){
		a = 16;
	}
	char buffer [50];
  	sprintf (buffer, "0x%x%x%x%x", a,b,g,r);
	lua_pushstring(L, buffer);
	return 1;
}
unsigned int tocolor(lua_State *L,int a)
{
	char *colorstr = luaL_checkstring(L, a);
	unsigned int color;
	sscanf (colorstr,"%x",&color);
	return color;
}
static const luaL_Reg color[] = {
  {"new",   new},
  {NULL, NULL}
};
void color_init(lua_State *L) {
	luaL_register(L, "Color", color);
}