farout.nvim/lua/tokyonight/util.lua

205 lines
4.9 KiB
Lua
Raw Normal View History

local ts = require("tokyonight.treesitter")
local M = {}
M.bg = "#000000"
M.fg = "#ffffff"
M.day_brightness = 0.3
2021-04-19 12:39:35 +00:00
2022-09-05 14:55:26 +00:00
---@param c string
local function hexToRgb(c)
c = string.lower(c)
return { tonumber(c:sub(2, 3), 16), tonumber(c:sub(4, 5), 16), tonumber(c:sub(6, 7), 16) }
2021-04-19 12:39:35 +00:00
end
---@param foreground string foreground color
---@param background string background color
---@param alpha number|string number between 0 and 1. 0 results in bg, 1 results in fg
function M.blend(foreground, background, alpha)
alpha = type(alpha) == "string" and (tonumber(alpha, 16) / 0xff) or alpha
local bg = hexToRgb(background)
local fg = hexToRgb(foreground)
2021-04-17 19:22:55 +00:00
2021-04-19 18:43:20 +00:00
local blendChannel = function(i)
2021-05-12 09:19:28 +00:00
local ret = (alpha * fg[i] + ((1 - alpha) * bg[i]))
return math.floor(math.min(math.max(0, ret), 255) + 0.5)
2021-04-17 19:22:55 +00:00
end
2022-09-05 14:55:26 +00:00
return string.format("#%02x%02x%02x", blendChannel(1), blendChannel(2), blendChannel(3))
2021-04-17 19:22:55 +00:00
end
function M.darken(hex, amount, bg)
return M.blend(hex, bg or M.bg, amount)
2021-05-12 09:19:28 +00:00
end
function M.lighten(hex, amount, fg)
return M.blend(hex, fg or M.fg, amount)
end
function M.invert_color(color)
local hsluv = require("tokyonight.hsluv")
2021-04-24 20:24:40 +00:00
if color ~= "NONE" then
2021-05-09 15:45:05 +00:00
local hsl = hsluv.hex_to_hsluv(color)
hsl[3] = 100 - hsl[3]
2021-05-12 09:19:28 +00:00
if hsl[3] < 40 then
hsl[3] = hsl[3] + (100 - hsl[3]) * M.day_brightness
2021-05-12 09:19:28 +00:00
end
2021-05-09 15:45:05 +00:00
return hsluv.hsluv_to_hex(hsl)
end
return color
end
---@param group string
function M.highlight(group, hl)
group = ts.get(group)
if not group then
return
end
if hl.style then
if type(hl.style) == "table" then
hl = vim.tbl_extend("force", hl, hl.style)
elseif hl.style:lower() ~= "none" then
-- handle old string style definitions
for s in string.gmatch(hl.style, "([^,]+)") do
hl[s] = true
end
2021-04-19 12:39:35 +00:00
end
hl.style = nil
2021-04-19 12:39:35 +00:00
end
vim.api.nvim_set_hl(0, group, hl)
2021-04-17 19:22:55 +00:00
end
--- Delete the autocmds when the theme changes to something else
function M.onColorScheme()
vim.cmd([[autocmd! TokyoNight]])
vim.cmd([[augroup! TokyoNight]])
end
2021-04-20 10:19:20 +00:00
---@param config Config
function M.autocmds(config)
2021-05-12 09:19:28 +00:00
vim.cmd([[augroup TokyoNight]])
vim.cmd([[ autocmd!]])
vim.cmd([[ autocmd ColorSchemePre * lua require("tokyonight.util").onColorScheme()]])
vim.cmd(
[[ autocmd FileType ]]
.. table.concat(config.sidebars, ",")
.. [[ setlocal winhighlight=Normal:NormalSB,SignColumn:SignColumnSB]]
)
if vim.tbl_contains(config.sidebars, "terminal") then
vim.cmd([[ autocmd TermOpen * setlocal winhighlight=Normal:NormalSB,SignColumn:SignColumnSB]])
2021-04-20 10:19:20 +00:00
end
2021-05-12 09:19:28 +00:00
vim.cmd([[augroup end]])
2021-04-20 10:19:20 +00:00
end
-- Simple string interpolation.
--
-- Example template: "${name} is ${value}"
--
---@param str string template string
---@param table table key value pairs to replace in the string
function M.template(str, table)
2022-11-10 17:11:09 +00:00
return (
str:gsub("($%b{})", function(w)
return vim.tbl_get(table, unpack(vim.split(w:sub(3, -2), ".", { plain = true }))) or w
end)
)
2021-04-20 10:19:20 +00:00
end
function M.syntax(syntax)
2021-05-12 09:19:28 +00:00
for group, colors in pairs(syntax) do
M.highlight(group, colors)
2021-05-12 09:19:28 +00:00
end
end
2021-04-17 19:22:55 +00:00
---@param colors ColorScheme
function M.terminal(colors)
2021-04-20 09:58:45 +00:00
-- dark
vim.g.terminal_color_0 = colors.black
2021-04-20 09:58:45 +00:00
vim.g.terminal_color_8 = colors.terminal_black
-- light
vim.g.terminal_color_7 = colors.fg_dark
vim.g.terminal_color_15 = colors.fg
-- colors
2021-04-17 19:22:55 +00:00
vim.g.terminal_color_1 = colors.red
vim.g.terminal_color_9 = colors.red
2021-04-20 09:58:45 +00:00
vim.g.terminal_color_2 = colors.green
2021-04-17 19:22:55 +00:00
vim.g.terminal_color_10 = colors.green
2021-04-20 09:58:45 +00:00
vim.g.terminal_color_3 = colors.yellow
vim.g.terminal_color_11 = colors.yellow
vim.g.terminal_color_4 = colors.blue
2021-04-17 19:22:55 +00:00
vim.g.terminal_color_12 = colors.blue
2021-04-20 09:58:45 +00:00
vim.g.terminal_color_5 = colors.magenta
2021-04-17 19:22:55 +00:00
vim.g.terminal_color_13 = colors.magenta
2021-04-20 09:58:45 +00:00
vim.g.terminal_color_6 = colors.cyan
2021-04-17 19:22:55 +00:00
vim.g.terminal_color_14 = colors.cyan
end
---@param colors ColorScheme
function M.invert_colors(colors)
2021-05-12 09:19:28 +00:00
if type(colors) == "string" then
---@diagnostic disable-next-line: return-type-mismatch
return M.invert_color(colors)
2021-05-12 09:19:28 +00:00
end
for key, value in pairs(colors) do
2022-11-11 06:23:27 +00:00
colors[key] = M.invert_colors(value)
end
2022-11-11 06:23:27 +00:00
return colors
end
---@param hls Highlights
function M.invert_highlights(hls)
for _, hl in pairs(hls) do
if hl.fg then
hl.fg = M.invert_color(hl.fg)
end
if hl.bg then
hl.bg = M.invert_color(hl.bg)
end
if hl.sp then
hl.sp = M.invert_color(hl.sp)
end
2021-05-12 09:19:28 +00:00
end
2021-05-02 08:43:18 +00:00
end
2021-04-17 19:22:55 +00:00
---@param theme Theme
function M.load(theme)
-- only needed to clear when not the default colorscheme
if vim.g.colors_name then
vim.cmd("hi clear")
end
2021-04-17 19:22:55 +00:00
vim.o.termguicolors = true
vim.g.colors_name = "tokyonight"
if ts.new_style() then
for group, colors in pairs(ts.defaults) do
if not theme.highlights[group] then
M.highlight(group, colors)
end
end
end
M.syntax(theme.highlights)
-- vim.api.nvim_set_hl_ns(M.ns)
if theme.config.terminal_colors then
M.terminal(theme.colors)
end
M.autocmds(theme.config)
vim.defer_fn(function()
M.syntax(theme.defer)
end, 100)
2021-04-17 19:22:55 +00:00
end
return M