···
1
1
-
local hlutils = require("sense.utils.hlutils")
1
1
+
local M = {}
2
2
3
3
-
local M = {}
3
3
+
local function dec_to_rgb(num)
4
4
+
return {
5
5
+
r = bit.rshift(num, 16),
6
6
+
g = bit.band(bit.rshift(num, 8), 0xFF),
7
7
+
b = bit.band(num, 0xFF),
8
8
+
}
9
9
+
end
4
10
5
5
-
local function fill_bg(name)
6
6
-
local hl = hlutils.read_hl(0, { name = name, link = false })
7
7
-
assert(hl)
8
8
-
assert(hl.fg)
9
9
-
if not hl.bg then
10
10
-
hl.bg = hlutils.tint(hl.fg, -0.8)
11
11
+
local function rgb_to_dec(rgb)
12
12
+
return bit.lshift(rgb.r, 16) + bit.lshift(rgb.g, 8) + rgb.b
13
13
+
end
14
14
+
15
15
+
---@param ns_id integer
16
16
+
---@param opts vim.api.keyset.get_highlight
17
17
+
local function read_hl(ns_id, opts)
18
18
+
opts.link = false
19
19
+
local hl = vim.api.nvim_get_hl(ns_id, opts)
20
20
+
local rich_hl = {}
21
21
+
if vim.tbl_isempty(hl) then
22
22
+
return
11
23
end
12
12
-
return hl
24
24
+
rich_hl.fg = hl.fg and dec_to_rgb(hl.fg)
25
25
+
rich_hl.bg = hl.bg and dec_to_rgb(hl.bg)
26
26
+
return rich_hl
27
27
+
end
28
28
+
29
29
+
---@param c table A hex color
30
30
+
---@param percent number a negative number darkens and a positive one brightens
31
31
+
local function tint(c, percent)
32
32
+
if not c.r or not c.g or not c.b then
33
33
+
return "NONE"
34
34
+
end
35
35
+
local blend = function(component)
36
36
+
component = math.floor(component * (1 + percent))
37
37
+
return math.min(math.max(component, 0), 255)
38
38
+
end
39
39
+
return {
40
40
+
r = blend(c.r),
41
41
+
g = blend(c.g),
42
42
+
b = blend(c.b),
43
43
+
}
44
44
+
end
45
45
+
46
46
+
local function set(name, from)
47
47
+
local hl = read_hl(0, { name = from, link = false })
48
48
+
if hl and hl.fg and not hl.bg then
49
49
+
hl.bg = rgb_to_dec(tint(hl.fg, -0.8))
50
50
+
hl.fg = hl.fg and rgb_to_dec(hl.fg)
51
51
+
else
52
52
+
hl = { link = from }
53
53
+
end
54
54
+
vim.api.nvim_set_hl(0, name, hl)
13
55
end
14
56
15
57
function M.setup()
16
58
-- generate hl-groups based on existing colors
17
17
-
hlutils.set_hl("SenseVirtualTextError", fill_bg("DiagnosticVirtualTextError"))
18
18
-
hlutils.set_hl("SenseVirtualTextWarn", fill_bg("DiagnosticVirtualTextWarn"))
19
19
-
hlutils.set_hl("SenseVirtualTextInfo", fill_bg("DiagnosticVirtualTextInfo"))
20
20
-
hlutils.set_hl("SenseVirtualTextHint", fill_bg("DiagnosticVirtualTextHint"))
21
21
-
hlutils.set_hl("SenseStatusColError", fill_bg("DiagnosticVirtualTextError"))
22
22
-
hlutils.set_hl("SenseStatusColWarn", fill_bg("DiagnosticVirtualTextWarn"))
23
23
-
hlutils.set_hl("SenseStatusColInfo", fill_bg("DiagnosticVirtualTextInfo"))
24
24
-
hlutils.set_hl("SenseStatusColHint", fill_bg("DiagnosticVirtualTextHint"))
59
59
+
set("SenseVirtualTextError", "DiagnosticVirtualTextError")
60
60
+
set("SenseVirtualTextWarn", "DiagnosticVirtualTextWarn")
61
61
+
set("SenseVirtualTextInfo", "DiagnosticVirtualTextInfo")
62
62
+
set("SenseVirtualTextHint", "DiagnosticVirtualTextHint")
63
63
+
set("SenseStatusColError", "DiagnosticVirtualTextError")
64
64
+
set("SenseStatusColWarn", "DiagnosticVirtualTextWarn")
65
65
+
set("SenseStatusColInfo", "DiagnosticVirtualTextInfo")
66
66
+
set("SenseStatusColHint", "DiagnosticVirtualTextHint")
25
67
end
26
68
27
69
return M
···
1
1
-
local bit = require("bit")
2
2
-
3
3
-
local M = {}
4
4
-
5
5
-
function M.dec_to_rgb(num)
6
6
-
return {
7
7
-
r = bit.rshift(num, 16),
8
8
-
g = bit.band(bit.rshift(num, 8), 0xFF),
9
9
-
b = bit.band(num, 0xFF),
10
10
-
}
11
11
-
end
12
12
-
13
13
-
function M.rgb_to_dec(rgb)
14
14
-
return bit.lshift(rgb.r, 16) + bit.lshift(rgb.g, 8) + rgb.b
15
15
-
end
16
16
-
17
17
-
---@param ns_id integer
18
18
-
---@param opts vim.api.keyset.get_highlight
19
19
-
function M.read_hl(ns_id, opts)
20
20
-
local hl = vim.api.nvim_get_hl(ns_id, opts)
21
21
-
local rich_hl = {}
22
22
-
if vim.tbl_isempty(hl) then
23
23
-
return nil
24
24
-
end
25
25
-
-- if hl.link then
26
26
-
-- return M.read_hl(ns_id, { name = hl.link })
27
27
-
-- end
28
28
-
rich_hl.fg = hl.fg and M.dec_to_rgb(hl.fg)
29
29
-
rich_hl.bg = hl.bg and M.dec_to_rgb(hl.bg)
30
30
-
return rich_hl
31
31
-
end
32
32
-
33
33
-
---@param c table A hex color
34
34
-
---@param percent number a negative number darkens and a positive one brightens
35
35
-
function M.tint(c, percent)
36
36
-
if not c.r or not c.g or not c.b then
37
37
-
return "NONE"
38
38
-
end
39
39
-
local blend = function(component)
40
40
-
component = math.floor(component * (1 + percent))
41
41
-
return math.min(math.max(component, 0), 255)
42
42
-
end
43
43
-
return {
44
44
-
r = blend(c.r),
45
45
-
g = blend(c.g),
46
46
-
b = blend(c.b),
47
47
-
}
48
48
-
end
49
49
-
50
50
-
function M.set_hl(name, hl)
51
51
-
vim.api.nvim_set_hl(0, name, {
52
52
-
fg = M.rgb_to_dec(hl.fg),
53
53
-
bg = M.rgb_to_dec(hl.bg),
54
54
-
})
55
55
-
end
56
56
-
57
57
-
return M