Plugin for cycling through a color list

Plugin for cycling through a color list
master
Roland Lötscher 6 years ago committed by GitHub
parent a332c43ffb
commit 3fe8f59f57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 38
      plugins/ColorCycle/main.lua
  2. 15
      plugins/ColorCycle/plugin.ini
  3. 77
      src/plugin/luapi_application.h

@ -0,0 +1,38 @@
-- Register all Toolbar actions and intialize all UI stuff
function initUi()
app.registerUi({["menu"] = "Cycle through color list", ["callback"] = "cycle", ["accelerator"] = "<Alt>c"});
-- if you want to have multiple color lists you must use the app.registerUi function multiple times
-- with different callback functions and accelerators
end
-- Predefined colors copied from LoadHandlerHelper.cpp
-- modify to your needs
local colorList = {
{"black", 0x000000},
{"green", 0x008000},
{"lightblue", 0x00c0ff},
{"lightgreen", 0x00ff00},
{"blue", 0x3333cc},
{"gray", 0x808080},
{"red", 0xff0000},
{"magenta", 0xff00ff},
{"orange", 0xff8000},
{"yellow", 0xffff00},
{"white", 0xffffff}
}
-- start with first color
local currentColor = 0
function cycle()
if (currentColor < #colorList) then
currentColor = currentColor + 1
else
currentColor = 1
end
-- apply color to currently used tool and allow coloring of elements from selections
app.changeToolColor({["color"] = colorList[currentColor][2], ["selection"] = true})
-- use app.changeColor({["color"] = colorList[currentColor][2], ["tool"] = "pen""})
-- instead if you only want to change pen color
-- similarly if you want to change hilighter color or the color from another tool with color capabilities.
end

@ -0,0 +1,15 @@
[about]
## Author / Copyright notice
author=Roland Lötscher
description=Cycling through color list.
## If the plugin is packed with Xournal++, use
## <xournalpp> then it gets the same version number
version=<xournalpp>
[default]
enabled=false
[plugin]
mainfile=main.lua

@ -14,6 +14,7 @@
#include "control/Control.h"
#include "control/PageBackgroundChangeController.h"
#include "control/Tool.h"
#include "control/pagetype/PageTypeHandler.h"
#include "StringUtils.h"
@ -218,7 +219,7 @@ static int applib_uiActionSelected(lua_State* L) {
}
/**
* Select UI action
* Change page background
*/
static int applib_changeCurrentPageBackground(lua_State* L) {
PageType pt;
@ -233,6 +234,79 @@ static int applib_changeCurrentPageBackground(lua_State* L) {
return 1;
}
/**
* Change color of a specified tool or of the current tool
*/
static int applib_changeToolColor(lua_State* L) {
// discard any extra arguments passed in
lua_settop(L, 1);
luaL_checktype(L, 1, LUA_TTABLE);
lua_getfield(L, 1, "selection"); /* either true or false, for changing selection color
defaults to false*/
lua_getfield(L, 1, "tool"); /* "pen", "hilighter", "text"
"select_rect", "select_object", "select_region"
if omitted, current Tool is used */
lua_getfield(L, 1, "color"); // an RGB hex code defining the color
// stack now has following:
// 1 = {["color"] = 0xff00ff, ["tool"] = "PEN", ["selection"] = false}
// -3 = false
// -2 = "pen"
// -1 = 0xff0077
Plugin* plugin = Plugin::getPluginFromLua(L);
Control* ctrl = plugin->getControl();
ToolHandler* toolHandler = ctrl->getToolHandler();
bool selection = false;
if (lua_isboolean(L, -3)) {
selection = lua_toboolean(L, -3);
} else if (!lua_isnil(L, -3)) {
g_warning(""
"selection"
" key should be a boolean value (or nil)");
}
ToolType toolType = toolHandler->getToolType();
const char* toolStr = luaL_optstring(L, -2, nullptr);
if (toolStr != nullptr) {
toolType = toolTypeFromString(StringUtils::toLowerCase(toolStr));
}
if (toolType == TOOL_NONE) {
g_warning("tool \"%s\" is not valid or no tool has been selected", toolTypeToString(toolType).c_str());
lua_pop(L, 3);
return 0;
}
int color = 0x000000;
if (lua_isinteger(L, -1)) {
color = lua_tointeger(L, -1);
if (color < 0x000000 || color > 0xffffff) {
g_warning("Color 0x%x is no valid RGB color. ", color);
return 0;
}
} else if (!lua_isnil(L, -1)) {
g_warning(" "
"color"
" key should be an RGB hex code in the form 0xRRGGBB (or nil)");
}
Tool& tool = toolHandler->getTool(toolType);
if (tool.hasCapability(TOOL_CAP_COLOR)) {
tool.setColor(color);
ctrl->toolColorChanged(selection);
} else {
g_warning("tool \"%s\" has no color capability", toolTypeToString(toolType).c_str());
}
// Make sure to remove all vars which are put to the stack before!
lua_pop(L, 3);
return 1;
}
static const luaL_Reg applib[] = {{"msgbox", applib_msgbox},
{"registerUi", applib_registerUi},
@ -240,6 +314,7 @@ static const luaL_Reg applib[] = {{"msgbox", applib_msgbox},
{"uiActionSelected", applib_uiActionSelected},
{"changeCurrentPageBackground", applib_changeCurrentPageBackground},
{"saveAs", applib_saveAs},
{"changeToolColor", applib_changeToolColor},
// Placeholder
// {"MSG_BT_OK", nullptr},

Loading…
Cancel
Save