aboutsummaryrefslogtreecommitdiff
path: root/plugins/colorpreview.lua
blob: 8095f4e5ba1020dd4719cbe59434d02db88362ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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