feat: use background setting to determine light / dark theme #15

This commit is contained in:
Folke Lemaitre
2021-04-25 15:50:02 +02:00
parent 8041e49cf7
commit f7b4afbba2
5 changed files with 27 additions and 21 deletions

View File

@@ -92,6 +92,11 @@ require('lualine').setup {
The theme comes in three styles, `storm`, a darker variant `night` and `day`.
The **day** style will be used if:
+ `vim.g.tokyonight_style == "day"`
+ or `vim.o.background == "light"`
| Option | Default | Description |
| ----------------------------------- | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| tokyonight_style | `"storm"` | The theme comes in three styles, `storm`, a darker variant `night` and `day`. |

View File

@@ -36,11 +36,11 @@ tokyonight.inactive = {
c = { bg = colors.bg_statusline, fg = colors.fg_gutter },
}
if config.style == "day" then
if vim.o.background == "light" then
for _, mode in pairs(tokyonight) do
for _, section in pairs(mode) do
if section.bg then section.bg = util.getColor(section.bg, config) end
if section.fg then section.fg = util.getColor(section.fg, config) end
if section.bg then section.bg = util.getColor(section.bg) end
if section.fg then section.fg = util.getColor(section.fg) end
end
end
end

View File

@@ -42,7 +42,7 @@ function M.setup(config)
diff = { change = "#394b70", add = "#164846", delete = "#823c41" },
git = { change = "#6183bb", add = "#449dab", delete = "#f7768e" },
}
if config.style == "night" or config.style == "day" then colors.bg = "#1a1b26" end
if config.style == "night" or vim.o.background == "light" then colors.bg = "#1a1b26" end
colors.gitSigns = {
add = util.brighten(colors.diff.add, .2),

View File

@@ -27,4 +27,6 @@ config = {
darkSidebar = opt("dark_sidebar", true),
}
if config.style == "day" then vim.o.background = "light" end
return config

View File

@@ -45,7 +45,7 @@ function util.brighten(color, percentage)
return hsluv.hsluv_to_hex(hsl)
end
function util.getDayColor(color)
function util.invertColor(color)
if color ~= "NONE" then
if color ~= "NONE" then
local hsl = hsluv.hex_to_hsluv(color)
@@ -57,21 +57,21 @@ function util.getDayColor(color)
return color
end
function util.getColor(color, config)
if config.style ~= "day" then return color end
if not util.colorCache[color] then util.colorCache[color] = util.getDayColor(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
return util.colorCache[color]
end
function util.highlight(group, color, opts)
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
local style = color.style and "gui=" .. color.style or "gui=NONE"
local fg = color.fg and "guifg=" .. util.getColor(color.fg, opts) or "guifg=NONE"
local bg = color.bg and "guibg=" .. util.getColor(color.bg, opts) or "guibg=NONE"
local sp = color.sp and "guisp=" .. util.getColor(color.sp, opts) or ""
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 ""
local hl = "highlight " .. group .. " " .. style .. " " .. fg .. " " .. bg .. " " .. sp
@@ -128,9 +128,7 @@ function util.template(str, table)
return (str:gsub("($%b{})", function(w) return table[w:sub(3, -2)] or w end))
end
function util.syntax(syntax, opts)
for group, colors in pairs(syntax) do util.highlight(group, colors, opts) 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)
@@ -166,18 +164,18 @@ end
function util.load(theme)
vim.cmd("hi clear")
if vim.fn.exists("syntax_on") then vim.cmd("syntax reset") end
vim.o.background = "dark"
vim.o.termguicolors = true
vim.g.colors_name = "tokyonight"
-- load base theme
util.syntax(theme.base, theme.config)
util.syntax(theme.base)
-- load syntax for plugins and terminal async
local async
async = vim.loop.new_async(vim.schedule_wrap(function()
util.terminal(theme.colors)
util.syntax(theme.plugins, theme.config)
util.syntax(theme.plugins)
util.autocmds(theme.config)
async:close()
end))
@@ -203,18 +201,19 @@ function util.color_overrides(colors, config)
end
end
function util.light()
function util.light(brightness)
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])
local color = util.getDayColor(hex)
local color = util.invertColor(hex)
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" }) do
for _, style in pairs({ "bold", "italic", "underline", "undercurl", "reverse" }) do
if hl[style] then table.insert(def, "gui=" .. style) end
end