Updated API to not include 'rgb' and on the lua side, to use [0.0, 1.0]

instead of [0, 255].
This commit is contained in:
Bruce Hill 2020-04-19 00:45:48 -07:00
parent bc1f39d12c
commit 36fc3d377d
4 changed files with 29 additions and 23 deletions

View File

@ -24,10 +24,10 @@ int main(void)
int r = (int)(255.0 * (0.5 + 0.5*sin(t*a + (double)(x) / 50.0)));
int g = (int)(255.0 * (0.5 + 0.5*sin(0.8 + t*b + (double)(x) / 50.0)));
int b = (int)(255.0 * (0.5 + 0.5*sin(1.3 + t*c + (double)(x) / 50.0)));
btui_set_bg_rgb(bt,
(r < 0 ? 0 : (r > 255 ? 255 : r)),
(g < 0 ? 0 : (g > 255 ? 255 : g)),
(b < 0 ? 0 : (b > 255 ? 255 : b)));
btui_set_bg(bt,
(r < 0 ? 0 : (r > 255 ? 255 : r)),
(g < 0 ? 0 : (g > 255 ? 255 : g)),
(b < 0 ? 0 : (b > 255 ? 255 : b)));
btui_puts(bt, " ");
}
btui_flush(bt);

View File

@ -149,13 +149,16 @@ static int Lbtui_withfg(lua_State *L)
if (bt == NULL) luaL_error(L, "Not a BTUI object");
if (lua_gettop(L) < 5) luaL_error(L, "Expected r,g,b values and a function");
int isnum;
int r = lua_tointegerx(L, 2, &isnum);
if (!isnum) luaL_error(L, "Expected integer r value");
int g = lua_tointegerx(L, 3, &isnum);
if (!isnum) luaL_error(L, "Expected integer g value");
int b = lua_tointegerx(L, 4, &isnum);
if (!isnum) luaL_error(L, "Expected integer b value");
btui_set_fg_rgb(*bt, r, g, b);
lua_Number r = lua_tonumberx(L, 2, &isnum);
if (!isnum) luaL_argerror(L, 2, "expected number for red value");
lua_Number g = lua_tonumberx(L, 3, &isnum);
if (!isnum) luaL_argerror(L, 3, "expected number for green value");
lua_Number b = lua_tonumberx(L, 4, &isnum);
if (!isnum) luaL_argerror(L, 4, "expected number for blue value");
btui_set_fg(*bt,
r < 0.0 ? 0 : (r > 1.0 ? 255 : (int)(255.0 * r)),
g < 0.0 ? 0 : (g > 1.0 ? 255 : (int)(255.0 * g)),
b < 0.0 ? 0 : (b > 1.0 ? 255 : (int)(255.0 * b)));
int top = lua_gettop(L);
int status = lua_pcall(L, 0, LUA_MULTRET, 0);
btui_set_attributes(*bt, BTUI_FG_NORMAL);
@ -170,13 +173,16 @@ static int Lbtui_withbg(lua_State *L)
if (bt == NULL) luaL_error(L, "Not a BTUI object");
if (lua_gettop(L) < 5) luaL_error(L, "Expected r,g,b values and a function");
int isnum;
int r = lua_tointegerx(L, 2, &isnum);
if (!isnum) luaL_error(L, "Expected integer r value");
int g = lua_tointegerx(L, 3, &isnum);
if (!isnum) luaL_error(L, "Expected integer g value");
int b = lua_tointegerx(L, 4, &isnum);
if (!isnum) luaL_error(L, "Expected integer b value");
btui_set_bg_rgb(*bt, r, g, b);
lua_Number r = lua_tonumberx(L, 2, &isnum);
if (!isnum) luaL_argerror(L, 2, "expected number for red value");
lua_Number g = lua_tonumberx(L, 3, &isnum);
if (!isnum) luaL_argerror(L, 3, "expected number for green value");
lua_Number b = lua_tonumberx(L, 4, &isnum);
if (!isnum) luaL_argerror(L, 4, "expected number for blue value");
btui_set_bg(*bt,
r < 0.0 ? 0 : (r > 1.0 ? 255 : (int)(255.0 * r)),
g < 0.0 ? 0 : (g > 1.0 ? 255 : (int)(255.0 * g)),
b < 0.0 ? 0 : (b > 1.0 ? 255 : (int)(255.0 * b)));
int top = lua_gettop(L);
int status = lua_pcall(L, 0, LUA_MULTRET, 0);
btui_set_attributes(*bt, BTUI_BG_NORMAL);

View File

@ -16,7 +16,7 @@ btui(function(bt)
bt:clear()
bt:move(x, y)
bt:withbg(150,225,80, function()
bt:withbg(.8,.95,.2, function()
bt:withfg(0,0,0, function()
bt:print(" Pressed: ", key, " ")
end)

8
btui.h
View File

@ -35,8 +35,8 @@ int btui_move_cursor(btui_t *bt, int x, int y);
char *btui_keyname(int key, char *buf);
int btui_keynamed(const char *name);
int btui_set_attributes(btui_t *bt, attr_t attrs);
int btui_set_fg_rgb(btui_t *bt, unsigned char r, unsigned char g, unsigned char b);
int btui_set_bg_rgb(btui_t *bt, unsigned char r, unsigned char g, unsigned char b);
int btui_set_fg(btui_t *bt, unsigned char r, unsigned char g, unsigned char b);
int btui_set_bg(btui_t *bt, unsigned char r, unsigned char g, unsigned char b);
int btui_set_fg_hex(btui_t *bt, int hex);
int btui_set_bg_hex(btui_t *bt, int hex);
#define btui_printf(bt, ...) fprintf((bt)->out, __VA_ARGS__)
@ -497,12 +497,12 @@ int btui_move_cursor(btui_t *bt, int x, int y)
return fprintf(bt->out, "\033[%d;%dH", y+1, x+1);
}
int btui_set_fg_rgb(btui_t *bt, unsigned char r, unsigned char g, unsigned char b)
int btui_set_fg(btui_t *bt, unsigned char r, unsigned char g, unsigned char b)
{
return fprintf(bt->out, "\033[38;2;%d;%d;%dm", r, g, b);
}
int btui_set_bg_rgb(btui_t *bt, unsigned char r, unsigned char g, unsigned char b)
int btui_set_bg(btui_t *bt, unsigned char r, unsigned char g, unsigned char b)
{
return fprintf(bt->out, "\033[48;2;%d;%d;%dm", r, g, b);
}