16#include "nlua_colour.h"
21static int colL_eq( lua_State *L );
89 return (glColour *)lua_touserdata( L, ind );
102 luaL_typerror( L, ind, COL_METATABLE );
114 glColour *
c = (glColour *)lua_newuserdata( L,
sizeof( glColour ) );
116 luaL_getmetatable( L, COL_METATABLE );
117 lua_setmetatable( L, -2 );
131 if ( lua_getmetatable( L, ind ) == 0 )
133 lua_getfield( L, LUA_REGISTRYINDEX, COL_METATABLE );
136 if ( lua_rawequal( L, -1, -2 ) )
153 const glColour *c1, *c2;
156 lua_pushboolean( L, ( memcmp( c1, c2,
sizeof( glColour ) ) == 0 ) );
170 char buf[STRMAX_SHORT];
171 snprintf( buf,
sizeof( buf ),
"Colour( %.2f, %.2f, %.2f, %.2f )", col->r,
172 col->g, col->b, col->a );
173 lua_pushstring( L, buf );
201 if ( lua_gettop( L ) == 0 ) {
202 col.r = col.g = col.b = col.a = 1.;
203 }
else if ( lua_isnumber( L, 1 ) ) {
204 if ( lua_toboolean( L, 5 ) ) {
205 col.r = luaL_checknumber( L, 1 );
206 col.g = luaL_checknumber( L, 2 );
207 col.b = luaL_checknumber( L, 3 );
209 col.r = gammaToLinear( luaL_checknumber( L, 1 ) );
210 col.g = gammaToLinear( luaL_checknumber( L, 2 ) );
211 col.b = gammaToLinear( luaL_checknumber( L, 3 ) );
213 col.a = luaL_optnumber( L, 4, 1. );
214 }
else if ( lua_isstring( L, 1 ) ) {
215 const glColour *col2 = col_fromName( lua_tostring( L, 1 ) );
217 return NLUA_ERROR( L, _(
"Colour '%s' does not exist!" ),
218 lua_tostring( L, 1 ) );
219 if ( lua_toboolean( L, 3 ) ) {
220 col.r = linearToGamma( col2->r );
221 col.g = linearToGamma( col2->g );
222 col.b = linearToGamma( col2->b );
225 col.a = luaL_optnumber( L, 2, 1. );
229 NLUA_INVALID_PARAMETER( L, 1 );
255 if ( lua_isnumber( L, 1 ) ) {
257 h = luaL_checknumber( L, 1 );
258 s = luaL_checknumber( L, 2 );
259 v = luaL_checknumber( L, 3 );
261 if ( !lua_toboolean( L, 5 ) ) {
262 col.r = gammaToLinear( col.r );
263 col.g = gammaToLinear( col.g );
264 col.b = gammaToLinear( col.b );
266 col.a = luaL_optnumber( L, 4, 1. );
268 NLUA_INVALID_PARAMETER( L, 1 );
288 lua_pushnumber( L, col->a );
310 if ( lua_toboolean( L, 2 ) ) {
311 lua_pushnumber( L, linearToGamma( col->r ) );
312 lua_pushnumber( L, linearToGamma( col->g ) );
313 lua_pushnumber( L, linearToGamma( col->b ) );
315 lua_pushnumber( L, col->r );
316 lua_pushnumber( L, col->g );
317 lua_pushnumber( L, col->b );
341 if ( lua_toboolean( L, 2 ) ) {
342 lua_pushnumber( L, linearToGamma( col->r ) );
343 lua_pushnumber( L, linearToGamma( col->g ) );
344 lua_pushnumber( L, linearToGamma( col->b ) );
346 lua_pushnumber( L, col->r );
347 lua_pushnumber( L, col->g );
348 lua_pushnumber( L, col->b );
350 lua_pushnumber( L, col->a );
371 float h, s, v, r, g, b;
373 if ( lua_toboolean( L, 2 ) ) {
374 r = linearToGamma( col->r );
375 g = linearToGamma( col->g );
376 b = linearToGamma( col->b );
383 lua_pushnumber( L, h );
384 lua_pushnumber( L, s );
385 lua_pushnumber( L, v );
405 col->r = luaL_checknumber( L, 2 );
406 col->g = luaL_checknumber( L, 3 );
407 col->b = luaL_checknumber( L, 4 );
428 h = luaL_checknumber( L, 2 );
429 s = luaL_checknumber( L, 3 );
430 v = luaL_checknumber( L, 4 );
449 col->a = luaL_checknumber( L, 2 );
463 out.r = linearToGamma( col->r );
464 out.g = linearToGamma( col->g );
465 out.b = linearToGamma( col->b );
481 out.r = gammaToLinear( col->r );
482 out.g = gammaToLinear( col->g );
483 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)
Creates a new colour. Colours are assumed to be in gamma colourspace by default and are converted to ...
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_newHSV(lua_State *L)
Creates a new colour from HSV values. Colours are assumed to be in gamma colourspace by default and a...
static int colL_setalpha(lua_State *L)
Sets the alpha of a colour.
static int colL_rgba(lua_State *L)
Gets the RGBA values 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.