2021-04-25 09:42:59 +00:00
|
|
|
local hsluv = require("tokyonight.hsluv")
|
|
|
|
|
2021-04-17 19:22:55 +00:00
|
|
|
local util = {}
|
|
|
|
|
2021-04-19 12:39:35 +00:00
|
|
|
util.colorsUsed = {}
|
2021-04-24 20:24:40 +00:00
|
|
|
util.colorCache = {}
|
2021-04-19 12:39:35 +00:00
|
|
|
|
|
|
|
util.bg = "#000000"
|
|
|
|
util.fg = "#ffffff"
|
|
|
|
|
|
|
|
local function hexToRgb(hex_str)
|
2021-04-17 19:22:55 +00:00
|
|
|
local hex = "[abcdef0-9][abcdef0-9]"
|
|
|
|
local pat = "^#(" .. hex .. ")(" .. hex .. ")(" .. hex .. ")$"
|
|
|
|
hex_str = string.lower(hex_str)
|
|
|
|
|
|
|
|
assert(string.find(hex_str, pat) ~= nil, "hex_to_rgb: invalid hex_str: " .. tostring(hex_str))
|
|
|
|
|
|
|
|
local r, g, b = string.match(hex_str, pat)
|
2021-04-19 12:39:35 +00:00
|
|
|
return { tonumber(r, 16), tonumber(g, 16), tonumber(b, 16) }
|
|
|
|
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
|
|
|
|
function util.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)
|
|
|
|
local ret = (alpha * (fg[i]) + ((1 - alpha) * (bg[i])))
|
|
|
|
return math.floor(math.min(math.max(0, ret), 255) + .5)
|
2021-04-17 19:22:55 +00:00
|
|
|
end
|
|
|
|
|
2021-04-19 18:43:20 +00:00
|
|
|
return string.format("#%02X%02X%02X", blendChannel(1), blendChannel(2), blendChannel(3))
|
2021-04-17 19:22:55 +00:00
|
|
|
end
|
|
|
|
|
2021-04-20 10:19:20 +00:00
|
|
|
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
|
2021-04-17 19:22:55 +00:00
|
|
|
|
2021-04-25 09:42:59 +00:00
|
|
|
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
|
|
|
|
hsl[3] = hsl[3] + larpSpace * percentage
|
|
|
|
return hsluv.hsluv_to_hex(hsl)
|
|
|
|
end
|
|
|
|
|
2021-04-25 13:50:02 +00:00
|
|
|
function util.invertColor(color)
|
2021-04-24 20:24:40 +00:00
|
|
|
if color ~= "NONE" then
|
2021-04-25 09:42:59 +00:00
|
|
|
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
|
|
|
|
return hsluv.hsluv_to_hex(hsl)
|
|
|
|
end
|
2021-04-24 20:24:40 +00:00
|
|
|
end
|
|
|
|
return color
|
|
|
|
end
|
|
|
|
|
2021-04-25 13:50:02 +00:00
|
|
|
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
|
2021-04-24 20:24:40 +00:00
|
|
|
return util.colorCache[color]
|
|
|
|
end
|
|
|
|
|
2021-04-25 13:50:02 +00:00
|
|
|
function util.highlight(group, color)
|
2021-04-19 12:39:35 +00:00
|
|
|
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
|
|
|
|
|
2021-04-17 19:22:55 +00:00
|
|
|
local style = color.style and "gui=" .. color.style or "gui=NONE"
|
2021-04-25 13:50:02 +00:00
|
|
|
local fg = color.fg and "guifg=" .. util.getColor(color.fg) or "guifg=NONE"
|
|
|
|
local bg = color.bg and "guibg=" .. util.getColor(color.bg) or "guibg=NONE"
|
|
|
|
local sp = color.sp and "guisp=" .. util.getColor(color.sp) or ""
|
2021-04-23 06:48:26 +00:00
|
|
|
|
|
|
|
local hl = "highlight " .. group .. " " .. style .. " " .. fg .. " " .. bg .. " " .. sp
|
|
|
|
|
|
|
|
vim.cmd(hl)
|
2021-04-19 12:39:35 +00:00
|
|
|
if color.link then vim.cmd("highlight! link " .. group .. " " .. color.link) end
|
|
|
|
end
|
|
|
|
|
|
|
|
function util.debug(colors)
|
|
|
|
colors = colors or require("tokyonight.colors")
|
|
|
|
-- Dump unused colors
|
|
|
|
for name, color in pairs(colors) do
|
|
|
|
if type(color) == "table" then
|
|
|
|
util.debug(color)
|
|
|
|
else
|
|
|
|
if util.colorsUsed[color] == nil then print("not used: " .. name .. " : " .. color) end
|
|
|
|
end
|
|
|
|
end
|
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
|
|
|
|
function util.onColorScheme()
|
|
|
|
if vim.g.colors_name ~= "tokyonight" then
|
|
|
|
vim.cmd [[autocmd! TokyoNight]]
|
|
|
|
vim.cmd [[augroup! TokyoNight]]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-20 10:19:20 +00:00
|
|
|
---@param config Config
|
|
|
|
function util.autocmds(config)
|
|
|
|
vim.cmd [[augroup TokyoNight]]
|
|
|
|
vim.cmd [[ autocmd!]]
|
2021-04-20 21:57:15 +00:00
|
|
|
vim.cmd [[ autocmd ColorScheme * lua require("tokyonight.util").onColorScheme()]]
|
2021-04-20 10:19:20 +00:00
|
|
|
if config.dev then
|
|
|
|
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]]
|
|
|
|
else
|
2021-04-23 15:17:52 +00:00
|
|
|
vim.cmd([[ autocmd FileType ]] .. sidebar ..
|
2021-04-20 10:19:20 +00:00
|
|
|
[[ setlocal winhighlight=Normal:NormalSB,SignColumn:SignColumnSB]])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
vim.cmd [[augroup end]]
|
|
|
|
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 util.template(str, table)
|
|
|
|
return (str:gsub("($%b{})", function(w) return table[w:sub(3, -2)] or w end))
|
|
|
|
end
|
|
|
|
|
2021-04-25 13:50:02 +00:00
|
|
|
function util.syntax(syntax) for group, colors in pairs(syntax) do util.highlight(group, colors) end end
|
2021-04-17 19:22:55 +00:00
|
|
|
|
|
|
|
---@param colors ColorScheme
|
|
|
|
function util.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
|
2021-04-25 13:53:20 +00:00
|
|
|
|
|
|
|
if vim.o.background == "light" then
|
|
|
|
for i = 0, 15, 1 do
|
|
|
|
vim.g["terminal_color_" .. i] = util.getColor(vim.g["terminal_color_" .. i])
|
|
|
|
end
|
|
|
|
end
|
2021-04-17 19:22:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param theme Theme
|
|
|
|
function util.load(theme)
|
|
|
|
vim.cmd("hi clear")
|
|
|
|
if vim.fn.exists("syntax_on") then vim.cmd("syntax reset") 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"
|
|
|
|
|
|
|
|
-- load base theme
|
2021-04-25 13:50:02 +00:00
|
|
|
util.syntax(theme.base)
|
2021-04-17 19:22:55 +00:00
|
|
|
|
|
|
|
-- load syntax for plugins and terminal async
|
|
|
|
local async
|
|
|
|
async = vim.loop.new_async(vim.schedule_wrap(function()
|
2021-04-20 09:58:45 +00:00
|
|
|
util.terminal(theme.colors)
|
2021-04-25 13:50:02 +00:00
|
|
|
util.syntax(theme.plugins)
|
2021-04-20 10:19:20 +00:00
|
|
|
util.autocmds(theme.config)
|
2021-04-17 19:22:55 +00:00
|
|
|
async:close()
|
|
|
|
end))
|
|
|
|
async:send()
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2021-04-23 18:21:12 +00:00
|
|
|
---@param config Config
|
|
|
|
---@param colors ColorScheme
|
|
|
|
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 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
|
|
|
|
colors[key] = colors[value]
|
|
|
|
end
|
2021-04-23 06:48:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-25 13:50:02 +00:00
|
|
|
function util.light(brightness)
|
2021-04-24 20:24:40 +00:00
|
|
|
for hl_name, hl in pairs(vim.api.nvim__get_hl_defs(0)) do
|
|
|
|
local def = {}
|
|
|
|
for key, def_key in pairs({ foreground = "fg", background = "bg", special = "sp" }) do
|
|
|
|
if type(hl[key]) == "number" then
|
|
|
|
local hex = string.format("#%06x", hl[key])
|
2021-04-25 13:50:02 +00:00
|
|
|
local color = util.invertColor(hex)
|
|
|
|
if brightness then color = util.brighten(hex, brightness) end
|
2021-04-24 20:24:40 +00:00
|
|
|
table.insert(def, "gui" .. def_key .. "=" .. color)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if hl_name ~= "" and #def > 0 then
|
2021-04-25 13:50:02 +00:00
|
|
|
for _, style in pairs({ "bold", "italic", "underline", "undercurl", "reverse" }) do
|
2021-04-24 20:24:40 +00:00
|
|
|
if hl[style] then table.insert(def, "gui=" .. style) end
|
|
|
|
end
|
|
|
|
|
|
|
|
vim.cmd("highlight! " .. hl_name .. " " .. table.concat(def, " "))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-17 19:22:55 +00:00
|
|
|
return util
|
|
|
|
|