XuNeo / luavgl

lua + lvgl = luavgl An optimized lvgl Lua binding
MIT License
57 stars 13 forks source link

Access to lv palette colors #35

Open jonsmirl opened 3 months ago

jonsmirl commented 3 months ago

Introduce the problem

Would need these three API and some constants.

lv_palette_main(LVPALETTE...) lv_palette_lighten(LVPALETTE..., v) lv_palette_darken(LVPALETTE..., v)

Proposal

No response

jonsmirl commented 3 months ago

I put this into constants.c, not sure of your naming conventions.

  lua_pushcfunction(L, luavgl_palette);
  lua_setfield(L, -2, "Palette");

  lua_pushcfunction(L, luavgl_palette_light);
  lua_setfield(L, -2, "PaletteLight");

  lua_pushcfunction(L, luavgl_palette_dark);
  lua_setfield(L, -2, "PaletteDark");

static int luavgl_palette(lua_State *L)
{
  int palette = luaL_checkinteger(L, 1);

  int color = lv_color_to32(lv_palette_main(palette));

  lua_pushinteger(L, color);
  return 1;
}

static int luavgl_palette_light(lua_State *L)
{
  int palette = luaL_checkinteger(L, 1);
  int light = luaL_checkinteger(L, 2);

  int color = lv_color_to32(lv_palette_lighten(palette, light));

  lua_pushinteger(L, color);
  return 1;
}

static int luavgl_palette_dark(lua_State *L)
{
  int palette = luaL_checkinteger(L, 1);
  int dark = luaL_checkinteger(L, 2);

  int color = lv_color_to32(lv_palette_darken(palette, dark));

  lua_pushinteger(L, color);
  return 1;
}
XuNeo commented 3 months ago

Thank you. You can use luavgl_tocolor to convert other supported color format to lv_color_t, such as color in format of "#FFF".

not sure of your naming conventions.

I wanted to preserve name of the directly mapped APIs from lvgl C functions and convert those widget names to Widget. For constants, it's always uppercase.