Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(palette): add palette API #58

Merged
merged 1 commit into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/examples.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/luavgl.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "group.c"
#include "indev.c"
#include "obj.c"
#include "palette.c"
#include "timer.c"
#include "util.c"

Expand Down Expand Up @@ -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`
Expand Down
5 changes: 5 additions & 0 deletions src/luavgl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
68 changes: 68 additions & 0 deletions src/palette.c
Original file line number Diff line number Diff line change
@@ -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");
}
8 changes: 7 additions & 1 deletion src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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} */
Expand Down