naev 0.11.5
nlua_colour.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
11#include <lauxlib.h>
12
13#include "naev.h"
16#include "nlua_colour.h"
17
18#include "log.h"
19#include "nluadef.h"
20
21/* Colour metatable methods. */
22static int colL_eq( lua_State *L );
23static int colL_tostring( lua_State * L );
24static int colL_new( lua_State *L );
25static int colL_alpha( lua_State *L );
26static int colL_rgb( lua_State *L );
27static int colL_hsv( lua_State *L );
28static int colL_setrgb( lua_State *L );
29static int colL_sethsv( lua_State *L );
30static int colL_setalpha( lua_State *L );
31static int colL_linearToGamma( lua_State *L );
32static int colL_gammaToLinear( lua_State *L );
33static const luaL_Reg colL_methods[] = {
34 { "__eq", colL_eq },
35 { "__tostring", colL_tostring },
36 { "new", colL_new },
37 { "alpha", colL_alpha },
38 { "rgb", colL_rgb },
39 { "hsv", colL_hsv },
40 { "setRGB", colL_setrgb },
41 { "setHSV", colL_sethsv },
42 { "setAlpha", colL_setalpha },
43 { "linearToGamma", colL_linearToGamma },
44 { "gammaToLinear", colL_gammaToLinear },
45 {0,0}
46};
54int nlua_loadCol( nlua_env env )
55{
56 nlua_register(env, COL_METATABLE, colL_methods, 1);
57 return 0;
58}
59
80glColour* lua_tocolour( lua_State *L, int ind )
81{
82 return (glColour*) lua_touserdata(L,ind);
83}
91glColour* luaL_checkcolour( lua_State *L, int ind )
92{
93 if (lua_iscolour(L,ind))
94 return lua_tocolour(L,ind);
95 luaL_typerror(L, ind, COL_METATABLE);
96 return NULL;
97}
105glColour* lua_pushcolour( lua_State *L, glColour colour )
106{
107 glColour *c = (glColour*) lua_newuserdata(L, sizeof(glColour));
108 *c = colour;
109 luaL_getmetatable(L, COL_METATABLE);
110 lua_setmetatable(L, -2);
111 return c;
112}
120int lua_iscolour( lua_State *L, int ind )
121{
122 int ret;
123
124 if (lua_getmetatable(L,ind)==0)
125 return 0;
126 lua_getfield(L, LUA_REGISTRYINDEX, COL_METATABLE);
127
128 ret = 0;
129 if (lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
130 ret = 1;
131
132 lua_pop(L, 2); /* remove both metatables */
133 return ret;
134}
135
144static int colL_eq( lua_State *L )
145{
146 glColour *c1, *c2;
147 c1 = luaL_checkcolour(L,1);
148 c2 = luaL_checkcolour(L,2);
149 lua_pushboolean( L, (memcmp( c1, c2, sizeof(glColour) )==0) );
150 return 1;
151}
152
160static int colL_tostring( lua_State * L )
161{
162 const glColour *col = luaL_checkcolour(L,1);
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 );
166 return 1;
167}
168
185static int colL_new( lua_State *L )
186{
187 glColour col;
188 const glColour *col2;
189
190 if (lua_gettop(L)==0) {
191 col.r = col.g = col.b = col.a = 1.;
192 }
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.);
198 }
199 else if (lua_isstring(L,1)) {
200 col2 = col_fromName( lua_tostring(L,1) );
201 if (col2 == NULL) {
202 return NLUA_ERROR( L, _("Colour '%s' does not exist!"), lua_tostring(L,1) );
203 return 0;
204 }
205 col = *col2;
206 col.a = luaL_optnumber(L,2,1.);
207 }
208 else if (lua_iscolour(L,1))
209 col = *lua_tocolour(L,1);
210 else
211 NLUA_INVALID_PARAMETER(L,1);
212
213 lua_pushcolour( L, col );
214 return 1;
215}
216
228static int colL_alpha( lua_State *L )
229{
230 glColour *col = luaL_checkcolour(L,1);
231 lua_pushnumber( L, col->a );
232 return 1;
233}
234
249static int colL_rgb( lua_State *L )
250{
251 glColour *col = luaL_checkcolour(L,1);
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 ) );
256 }
257 else {
258 lua_pushnumber( L, col->r );
259 lua_pushnumber( L, col->g );
260 lua_pushnumber( L, col->b );
261 }
262 return 3;
263}
264
279static int colL_hsv( lua_State *L )
280{
281 float h, s, v, r, g, b;
282 const glColour *col = luaL_checkcolour(L,1);
283 if (lua_toboolean(L,2)) {
284 r = linearToGamma( col->r );
285 g = linearToGamma( col->g );
286 b = linearToGamma( col->b );
287 }
288 else {
289 r = col->r;
290 g = col->g;
291 b = col->b;
292 }
293 col_rgb2hsv( &h, &s, &v, r, g, b );
294 lua_pushnumber( L, h );
295 lua_pushnumber( L, s );
296 lua_pushnumber( L, v );
297 return 3;
298}
299
313static int colL_setrgb( lua_State *L )
314{
315 glColour *col = luaL_checkcolour(L,1);
316 col->r = luaL_checknumber(L,2);
317 col->g = luaL_checknumber(L,3);
318 col->b = luaL_checknumber(L,4);
319 return 0;
320}
321
335static int colL_sethsv( lua_State *L )
336{
337 float h, s, v;
338 glColour *col = luaL_checkcolour(L,1);
339 h = luaL_checknumber(L,2);
340 s = luaL_checknumber(L,3);
341 v = luaL_checknumber(L,4);
342 col_hsv2rgb( col, h, s, v );
343 return 0;
344}
345
357static int colL_setalpha( lua_State *L )
358{
359 glColour *col = luaL_checkcolour(L,1);
360 col->a = luaL_checknumber(L,2);
361 return 0;
362}
363
370static int colL_linearToGamma( lua_State *L )
371{
372 const glColour *col = luaL_checkcolour(L,1);
373 glColour out;
374 out.r = linearToGamma( col->r );
375 out.g = linearToGamma( col->g );
376 out.b = linearToGamma( col->b );
377 out.a = col->a;
378 lua_pushcolour(L,out);
379 return 1;
380}
381
388static int colL_gammaToLinear( lua_State *L )
389{
390 const glColour *col = luaL_checkcolour(L,1);
391 glColour out;
392 out.r = gammaToLinear( col->r );
393 out.g = gammaToLinear( col->g );
394 out.b = gammaToLinear( col->b );
395 out.a = col->a;
396 lua_pushcolour(L,out);
397 return 1;
398}
void col_rgb2hsv(float *H, float *S, float *V, float R, float G, float B)
Changes colour space from RGB to HSV.
Definition colour.c:111
void col_hsv2rgb(glColour *c, float h, float s, float v)
Changes colour space from HSV to RGB.
Definition colour.c:65
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.
Definition nlua_colour.c:54
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.
Definition nlua_colour.c:80
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.
Definition nlua_colour.c:91
static const luaL_Reg colL_methods[]
Definition nlua_colour.c:33
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.
static const double c[]
Definition rng.c:264