aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjgmdev <jgmdev@gmail.com>2023-01-12 21:57:59 -0400
committerjgmdev <jgmdev@gmail.com>2023-01-12 21:57:59 -0400
commitaebce8ccf05f2c27d21b475149278193b9d70d43 (patch)
treeb6d2fb4a4edab1692e6c9f1b2300f53047dde660
parent636a7044c7cc49c1bf458d30069d41b1502a3e14 (diff)
downloadlite-xl-plugins-aebce8ccf05f2c27d21b475149278193b9d70d43.tar.gz
lite-xl-plugins-aebce8ccf05f2c27d21b475149278193b9d70d43.zip
added colorpicker plugin
-rw-r--r--manifest.json11
-rw-r--r--plugins/colorpicker.lua56
2 files changed, 66 insertions, 1 deletions
diff --git a/manifest.json b/manifest.json
index 0f0e79b..f284381 100644
--- a/manifest.json
+++ b/manifest.json
@@ -71,6 +71,14 @@
"mod_version": "3"
},
{
+ "description": "Color picker dialog that supports html and rgb notations.",
+ "version": "0.1",
+ "path": "plugins/colorpicker.lua",
+ "id": "colorpicker",
+ "mod_version": "3",
+ "dependencies": { "widget": {} }
+ },
+ {
"description": "Underlays color values (eg. `#ff00ff` or `rgb(255, 0, 255)`) with their resultant color. *([screenshot](https://user-images.githubusercontent.com/3920290/80743752-731bd780-8b15-11ea-97d3-847db927c5dc.png))*",
"version": "0.1",
"path": "plugins/colorpreview.lua",
@@ -1063,7 +1071,8 @@
"version": "0.5",
"path": "plugins/settings.lua",
"id": "settings",
- "mod_version": "3"
+ "mod_version": "3",
+ "dependencies": { "widget": {} }
},
{
"description": "Displays the current time in the corner of the status view",
diff --git a/plugins/colorpicker.lua b/plugins/colorpicker.lua
new file mode 100644
index 0000000..c280fb6
--- /dev/null
+++ b/plugins/colorpicker.lua
@@ -0,0 +1,56 @@
+-- mod-version:3
+local command = require "core.command"
+local keymap = require "core.keymap"
+local ColorPickerDialog = require "libraries.widget.colorpickerdialog"
+
+---Get the color format of given text.
+---@param text string
+---@return "html" | "html_opacity" | "rgb"
+local function get_color_type(text)
+ local found = text:find("#%x%x%x%x%x%x%x?%x?")
+ if found then
+ found = text:find("#%x%x%x%x%x%x%x%x")
+ if found then return "html_opacity" end
+ return "html"
+ else
+ found = text:find("#%x%x%x")
+ if found then
+ return "html"
+ else
+ found = text:find(
+ "rgba?%((%d+)%D+(%d+)%D+(%d+)[%s,]-([%.%d]-)%s-%)"
+ )
+ if found then return "rgb" end
+ end
+ end
+ return "html"
+end
+
+command.add("core.docview!", {
+ ["color-picker:open"] = function(dv)
+ ---@type core.doc
+ local doc = dv.doc
+ local selection = doc:get_text(doc:get_selection())
+ local type = get_color_type(selection)
+
+ ---@type widget.colorpickerdialog
+ local picker = ColorPickerDialog(nil, selection)
+ function picker:on_apply(c)
+ local value
+ if type == "html" then
+ value = string.format("#%02X%02X%02X", c[1], c[2], c[3])
+ elseif type == "html_opacity" then
+ value = string.format("#%02X%02X%02X%02X", c[1], c[2], c[3], c[4])
+ elseif type == "rgb" then
+ value = string.format("rgba(%d, %d, %d, %.2f)", c[1], c[2], c[3], c[4]/255)
+ end
+ doc:text_input(value)
+ end
+ picker:show()
+ picker:centered()
+ end,
+})
+
+keymap.add {
+ ["ctrl+alt+k"] = "color-picker:open"
+}