2022-09-05 14:56:09 +00:00
|
|
|
local M = {}
|
2021-04-25 09:42:59 +00:00
|
|
|
|
2022-09-05 14:56:09 +00:00
|
|
|
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
|
|
|
|
|
2021-04-20 09:58:45 +00:00
|
|
|
---@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
|
2022-09-05 14:55:26 +00:00
|
|
|
function M.blend(fg, bg, alpha)
|
2021-04-19 12:39:35 +00:00
|
|
|
bg = hexToRgb(bg)
|
2021-04-19 18:43:20 +00:00
|
|
|
fg = hexToRgb(fg)
|
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
|
|
|
|
|
2022-09-05 14:56:09 +00:00
|
|
|
function M.darken(hex, amount, bg)
|
|
|
|
return M.blend(hex, bg or M.bg, math.abs(amount))
|
2021-05-12 09:19:28 +00:00
|
|
|
end
|
2022-09-05 14:56:09 +00:00
|
|
|
function M.lighten(hex, amount, fg)
|
|
|
|
return M.blend(hex, fg or M.fg, math.abs(amount))
|
2021-04-25 09:42:59 +00:00
|
|
|
end
|
|
|
|
|
2022-09-05 14:56:09 +00:00
|
|
|
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
|
2022-09-05 14:56:09 +00:00
|
|
|
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
|
|
|
|
|
2022-09-10 20:18:47 +00:00
|
|
|
-- new in 0.8 something
|
|
|
|
local has_new_group_names = vim.fn.hlexists("@comment") ~= 0
|
|
|
|
|
2022-09-10 19:28:27 +00:00
|
|
|
---@param group string
|
2022-09-05 14:56:09 +00:00
|
|
|
function M.highlight(group, color)
|
2022-09-10 20:18:47 +00:00
|
|
|
if group:sub(1, 1) == "@" and not has_new_group_names then
|
2022-09-10 19:28:27 +00:00
|
|
|
return
|
|
|
|
end
|
2022-09-05 14:56:09 +00:00
|
|
|
local hl = { fg = color.fg, bg = color.bg, sp = color.sp, link = color.link }
|
|
|
|
if color.style and color.style:lower() ~= "none" then
|
|
|
|
for s in string.gmatch(color.style, "([^,]+)") do
|
|
|
|
hl[s] = true
|
2021-04-19 12:39:35 +00:00
|
|
|
end
|
|
|
|
end
|
2022-09-05 14:56:09 +00:00
|
|
|
vim.api.nvim_set_hl(0, group, hl)
|
2021-04-17 19:22:55 +00:00
|
|
|
end
|
|
|
|
|
2021-04-20 21:57:15 +00:00
|
|
|
--- Delete the autocmds when the theme changes to something else
|
2022-09-05 14:56:09 +00:00
|
|
|
function M.onColorScheme()
|
2021-04-20 21:57:15 +00:00
|
|
|
if vim.g.colors_name ~= "tokyonight" then
|
2021-05-12 09:19:28 +00:00
|
|
|
vim.cmd([[autocmd! TokyoNight]])
|
|
|
|
vim.cmd([[augroup! TokyoNight]])
|
2021-04-20 21:57:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-20 10:19:20 +00:00
|
|
|
---@param config Config
|
2022-09-05 14:56:09 +00:00
|
|
|
function M.autocmds(config)
|
2021-05-12 09:19:28 +00:00
|
|
|
vim.cmd([[augroup TokyoNight]])
|
|
|
|
vim.cmd([[ autocmd!]])
|
|
|
|
vim.cmd([[ autocmd ColorScheme * lua require("tokyonight.util").onColorScheme()]])
|
2021-04-20 10:19:20 +00:00
|
|
|
for _, sidebar in ipairs(config.sidebars) do
|
|
|
|
if sidebar == "terminal" then
|
2021-05-12 09:19:28 +00:00
|
|
|
vim.cmd([[ autocmd TermOpen * setlocal winhighlight=Normal:NormalSB,SignColumn:SignColumnSB]])
|
2021-04-20 10:19:20 +00:00
|
|
|
else
|
2021-05-12 09:19:28 +00:00
|
|
|
vim.cmd([[ autocmd FileType ]] .. sidebar .. [[ setlocal winhighlight=Normal:NormalSB,SignColumn:SignColumnSB]])
|
2021-04-20 10:19:20 +00:00
|
|
|
end
|
|
|
|
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
|
2022-09-05 14:56:09 +00:00
|
|
|
function M.template(str, table)
|
2021-05-12 09:19:28 +00:00
|
|
|
return (str:gsub("($%b{})", function(w)
|
|
|
|
return table[w:sub(3, -2)] or w
|
|
|
|
end))
|
2021-04-20 10:19:20 +00:00
|
|
|
end
|
|
|
|
|
2022-09-05 14:56:09 +00:00
|
|
|
function M.syntax(syntax)
|
2021-05-12 09:19:28 +00:00
|
|
|
for group, colors in pairs(syntax) do
|
2022-09-05 14:56:09 +00:00
|
|
|
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
|
2022-09-05 14:56:09 +00:00
|
|
|
function M.terminal(colors)
|
2021-04-20 09:58:45 +00:00
|
|
|
-- dark
|
2021-05-02 08:16:56 +00:00
|
|
|
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
|
|
|
|
|
2022-09-05 10:16:26 +00:00
|
|
|
---@param colors ColorScheme
|
2022-09-05 14:56:09 +00:00
|
|
|
function M.invert_colors(colors)
|
2021-05-12 09:19:28 +00:00
|
|
|
if type(colors) == "string" then
|
2022-09-05 10:16:26 +00:00
|
|
|
---@diagnostic disable-next-line: return-type-mismatch
|
2022-09-05 14:56:09 +00:00
|
|
|
return M.invert_color(colors)
|
2021-05-12 09:19:28 +00:00
|
|
|
end
|
|
|
|
for key, value in pairs(colors) do
|
2022-09-05 14:56:09 +00:00
|
|
|
colors[key] = M.invert_colors(value)
|
2022-09-05 12:06:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param hls Highlights
|
2022-09-05 14:56:09 +00:00
|
|
|
function M.invert_highlights(hls)
|
2022-09-05 12:06:03 +00:00
|
|
|
for _, hl in pairs(hls) do
|
|
|
|
if hl.fg then
|
2022-09-05 14:56:09 +00:00
|
|
|
hl.fg = M.invert_color(hl.fg)
|
2022-09-05 12:06:03 +00:00
|
|
|
end
|
|
|
|
if hl.bg then
|
2022-09-05 14:56:09 +00:00
|
|
|
hl.bg = M.invert_color(hl.bg)
|
2022-09-05 12:06:03 +00:00
|
|
|
end
|
|
|
|
if hl.sp then
|
2022-09-05 14:56:09 +00:00
|
|
|
hl.sp = M.invert_color(hl.sp)
|
2022-09-05 12:06:03 +00:00
|
|
|
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
|
2022-09-05 14:56:09 +00:00
|
|
|
function M.load(theme)
|
2021-06-29 07:44:01 +00:00
|
|
|
-- only needed to clear when not the default colorscheme
|
|
|
|
if vim.g.colors_name then
|
|
|
|
vim.cmd("hi clear")
|
|
|
|
end
|
2021-04-25 13:50:02 +00:00
|
|
|
|
2021-04-17 19:22:55 +00:00
|
|
|
vim.o.termguicolors = true
|
|
|
|
vim.g.colors_name = "tokyonight"
|
2022-09-05 18:32:31 +00:00
|
|
|
|
|
|
|
M.syntax(theme.highlights)
|
2022-09-05 14:56:09 +00:00
|
|
|
|
|
|
|
-- vim.api.nvim_set_hl_ns(M.ns)
|
2022-09-05 18:32:31 +00:00
|
|
|
if theme.config.terminal_colors then
|
2022-09-05 14:56:09 +00:00
|
|
|
M.terminal(theme.colors)
|
2022-09-02 06:00:36 +00:00
|
|
|
end
|
|
|
|
|
2022-09-05 14:56:09 +00:00
|
|
|
M.autocmds(theme.config)
|
2021-08-05 11:51:06 +00:00
|
|
|
|
|
|
|
vim.defer_fn(function()
|
2022-09-05 14:56:09 +00:00
|
|
|
M.syntax(theme.defer)
|
2021-08-05 11:51:06 +00:00
|
|
|
end, 100)
|
2021-04-17 19:22:55 +00:00
|
|
|
end
|
|
|
|
|
2022-09-05 14:56:09 +00:00
|
|
|
return M
|