From ddb8fe4609ddf2c77e6791db2826d663da479564 Mon Sep 17 00:00:00 2001 From: Neo Xu Date: Sun, 4 Aug 2024 16:08:36 +0800 Subject: [PATCH] feat(palette): add palette API Usage: lvgl.palette.main(lvgl.palette.BLUE_GREY) Fix #35 Signed-off-by: Neo Xu --- examples/examples.lua | 3 ++ src/luavgl.c | 3 +- src/luavgl.h | 5 ++++ src/palette.c | 68 +++++++++++++++++++++++++++++++++++++++++++ src/util.c | 8 ++++- 5 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 src/palette.c diff --git a/examples/examples.lua b/examples/examples.lua index 952a90b..bfbc4e6 100644 --- a/examples/examples.lua +++ b/examples/examples.lua @@ -11,6 +11,9 @@ local container = lvgl.Object { } } +-- change color directly using property +container.bg_color = lvgl.palette.main(lvgl.palette.BLUE_GREY) + print("created container", container) local function createBtn(parent, name) diff --git a/src/luavgl.c b/src/luavgl.c index 32eb760..db59040 100644 --- a/src/luavgl.c +++ b/src/luavgl.c @@ -9,6 +9,7 @@ #include "group.c" #include "indev.c" #include "obj.c" +#include "palette.c" #include "timer.c" #include "util.c" @@ -137,7 +138,7 @@ LUALIB_API int luaopen_lvgl(lua_State *L) luavgl_indev_init(L); luavgl_group_init(L); luavgl_disp_init(L); - + luavgl_palette_init(L); luavgl_constants_init(L); /* Methods to create widget locate in widgets table, check `luavgl_obj_init` diff --git a/src/luavgl.h b/src/luavgl.h index 652641f..199272a 100644 --- a/src/luavgl.h +++ b/src/luavgl.h @@ -232,6 +232,11 @@ LUALIB_API lv_property_t luavgl_toproperty(lua_State *L, int idx, */ LUALIB_API int luavgl_pushproperty(lua_State *L, const lv_property_t *prop); +/** + * @brief Push lvgl color to stack + */ +LUALIB_API int luavgl_pushcolor(lua_State *L, lv_color_t color); + /** * @brief Helper function to iterate through table * diff --git a/src/palette.c b/src/palette.c new file mode 100644 index 0000000..c3cd3f0 --- /dev/null +++ b/src/palette.c @@ -0,0 +1,68 @@ +#include "luavgl.h" +#include "private.h" +#include "rotable.h" + +static int luavgl_palette_main(lua_State *L) +{ + lv_palette_t p = luaL_checkinteger(L, 1); + + lv_color_t c = lv_palette_main(p); + luavgl_pushcolor(L, c); + return 1; +} + +static int luavgl_palette_lighten(lua_State *L) +{ + lv_palette_t p = luaL_checkinteger(L, 1); + uint8_t lvl = luaL_checkinteger(L, 2); + + lv_color_t c = lv_palette_lighten(p, lvl); + luavgl_pushcolor(L, c); + return 1; +} + +static int luavgl_palette_darken(lua_State *L) +{ + lv_palette_t p = luaL_checkinteger(L, 1); + uint8_t lvl = luaL_checkinteger(L, 2); + + lv_color_t c = lv_palette_darken(p, lvl); + luavgl_pushcolor(L, c); + return 1; +} + +static const rotable_Reg palette_constants[] = { + {"RED", 0, {.integer = LV_PALETTE_RED} }, + {"PINK", 0, {.integer = LV_PALETTE_PINK} }, + {"PURPLE", 0, {.integer = LV_PALETTE_PURPLE} }, + {"DEEP_PURPLE", 0, {.integer = LV_PALETTE_DEEP_PURPLE}}, + {"INDIGO", 0, {.integer = LV_PALETTE_INDIGO} }, + {"BLUE", 0, {.integer = LV_PALETTE_BLUE} }, + {"LIGHT_BLUE", 0, {.integer = LV_PALETTE_LIGHT_BLUE} }, + {"CYAN", 0, {.integer = LV_PALETTE_CYAN} }, + {"TEAL", 0, {.integer = LV_PALETTE_TEAL} }, + {"GREEN", 0, {.integer = LV_PALETTE_GREEN} }, + {"LIGHT_GREEN", 0, {.integer = LV_PALETTE_LIGHT_GREEN}}, + {"LIME", 0, {.integer = LV_PALETTE_LIME} }, + {"YELLOW", 0, {.integer = LV_PALETTE_YELLOW} }, + {"AMBER", 0, {.integer = LV_PALETTE_AMBER} }, + {"ORANGE", 0, {.integer = LV_PALETTE_ORANGE} }, + {"DEEP_ORANGE", 0, {.integer = LV_PALETTE_DEEP_ORANGE}}, + {"BROWN", 0, {.integer = LV_PALETTE_BROWN} }, + {"BLUE_GREY", 0, {.integer = LV_PALETTE_BLUE_GREY} }, + {"GREY", 0, {.integer = LV_PALETTE_GREY} }, + {"LAST", 0, {.integer = LV_PALETTE_LAST} }, + {"NONE", 0, {.integer = LV_PALETTE_NONE} }, + + {"main", LUA_TFUNCTION, {luavgl_palette_main} }, + {"lighten", LUA_TFUNCTION, {luavgl_palette_lighten} }, + {"darken", LUA_TFUNCTION, {luavgl_palette_darken} }, + + {0, 0, {0} }, +}; + +static void luavgl_palette_init(lua_State *L) +{ + rotable_newlib(L, palette_constants); + lua_setfield(L, -2, "palette"); +} diff --git a/src/util.c b/src/util.c index ed72a0e..f40b5d3 100644 --- a/src/util.c +++ b/src/util.c @@ -431,6 +431,12 @@ LUALIB_API lv_property_t luavgl_toproperty(lua_State *L, int idx, return prop; } +LUALIB_API int luavgl_pushcolor(lua_State *L, lv_color_t color) +{ + lua_pushinteger(L, lv_color_to_int(color)); + return 1; +} + LUALIB_API int luavgl_pushproperty(lua_State *L, const lv_property_t *prop) { lv_prop_id_t id = prop->id; @@ -444,7 +450,7 @@ LUALIB_API int luavgl_pushproperty(lua_State *L, const lv_property_t *prop) lua_pushnumber(L, prop->precise); return 1; case LV_PROPERTY_TYPE_COLOR: - lua_pushinteger(L, lv_color_to_int(prop->color)); + luavgl_pushcolor(L, prop->color); return 1; case LV_PROPERTY_TYPE_POINT: /* table of {x, y} */