color: move push color function to color.c

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2009-06-19 17:01:01 +02:00
parent 55524ece8d
commit df20e95f82
4 changed files with 50 additions and 48 deletions

46
color.c
View File

@ -288,4 +288,50 @@ xcolor_to_color(const xcolor_t *xcol, color_t *col)
return true;
}
/** Push a color as a string onto the stack
* \param L The Lua VM state.
* \param c The color to push.
* \return The number of elements pushed on stack.
*/
int
luaA_pushxcolor(lua_State *L, const xcolor_t *c)
{
uint8_t r = (unsigned)c->red * 0xff / 0xffff;
uint8_t g = (unsigned)c->green * 0xff / 0xffff;
uint8_t b = (unsigned)c->blue * 0xff / 0xffff;
uint8_t a = (unsigned)c->alpha * 0xff / 0xffff;
char s[10];
int len;
/* do not print alpha if it's full */
if(a == 0xff)
len = snprintf(s, sizeof(s), "#%02x%02x%02x", r, g, b);
else
len = snprintf(s, sizeof(s), "#%02x%02x%02x%02x", r, g, b, a);
lua_pushlstring(L, s, len);
return 1;
}
/** Push a color as a string onto the stack
* \param L The Lua VM state.
* \param c The color to push.
* \return The number of elements pushed on stack.
*/
int
luaA_pushcolor(lua_State *L, const color_t *c)
{
uint8_t r = c->red;
uint8_t g = c->green;
uint8_t b = c->blue;
uint8_t a = c->alpha;
char s[10];
int len;
/* do not print alpha if it's full */
if(a == 0xff)
len = snprintf(s, sizeof(s), "#%02x%02x%02x", r, g, b);
else
len = snprintf(s, sizeof(s), "#%02x%02x%02x%02x", r, g, b, a);
lua_pushlstring(L, s, len);
return 1;
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80

View File

@ -25,6 +25,7 @@
#include <xcb/xcb.h>
#include <stdbool.h>
#include <lua.h>
typedef struct
{
@ -75,6 +76,9 @@ bool xcolor_init_reply(xcolor_init_request_t);
bool xcolor_to_color(const xcolor_t *, color_t *);
int luaA_pushxcolor(lua_State *, const xcolor_t *);
int luaA_pushcolor(lua_State *, const color_t *);
#endif
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80

46
luaa.c
View File

@ -864,50 +864,4 @@ luaA_on_timer(EV_P_ ev_timer *w, int revents)
luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.timer, 0, 0);
}
/** Push a color as a string onto the stack
* \param L The Lua VM state.
* \param c The color to push.
* \return The number of elements pushed on stack.
*/
int
luaA_pushxcolor(lua_State *L, const xcolor_t *c)
{
uint8_t r = (unsigned)c->red * 0xff / 0xffff;
uint8_t g = (unsigned)c->green * 0xff / 0xffff;
uint8_t b = (unsigned)c->blue * 0xff / 0xffff;
uint8_t a = (unsigned)c->alpha * 0xff / 0xffff;
char s[10];
int len;
/* do not print alpha if it's full */
if(a == 0xff)
len = snprintf(s, sizeof(s), "#%02x%02x%02x", r, g, b);
else
len = snprintf(s, sizeof(s), "#%02x%02x%02x%02x", r, g, b, a);
lua_pushlstring(L, s, len);
return 1;
}
/** Push a color as a string onto the stack
* \param L The Lua VM state.
* \param c The color to push.
* \return The number of elements pushed on stack.
*/
int
luaA_pushcolor(lua_State *L, const color_t *c)
{
uint8_t r = c->red;
uint8_t g = c->green;
uint8_t b = c->blue;
uint8_t a = c->alpha;
char s[10];
int len;
/* do not print alpha if it's full */
if(a == 0xff)
len = snprintf(s, sizeof(s), "#%02x%02x%02x", r, g, b);
else
len = snprintf(s, sizeof(s), "#%02x%02x%02x%02x", r, g, b, a);
lua_pushlstring(L, s, len);
return 1;
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80

2
luaa.h
View File

@ -336,8 +336,6 @@ luaA_pushpadding(lua_State *L, padding_t *padding)
void luaA_init(xdgHandle *);
bool luaA_parserc(xdgHandle *, const char *, bool);
void luaA_on_timer(EV_P_ ev_timer *, int);
int luaA_pushxcolor(lua_State *, const xcolor_t *);
int luaA_pushcolor(lua_State *, const color_t *);
bool luaA_hasitem(lua_State *, const void *);
void luaA_table2wtable(lua_State *);
int luaA_next(lua_State *, int);