build: selene + stylua
This commit is contained in:
@@ -27,20 +27,26 @@ function util.blend(fg, bg, alpha)
|
||||
fg = hexToRgb(fg)
|
||||
|
||||
local blendChannel = function(i)
|
||||
local ret = (alpha * (fg[i]) + ((1 - alpha) * (bg[i])))
|
||||
return math.floor(math.min(math.max(0, ret), 255) + .5)
|
||||
local ret = (alpha * fg[i] + ((1 - alpha) * bg[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 util.darken(hex, amount, bg) return util.blend(hex, bg or util.bg, math.abs(amount)) end
|
||||
function util.lighten(hex, amount, fg) return util.blend(hex, fg or util.fg, math.abs(amount)) end
|
||||
function util.darken(hex, amount, bg)
|
||||
return util.blend(hex, bg or util.bg, math.abs(amount))
|
||||
end
|
||||
function util.lighten(hex, amount, fg)
|
||||
return util.blend(hex, fg or util.fg, math.abs(amount))
|
||||
end
|
||||
|
||||
function util.brighten(color, percentage)
|
||||
local hsl = hsluv.hex_to_hsluv(color)
|
||||
local larpSpace = 100 - hsl[3]
|
||||
if percentage < 0 then larpSpace = hsl[3] end
|
||||
if percentage < 0 then
|
||||
larpSpace = hsl[3]
|
||||
end
|
||||
hsl[3] = hsl[3] + larpSpace * percentage
|
||||
return hsluv.hsluv_to_hex(hsl)
|
||||
end
|
||||
@@ -49,7 +55,9 @@ function util.invertColor(color)
|
||||
if color ~= "NONE" then
|
||||
local hsl = hsluv.hex_to_hsluv(color)
|
||||
hsl[3] = 100 - hsl[3]
|
||||
if hsl[3] < 40 then hsl[3] = hsl[3] + (100 - hsl[3]) * .3 end
|
||||
if hsl[3] < 40 then
|
||||
hsl[3] = hsl[3] + (100 - hsl[3]) * 0.3
|
||||
end
|
||||
return hsluv.hsluv_to_hex(hsl)
|
||||
end
|
||||
return color
|
||||
@@ -65,16 +73,26 @@ function util.randomColor(color)
|
||||
end
|
||||
|
||||
function util.getColor(color)
|
||||
if vim.o.background == "dark" then return color end
|
||||
if not util.colorCache[color] then util.colorCache[color] = util.invertColor(color) end
|
||||
if vim.o.background == "dark" then
|
||||
return color
|
||||
end
|
||||
if not util.colorCache[color] then
|
||||
util.colorCache[color] = util.invertColor(color)
|
||||
end
|
||||
return util.colorCache[color]
|
||||
end
|
||||
|
||||
-- local ns = vim.api.nvim_create_namespace("tokyonight")
|
||||
function util.highlight(group, color)
|
||||
if color.fg then util.colorsUsed[color.fg] = true end
|
||||
if color.bg then util.colorsUsed[color.bg] = true end
|
||||
if color.sp then util.colorsUsed[color.sp] = true end
|
||||
if color.fg then
|
||||
util.colorsUsed[color.fg] = true
|
||||
end
|
||||
if color.bg then
|
||||
util.colorsUsed[color.bg] = true
|
||||
end
|
||||
if color.sp then
|
||||
util.colorsUsed[color.sp] = true
|
||||
end
|
||||
|
||||
local style = color.style and "gui=" .. color.style or "gui=NONE"
|
||||
local fg = color.fg and "guifg=" .. util.getColor(color.fg) or "guifg=NONE"
|
||||
@@ -103,7 +121,9 @@ function util.debug(colors)
|
||||
if type(color) == "table" then
|
||||
util.debug(color)
|
||||
else
|
||||
if util.colorsUsed[color] == nil then print("not used: " .. name .. " : " .. color) end
|
||||
if util.colorsUsed[color] == nil then
|
||||
print("not used: " .. name .. " : " .. color)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -111,28 +131,27 @@ end
|
||||
--- Delete the autocmds when the theme changes to something else
|
||||
function util.onColorScheme()
|
||||
if vim.g.colors_name ~= "tokyonight" then
|
||||
vim.cmd [[autocmd! TokyoNight]]
|
||||
vim.cmd [[augroup! TokyoNight]]
|
||||
vim.cmd([[autocmd! TokyoNight]])
|
||||
vim.cmd([[augroup! TokyoNight]])
|
||||
end
|
||||
end
|
||||
|
||||
---@param config Config
|
||||
function util.autocmds(config)
|
||||
vim.cmd [[augroup TokyoNight]]
|
||||
vim.cmd [[ autocmd!]]
|
||||
vim.cmd [[ autocmd ColorScheme * lua require("tokyonight.util").onColorScheme()]]
|
||||
vim.cmd([[augroup TokyoNight]])
|
||||
vim.cmd([[ autocmd!]])
|
||||
vim.cmd([[ autocmd ColorScheme * lua require("tokyonight.util").onColorScheme()]])
|
||||
if config.dev then
|
||||
vim.cmd [[ autocmd BufWritePost */lua/tokyonight/** nested colorscheme tokyonight]]
|
||||
vim.cmd([[ autocmd BufWritePost */lua/tokyonight/** nested colorscheme tokyonight]])
|
||||
end
|
||||
for _, sidebar in ipairs(config.sidebars) do
|
||||
if sidebar == "terminal" then
|
||||
vim.cmd [[ autocmd TermOpen * setlocal winhighlight=Normal:NormalSB,SignColumn:SignColumnSB]]
|
||||
vim.cmd([[ autocmd TermOpen * setlocal winhighlight=Normal:NormalSB,SignColumn:SignColumnSB]])
|
||||
else
|
||||
vim.cmd([[ autocmd FileType ]] .. sidebar ..
|
||||
[[ setlocal winhighlight=Normal:NormalSB,SignColumn:SignColumnSB]])
|
||||
vim.cmd([[ autocmd FileType ]] .. sidebar .. [[ setlocal winhighlight=Normal:NormalSB,SignColumn:SignColumnSB]])
|
||||
end
|
||||
end
|
||||
vim.cmd [[augroup end]]
|
||||
vim.cmd([[augroup end]])
|
||||
end
|
||||
|
||||
-- Simple string interpolation.
|
||||
@@ -142,10 +161,16 @@ end
|
||||
---@param str string template string
|
||||
---@param table table key value pairs to replace in the string
|
||||
function util.template(str, table)
|
||||
return (str:gsub("($%b{})", function(w) return table[w:sub(3, -2)] or w end))
|
||||
return (str:gsub("($%b{})", function(w)
|
||||
return table[w:sub(3, -2)] or w
|
||||
end))
|
||||
end
|
||||
|
||||
function util.syntax(syntax) for group, colors in pairs(syntax) do util.highlight(group, colors) end end
|
||||
function util.syntax(syntax)
|
||||
for group, colors in pairs(syntax) do
|
||||
util.highlight(group, colors)
|
||||
end
|
||||
end
|
||||
|
||||
---@param colors ColorScheme
|
||||
function util.terminal(colors)
|
||||
@@ -184,16 +209,22 @@ function util.terminal(colors)
|
||||
end
|
||||
|
||||
function util.light_colors(colors)
|
||||
if type(colors) == "string" then return util.getColor(colors) end
|
||||
if type(colors) == "string" then
|
||||
return util.getColor(colors)
|
||||
end
|
||||
local ret = {}
|
||||
for key, value in pairs(colors) do ret[key] = util.light_colors(value) end
|
||||
for key, value in pairs(colors) do
|
||||
ret[key] = util.light_colors(value)
|
||||
end
|
||||
return ret
|
||||
end
|
||||
|
||||
---@param theme Theme
|
||||
function util.load(theme)
|
||||
vim.cmd("hi clear")
|
||||
if vim.fn.exists("syntax_on") then vim.cmd("syntax reset") end
|
||||
if vim.fn.exists("syntax_on") then
|
||||
vim.cmd("syntax reset")
|
||||
end
|
||||
|
||||
vim.o.termguicolors = true
|
||||
vim.g.colors_name = "tokyonight"
|
||||
@@ -214,13 +245,17 @@ end
|
||||
function util.color_overrides(colors, config)
|
||||
if type(config.colors) == "table" then
|
||||
for key, value in pairs(config.colors) do
|
||||
if not colors[key] then error("Color " .. key .. " does not exist") end
|
||||
if not colors[key] then
|
||||
error("Color " .. key .. " does not exist")
|
||||
end
|
||||
if string.sub(value, 1, 1) == "#" then
|
||||
-- hex override
|
||||
colors[key] = value
|
||||
else
|
||||
-- another group
|
||||
if not colors[value] then error("Color " .. value .. " does not exist") end
|
||||
if not colors[value] then
|
||||
error("Color " .. value .. " does not exist")
|
||||
end
|
||||
colors[key] = colors[value]
|
||||
end
|
||||
end
|
||||
@@ -234,13 +269,17 @@ function util.light(brightness)
|
||||
if type(hl[key]) == "number" then
|
||||
local hex = string.format("#%06x", hl[key])
|
||||
local color = util.invertColor(hex)
|
||||
if brightness then color = util.brighten(hex, brightness) end
|
||||
if brightness then
|
||||
color = util.brighten(hex, brightness)
|
||||
end
|
||||
table.insert(def, "gui" .. def_key .. "=" .. color)
|
||||
end
|
||||
end
|
||||
if hl_name ~= "" and #def > 0 then
|
||||
for _, style in pairs({ "bold", "italic", "underline", "undercurl", "reverse" }) do
|
||||
if hl[style] then table.insert(def, "gui=" .. style) end
|
||||
if hl[style] then
|
||||
table.insert(def, "gui=" .. style)
|
||||
end
|
||||
end
|
||||
|
||||
vim.cmd("highlight! " .. hl_name .. " " .. table.concat(def, " "))
|
||||
@@ -262,7 +301,9 @@ function util.random()
|
||||
end
|
||||
if hl_name ~= "" and #def > 0 then
|
||||
for _, style in pairs({ "bold", "italic", "underline", "undercurl", "reverse" }) do
|
||||
if hl[style] then table.insert(def, "gui=" .. style) end
|
||||
if hl[style] then
|
||||
table.insert(def, "gui=" .. style)
|
||||
end
|
||||
end
|
||||
|
||||
vim.cmd("highlight! " .. hl_name .. " " .. table.concat(def, " "))
|
||||
@@ -271,4 +312,3 @@ function util.random()
|
||||
end
|
||||
|
||||
return util
|
||||
|
||||
|
||||
Reference in New Issue
Block a user