aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md1
-rw-r--r--plugins/colorpreview.lua49
2 files changed, 50 insertions, 0 deletions
diff --git a/README.md b/README.md
index 244f6c5..b50dfc4 100644
--- a/README.md
+++ b/README.md
@@ -12,6 +12,7 @@ Plugin | Description
-------|-----------------------------------------
[`autowrap`](plugins/autowrap.lua?raw=1) | Automatically hardwraps lines when typing
[`bracketmatch`](plugins/bracketmatch.lua?raw=1) | Underlines matching right-bracket of left-bracket under caret *([screenshot](https://user-images.githubusercontent.com/3920290/80132745-0c863f00-8594-11ea-8875-c455c6fd7eae.png))*
+[`colorpreview`](plugins/colorpreview.lua?raw=1) | 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))*
[`drawwhitespace`](plugins/drawwhitespace.lua?raw=1) | Draws tabs and spaces *([screenshot](https://user-images.githubusercontent.com/3920290/80573013-22ae5800-89f7-11ea-9895-6362a1c0abc7.png))*
[`eval`](plugins/eval.lua?raw=1) | Replaces selected Lua code with its evaluated result
[`gofmt`](plugins/gofmt.lua?raw=1) | Auto-formats the current go file, adds the missing imports and the missing return cases
diff --git a/plugins/colorpreview.lua b/plugins/colorpreview.lua
new file mode 100644
index 0000000..8095f4e
--- /dev/null
+++ b/plugins/colorpreview.lua
@@ -0,0 +1,49 @@
+local common = require "core.common"
+local DocView = require "core.docview"
+
+
+local white = { common.color "#ffffff" }
+local black = { common.color "#000000" }
+local tmp = {}
+
+
+local function draw_color_previews(self, idx, x, y, ptn, base)
+ local text = self.doc.lines[idx]
+ local s, e = 0, 0
+
+ while true do
+ s, e = text:find(ptn, e + 1)
+ if not s then break end
+
+ local str = text:sub(s, e)
+ local r, g, b = str:match(ptn)
+ r, g, b = tonumber(r, base), tonumber(g, base), tonumber(b, base)
+
+ local x1 = x + self:get_col_x_offset(idx, s)
+ local x2 = x + self:get_col_x_offset(idx, e + 1)
+ local oy = self:get_line_text_y_offset()
+
+ local text_color = math.max(r, g, b) < 128 and white or black
+ tmp[1], tmp[2], tmp[3], tmp[4] = r, g, b, nil
+
+ local l1, _, l2, _ = self.doc:get_selection(true)
+
+ if not (self.doc:has_selection() and idx >= l1 and idx <= l2) then
+ renderer.draw_rect(x1, y, x2 - x1, self:get_line_height(), tmp)
+ renderer.draw_text(self:get_font(), str, x1, y + oy, text_color)
+ else
+ tmp[4] = 58
+ renderer.draw_rect(x1, y, x2 - x1, self:get_line_height(), tmp)
+ end
+ end
+end
+
+
+local draw_line_text = DocView.draw_line_text
+
+function DocView:draw_line_text(idx, x, y)
+ draw_line_text(self, idx, x, y)
+ draw_color_previews(self, idx, x, y, "#(%x%x)(%x%x)(%x%x)%f[%X]", 16)
+ draw_color_previews(self, idx, x, y, "rgba?%((%d+)%D+(%d+)%D+(%d+).-%)", 10)
+end
+