Root/
| Source at commit 03e16a6dc60ea94592416a9b5853931a86d3152c created 1 year 6 months ago. By Papymouge, Commit de depart. | |
|---|---|
| 1 | /* |
| 2 | * |
| 3 | */ |
| 4 | |
| 5 | #include <stdlib.h> |
| 6 | #include <stdio.h> |
| 7 | #include <math.h> |
| 8 | |
| 9 | #define ds_color_c |
| 10 | #define LUA_LIB |
| 11 | |
| 12 | #include "lua.h" |
| 13 | #include "lauxlib.h" |
| 14 | #include "lualib.h" |
| 15 | |
| 16 | #include <ulib/ulib.h> |
| 17 | |
| 18 | #include "vars.h" |
| 19 | |
| 20 | static int color_new(lua_State *L){ |
| 21 | int r = (int)luaL_checknumber(L, 1); |
| 22 | int g = (int)luaL_checknumber(L, 2); |
| 23 | int b = (int)luaL_checknumber(L, 3); |
| 24 | assert(L, r>=0 && r<=31, "Red mask must be between 0 and 31") |
| 25 | assert(L, g>=0 && g<=31, "Green mask must be between 0 and 31") |
| 26 | assert(L, b>=0 && b<=31, "Blue mask must be between 0 and 31") |
| 27 | lua_pushnumber(L, RGB15(r, g, b)); |
| 28 | return 1; |
| 29 | } |
| 30 | |
| 31 | static int color_new256(lua_State *L) { |
| 32 | int r = (int)luaL_checknumber(L, 1); |
| 33 | int g = (int)luaL_checknumber(L, 2); |
| 34 | int b = (int)luaL_checknumber(L, 3); |
| 35 | assert(L, r>=0 && r<=255, "Red mask must be between 0 and 255"); |
| 36 | assert(L, g>=0 && g<=255, "Green mask must be between 0 and 255"); |
| 37 | assert(L, b>=0 && b<=255, "Blue mask must be between 0 and 255"); |
| 38 | r = floor(r * 31 / 255); |
| 39 | g = floor(g * 31 / 255); |
| 40 | b = floor(b * 31 / 255); |
| 41 | lua_pushnumber(L, RGB15(r, g, b)); |
| 42 | return 1; |
| 43 | } |
| 44 | |
| 45 | static const luaL_Reg colorlib[] = { |
| 46 | {"new", color_new}, |
| 47 | {"new256", color_new256}, |
| 48 | {NULL, NULL} |
| 49 | }; |
| 50 | |
| 51 | /* |
| 52 | ** Open infos library |
| 53 | */ |
| 54 | LUALIB_API int luaopen_color(lua_State *L) { |
| 55 | luaL_register(L, LUA_COLORLIBNAME, colorlib); |
| 56 | return 1; |
| 57 | } |
| 58 | |
