fix: dim would not get applied when lazy loading plugin (#27)

This commit is contained in:
Toni Müller
2023-07-05 08:07:30 +02:00
committed by GitHub
parent 915c46b0f5
commit cb3b5f57ae
3 changed files with 69 additions and 45 deletions
+44 -11
View File
@@ -1,4 +1,15 @@
--- This file is deprecated and should be removed in the future.
--- It is still in use but the functionality does not belong in the scope of this plugin
local o = vim.o
local api = vim.api
local BLACK = "#000000"
local WHITE = "#ffffff"
local auto_save_hl_group = "MsgArea"
local M = {}
---@param hex_str string hexadecimal value of a color
local hex_to_rgb = function(hex_str)
local hex = "[abcdef0-9][abcdef0-9]"
@@ -11,7 +22,10 @@ local hex_to_rgb = function(hex_str)
return { tonumber(red, 16), tonumber(green, 16), tonumber(blue, 16) }
end
function M.highlight(group, color, force)
--- @param group string
--- @param color table
--- @param force? boolean
local function highlight(group, color, force)
if color.link then
vim.api.nvim_set_hl(0, group, {
link = color.link,
@@ -31,7 +45,7 @@ function M.highlight(group, color, force)
end
end
function M.get_hl(name)
local function get_hl(name)
local ok, hl = pcall(vim.api.nvim_get_hl_by_name, name, true)
if not ok then
return
@@ -44,27 +58,46 @@ function M.get_hl(name)
return hl
end
---@param fg string forecrust color
---@param fg string foreground color
---@param bg string background color
---@param alpha number number between 0 and 1. 0 results in bg, 1 results in fg
function M.blend(fg, bg, alpha)
bg = hex_to_rgb(bg)
fg = hex_to_rgb(fg)
local function blend(fg, bg, alpha)
local bg_hex = hex_to_rgb(bg)
local fg_hex = hex_to_rgb(fg)
local blendChannel = function(i)
local ret = (alpha * fg[i] + ((1 - alpha) * bg[i]))
local ret = (alpha * fg_hex[i] + ((1 - alpha) * bg_hex[i]))
return math.floor(math.min(math.max(0, ret), 255) + 0.5)
end
return string.format("#%02X%02X%02X", blendChannel(1), blendChannel(2), blendChannel(3))
end
function M.darken(hex, amount, bg)
return M.blend(hex, bg or M.bg, math.abs(amount))
--- This function is still in use, but should be removed in the future.
--- The dimming should be done by the colorscheme or an UI Plugin.
--- @deprecated
--- @param dim_value number
M.apply_colors = function(dim_value)
if dim_value > 0 then
MSG_AREA = get_hl("MsgArea")
if MSG_AREA.foreground ~= nil then
MSG_AREA.background = (MSG_AREA.background or get_hl("Normal")["background"])
local foreground = (
o.background == "dark" and blend(MSG_AREA.background or BLACK, MSG_AREA.foreground or BLACK, dim_value)
or blend(MSG_AREA.background or WHITE, MSG_AREA.foreground or WHITE, dim_value)
)
highlight("AutoSaveText", { fg = foreground })
auto_save_hl_group = "AutoSaveText"
end
end
end
function M.lighten(hex, amount, fg)
return M.blend(hex, fg or M.fg, math.abs(amount))
--- @deprecated
--- @see M.apply_colors
--- @param message string
M.echo_with_highlight = function(message)
api.nvim_echo({ { message, auto_save_hl_group } }, true, {})
end
return M