16#include "nlua_colour.h"
22static int colL_eq( lua_State *L );
82 return (glColour*) lua_touserdata(L,ind);
95 luaL_typerror(L, ind, COL_METATABLE);
107 glColour *
c = (glColour*) lua_newuserdata(L,
sizeof(glColour));
109 luaL_getmetatable(L, COL_METATABLE);
110 lua_setmetatable(L, -2);
124 if (lua_getmetatable(L,ind)==0)
126 lua_getfield(L, LUA_REGISTRYINDEX, COL_METATABLE);
129 if (lua_rawequal(L, -1, -2))
149 lua_pushboolean( L, (memcmp( c1, c2,
sizeof(glColour) )==0) );
163 char buf[STRMAX_SHORT];
164 snprintf( buf,
sizeof(buf),
"Colour( %.2f, %.2f, %.2f, %.2f )", col->r, col->g, col->b, col->a );
165 lua_pushstring( L, buf );
188 const glColour *col2;
190 if (lua_gettop(L)==0) {
191 col.r = col.g = col.b = col.a = 1.;
193 else if (lua_isnumber(L,1)) {
194 col.r = gammaToLinear(luaL_checknumber(L,1));
195 col.g = gammaToLinear(luaL_checknumber(L,2));
196 col.b = gammaToLinear(luaL_checknumber(L,3));
197 col.a = luaL_optnumber(L,4,1.);
199 else if (lua_isstring(L,1)) {
200 col2 = col_fromName( lua_tostring(L,1) );
202 return NLUA_ERROR( L, _(
"Colour '%s' does not exist!"), lua_tostring(L,1) );
206 col.a = luaL_optnumber(L,2,1.);
211 NLUA_INVALID_PARAMETER(L,1);
231 lua_pushnumber( L, col->a );
252 if (lua_toboolean(L,2)) {
253 lua_pushnumber( L, linearToGamma( col->r ) );
254 lua_pushnumber( L, linearToGamma( col->g ) );
255 lua_pushnumber( L, linearToGamma( col->b ) );
258 lua_pushnumber( L, col->r );
259 lua_pushnumber( L, col->g );
260 lua_pushnumber( L, col->b );
281 float h, s, v, r, g, b;
283 if (lua_toboolean(L,2)) {
284 r = linearToGamma( col->r );
285 g = linearToGamma( col->g );
286 b = linearToGamma( col->b );
294 lua_pushnumber( L, h );
295 lua_pushnumber( L, s );
296 lua_pushnumber( L, v );
316 col->r = luaL_checknumber(L,2);
317 col->g = luaL_checknumber(L,3);
318 col->b = luaL_checknumber(L,4);
339 h = luaL_checknumber(L,2);
340 s = luaL_checknumber(L,3);
341 v = luaL_checknumber(L,4);
360 col->a = luaL_checknumber(L,2);
374 out.r = linearToGamma( col->r );
375 out.g = linearToGamma( col->g );
376 out.b = linearToGamma( col->b );
392 out.r = gammaToLinear( col->r );
393 out.g = gammaToLinear( col->g );
394 out.b = gammaToLinear( col->b );
void col_rgb2hsv(float *H, float *S, float *V, float R, float G, float B)
Changes colour space from RGB to HSV.
void col_hsv2rgb(glColour *c, float h, float s, float v)
Changes colour space from HSV to RGB.
Header file with generic functions and naev-specifics.
static int colL_new(lua_State *L)
Gets a colour.
int lua_iscolour(lua_State *L, int ind)
Checks to see if ind is a colour.
static int colL_rgb(lua_State *L)
Gets the RGB values of a colour.
static int colL_setrgb(lua_State *L)
Sets the colours values from the RGB colourspace.
static int colL_eq(lua_State *L)
Compares two colours to see if they are the same.
static int colL_hsv(lua_State *L)
Gets the HSV values of a colour.
int nlua_loadCol(nlua_env env)
Loads the colour library.
static int colL_tostring(lua_State *L)
Converts a colour to a string.
glColour * lua_tocolour(lua_State *L, int ind)
Lua bindings to interact with colours.
glColour * lua_pushcolour(lua_State *L, glColour colour)
Pushes a colour on the stack.
static int colL_sethsv(lua_State *L)
Sets the colours values from the HSV colourspace.
glColour * luaL_checkcolour(lua_State *L, int ind)
Gets colour at index or raises error if there is no colour at index.
static const luaL_Reg colL_methods[]
static int colL_linearToGamma(lua_State *L)
Converts a colour from linear to gamma corrected.
static int colL_setalpha(lua_State *L)
Sets the alpha of a colour.
static int colL_gammaToLinear(lua_State *L)
Converts a colour from gamma corrected to linear.
static int colL_alpha(lua_State *L)
Gets the alpha of a colour.