naev 0.10.4
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_new( lua_State *L );
24static int colL_alpha( lua_State *L );
25static int colL_rgb( lua_State *L );
26static int colL_hsv( lua_State *L );
27static int colL_setrgb( lua_State *L );
28static int colL_sethsv( lua_State *L );
29static int colL_setalpha( lua_State *L );
30static int colL_linearToGamma( lua_State *L );
31static int colL_gammaToLinear( lua_State *L );
32static const luaL_Reg colL_methods[] = {
33 { "__eq", colL_eq },
34 { "new", colL_new },
35 { "alpha", colL_alpha },
36 { "rgb", colL_rgb },
37 { "hsv", colL_hsv },
38 { "setRGB", colL_setrgb },
39 { "setHSV", colL_sethsv },
40 { "setAlpha", colL_setalpha },
41 { "linearToGamma", colL_linearToGamma },
42 { "gammaToLinear", colL_gammaToLinear },
43 {0,0}
44};
52int nlua_loadCol( nlua_env env )
53{
54 nlua_register(env, COL_METATABLE, colL_methods, 1);
55 return 0;
56}
57
78glColour* lua_tocolour( lua_State *L, int ind )
79{
80 return (glColour*) lua_touserdata(L,ind);
81}
89glColour* luaL_checkcolour( lua_State *L, int ind )
90{
91 if (lua_iscolour(L,ind))
92 return lua_tocolour(L,ind);
93 luaL_typerror(L, ind, COL_METATABLE);
94 return NULL;
95}
103glColour* lua_pushcolour( lua_State *L, glColour colour )
104{
105 glColour *c = (glColour*) lua_newuserdata(L, sizeof(glColour));
106 *c = colour;
107 luaL_getmetatable(L, COL_METATABLE);
108 lua_setmetatable(L, -2);
109 return c;
110}
118int lua_iscolour( lua_State *L, int ind )
119{
120 int ret;
121
122 if (lua_getmetatable(L,ind)==0)
123 return 0;
124 lua_getfield(L, LUA_REGISTRYINDEX, COL_METATABLE);
125
126 ret = 0;
127 if (lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
128 ret = 1;
129
130 lua_pop(L, 2); /* remove both metatables */
131 return ret;
132}
133
142static int colL_eq( lua_State *L )
143{
144 glColour *c1, *c2;
145 c1 = luaL_checkcolour(L,1);
146 c2 = luaL_checkcolour(L,2);
147 lua_pushboolean( L, (memcmp( c1, c2, sizeof(glColour) )==0) );
148 return 1;
149}
150
167static int colL_new( lua_State *L )
168{
169 glColour col;
170 const glColour *col2;
171
172
173 if (lua_gettop(L)==0) {
174 col.r = col.g = col.b = col.a = 1.;
175 }
176 else if (lua_isnumber(L,1)) {
177 col.r = gammaToLinear(luaL_checknumber(L,1));
178 col.g = gammaToLinear(luaL_checknumber(L,2));
179 col.b = gammaToLinear(luaL_checknumber(L,3));
180 col.a = luaL_optnumber(L,4,1.);
181 }
182 else if (lua_isstring(L,1)) {
183 col2 = col_fromName( lua_tostring(L,1) );
184 if (col2 == NULL) {
185 NLUA_ERROR( L, _("Colour '%s' does not exist!"), lua_tostring(L,1) );
186 return 0;
187 }
188 col = *col2;
189 col.a = luaL_optnumber(L,2,1.);
190 }
191 else if (lua_iscolour(L,1))
192 col = *lua_tocolour(L,1);
193 else
194 NLUA_INVALID_PARAMETER(L);
195
196 lua_pushcolour( L, col );
197 return 1;
198}
199
211static int colL_alpha( lua_State *L )
212{
213 glColour *col = luaL_checkcolour(L,1);
214 lua_pushnumber( L, col->a );
215 return 1;
216}
217
232static int colL_rgb( lua_State *L )
233{
234 glColour *col = luaL_checkcolour(L,1);
235 if (lua_toboolean(L,2)) {
236 lua_pushnumber( L, linearToGamma( col->r ) );
237 lua_pushnumber( L, linearToGamma( col->g ) );
238 lua_pushnumber( L, linearToGamma( col->b ) );
239 }
240 else {
241 lua_pushnumber( L, col->r );
242 lua_pushnumber( L, col->g );
243 lua_pushnumber( L, col->b );
244 }
245 return 3;
246}
247
262static int colL_hsv( lua_State *L )
263{
264 float h, s, v, r, g, b;
265 glColour *col = luaL_checkcolour(L,1);
266 if (lua_toboolean(L,2)) {
267 r = linearToGamma( col->r );
268 g = linearToGamma( col->g );
269 b = linearToGamma( col->b );
270 }
271 else {
272 r = col->r;
273 g = col->g;
274 b = col->b;
275 }
276 col_rgb2hsv( &h, &s, &v, r, g, b );
277 lua_pushnumber( L, h );
278 lua_pushnumber( L, s );
279 lua_pushnumber( L, v );
280 return 3;
281}
282
296static int colL_setrgb( lua_State *L )
297{
298 glColour *col = luaL_checkcolour(L,1);
299 col->r = luaL_checknumber(L,2);
300 col->g = luaL_checknumber(L,3);
301 col->b = luaL_checknumber(L,4);
302 return 0;
303}
304
318static int colL_sethsv( lua_State *L )
319{
320 float h, s, v;
321 glColour *col = luaL_checkcolour(L,1);
322 h = luaL_checknumber(L,2);
323 s = luaL_checknumber(L,3);
324 v = luaL_checknumber(L,4);
325 col_hsv2rgb( col, h, s, v );
326 return 0;
327}
328
340static int colL_setalpha( lua_State *L )
341{
342 glColour *col = luaL_checkcolour(L,1);
343 col->a = luaL_checknumber(L,2);
344 return 0;
345}
346
353static int colL_linearToGamma( lua_State *L )
354{
355 glColour *col = luaL_checkcolour(L,1);
356 glColour out;
357 out.r = linearToGamma( col->r );
358 out.g = linearToGamma( col->g );
359 out.b = linearToGamma( col->b );
360 out.a = col->a;
361 lua_pushcolour(L,out);
362 return 1;
363}
364
371static int colL_gammaToLinear( lua_State *L )
372{
373 glColour *col = luaL_checkcolour(L,1);
374 glColour out;
375 out.r = gammaToLinear( col->r );
376 out.g = gammaToLinear( col->g );
377 out.b = gammaToLinear( col->b );
378 out.a = col->a;
379 lua_pushcolour(L,out);
380 return 1;
381}
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.
Definition: nlua_colour.c:167
int lua_iscolour(lua_State *L, int ind)
Checks to see if ind is a colour.
Definition: nlua_colour.c:118
static int colL_rgb(lua_State *L)
Gets the RGB values of a colour.
Definition: nlua_colour.c:232
static int colL_setrgb(lua_State *L)
Sets the colours values from the RGB colourspace.
Definition: nlua_colour.c:296
static int colL_eq(lua_State *L)
Compares two colours to see if they are the same.
Definition: nlua_colour.c:142
static int colL_hsv(lua_State *L)
Gets the HSV values of a colour.
Definition: nlua_colour.c:262
int nlua_loadCol(nlua_env env)
Loads the colour library.
Definition: nlua_colour.c:52
glColour * lua_tocolour(lua_State *L, int ind)
Lua bindings to interact with colours.
Definition: nlua_colour.c:78
glColour * lua_pushcolour(lua_State *L, glColour colour)
Pushes a colour on the stack.
Definition: nlua_colour.c:103
static int colL_sethsv(lua_State *L)
Sets the colours values from the HSV colourspace.
Definition: nlua_colour.c:318
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:89
static const luaL_Reg colL_methods[]
Definition: nlua_colour.c:32
static int colL_linearToGamma(lua_State *L)
Converts a colour from linear to gamma corrected.
Definition: nlua_colour.c:353
static int colL_setalpha(lua_State *L)
Sets the alpha of a colour.
Definition: nlua_colour.c:340
static int colL_gammaToLinear(lua_State *L)
Converts a colour from gamma corrected to linear.
Definition: nlua_colour.c:371
static int colL_alpha(lua_State *L)
Gets the alpha of a colour.
Definition: nlua_colour.c:211
static const double c[]
Definition: rng.c:264
static const double b[]
Definition: rng.c:256