aboutsummaryrefslogtreecommitdiff
path: root/.config/vis/plugins/vis-colorizer
diff options
context:
space:
mode:
authordacctal <dacctal@symlinx.net>2026-04-27 05:30:46 +0000
committerdacctal <dacctal@symlinx.net>2026-04-27 05:30:46 +0000
commitd33d907f53c50d323eca75c4bfc02ab5b989b30a (patch)
tree6888a2074338a723f0fde99b03b09fbab91a265f /.config/vis/plugins/vis-colorizer
parent89505535c652cd6f31a15df73293c6e90eaa852f (diff)
added bare pluginsHEADmaster
Diffstat (limited to '.config/vis/plugins/vis-colorizer')
-rw-r--r--.config/vis/plugins/vis-colorizer/LICENSE21
-rw-r--r--.config/vis/plugins/vis-colorizer/README.md17
-rw-r--r--.config/vis/plugins/vis-colorizer/init.lua100
-rw-r--r--.config/vis/plugins/vis-colorizer/screenshot.pngbin0 -> 37646 bytes
4 files changed, 138 insertions, 0 deletions
diff --git a/.config/vis/plugins/vis-colorizer/LICENSE b/.config/vis/plugins/vis-colorizer/LICENSE
new file mode 100644
index 0000000..77a589d
--- /dev/null
+++ b/.config/vis/plugins/vis-colorizer/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2023 Thim Cederlund
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/.config/vis/plugins/vis-colorizer/README.md b/.config/vis/plugins/vis-colorizer/README.md
new file mode 100644
index 0000000..89ca09f
--- /dev/null
+++ b/.config/vis/plugins/vis-colorizer/README.md
@@ -0,0 +1,17 @@
+# vis-colorizer
+Highlights hex colors. Created for the [vis editor](https://github.com/martanne/vis).
+
+Inspired by [chrisbra/Colorizer](https://github.com/chrisbra/Colorizer).
+
+![image](https://github.com/thimc/vis-colorizer/blob/main/screenshot.png)
+
+## Usage
+
+Clone the repo to `~/.config/vis/plugins/`.
+
+Append the following line to your `visrc.lua`:
+
+ local colorizer = require('plugins/vis-colorizer')
+ colorizer.three = false -- (optional) diables three digit hex colors
+ colorizer.six = true -- (enabled by default) six digit hex colors
+
diff --git a/.config/vis/plugins/vis-colorizer/init.lua b/.config/vis/plugins/vis-colorizer/init.lua
new file mode 100644
index 0000000..9fa3a6a
--- /dev/null
+++ b/.config/vis/plugins/vis-colorizer/init.lua
@@ -0,0 +1,100 @@
+local M = {
+ text_colors = {
+ dark = "#000000",
+ light = "#ffffff"
+ },
+ -- Highlight six digit hex color codes
+ six = true,
+ -- Highlight three digit hex color hexcodes
+ three = false,
+}
+
+local styleIdStack = {}
+
+M.styleIdIterator = function()
+ local i = 0
+ local MAX_STYLE_ID = 64 -- UI_STYLE_LEXER_MAX = 64
+ return function()
+ i = i + 1
+ if i <= MAX_STYLE_ID then
+ return i
+ end
+ return nil
+ end
+end
+
+M.initStyleIds = function()
+ styleIdStack = {}
+ for i in M.styleIdIterator() do
+ table.insert(styleIdStack, i)
+ end
+end
+
+M.initStyleIds()
+
+M.extract_hex_colors = function(input_string)
+ local pattern = "#([0-9a-fA-F]+)"
+ local matches = {}
+
+ local init = 1
+ local match_end = 0
+ while true do
+ match_start, match_end, hex = string.find(input_string, pattern, init + match_end)
+ -- TODO: add better validation
+ if match_start == nil or hex == nil then
+ break
+ end
+ if (hex:len() == 3 and M.three) or (hex:len() == 6 and M.six) then
+ table.insert(matches, {
+ starts = match_start,
+ ends = match_end,
+ hex = hex,
+ })
+ end
+ end
+
+ return matches
+end
+
+M.draw = function(win, colors)
+ local offset = win.viewport['bytes'].start
+ for i, color in ipairs(colors) do
+ local fg = M.text_colors.light
+
+ if color.hex:len() == 3 then
+ color.hex = string.gsub(color.hex,
+ "([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])",
+ "%1%1%2%2%3%3")
+ end
+
+ local r = tonumber(color.hex:sub(1, 2), 16)
+ local g = tonumber(color.hex:sub(3, 4), 16)
+ local b = tonumber(color.hex:sub(5, 6), 16)
+
+ if (r * 30) + (g * 59) + (b * 11) > 12000 then
+ fg = M.text_colors.dark
+ end
+
+ local id = table.remove(styleIdStack)
+
+ local style = "fore:" .. fg .. ",back:#" .. color.hex
+ if not win:style_define(id, style) then
+ break
+ end
+ win:style(id, color.starts - 1 + offset, color.ends - 1 + offset)
+ table.insert(styleIdStack, id)
+ if color.ends >= win.viewport['bytes'].finish then
+ break
+ end
+ end
+end
+
+M.on_higlight = function(win)
+ local content = win.file:content(win.viewport['bytes'])
+ local hex_colors = M.extract_hex_colors(content)
+ M.draw(win, hex_colors)
+end
+
+vis.events.subscribe(vis.events.WIN_HIGHLIGHT, M.on_higlight)
+
+return M
diff --git a/.config/vis/plugins/vis-colorizer/screenshot.png b/.config/vis/plugins/vis-colorizer/screenshot.png
new file mode 100644
index 0000000..718f08d
--- /dev/null
+++ b/.config/vis/plugins/vis-colorizer/screenshot.png
Binary files differ