feat: use background setting to determine light / dark theme #15
This commit is contained in:
@@ -92,6 +92,11 @@ require('lualine').setup {
|
|||||||
|
|
||||||
The theme comes in three styles, `storm`, a darker variant `night` and `day`.
|
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 |
|
| Option | Default | Description |
|
||||||
| ----------------------------------- | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
| ----------------------------------- | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||||
| tokyonight_style | `"storm"` | The theme comes in three styles, `storm`, a darker variant `night` and `day`. |
|
| tokyonight_style | `"storm"` | The theme comes in three styles, `storm`, a darker variant `night` and `day`. |
|
||||||
|
|||||||
@@ -36,11 +36,11 @@ tokyonight.inactive = {
|
|||||||
c = { bg = colors.bg_statusline, fg = colors.fg_gutter },
|
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 _, mode in pairs(tokyonight) do
|
||||||
for _, section in pairs(mode) do
|
for _, section in pairs(mode) do
|
||||||
if section.bg then section.bg = util.getColor(section.bg, config) end
|
if section.bg then section.bg = util.getColor(section.bg) end
|
||||||
if section.fg then section.fg = util.getColor(section.fg, config) end
|
if section.fg then section.fg = util.getColor(section.fg) end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ function M.setup(config)
|
|||||||
diff = { change = "#394b70", add = "#164846", delete = "#823c41" },
|
diff = { change = "#394b70", add = "#164846", delete = "#823c41" },
|
||||||
git = { change = "#6183bb", add = "#449dab", delete = "#f7768e" },
|
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 = {
|
colors.gitSigns = {
|
||||||
add = util.brighten(colors.diff.add, .2),
|
add = util.brighten(colors.diff.add, .2),
|
||||||
|
|||||||
@@ -27,4 +27,6 @@ config = {
|
|||||||
darkSidebar = opt("dark_sidebar", true),
|
darkSidebar = opt("dark_sidebar", true),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if config.style == "day" then vim.o.background = "light" end
|
||||||
|
|
||||||
return config
|
return config
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ function util.brighten(color, percentage)
|
|||||||
return hsluv.hsluv_to_hex(hsl)
|
return hsluv.hsluv_to_hex(hsl)
|
||||||
end
|
end
|
||||||
|
|
||||||
function util.getDayColor(color)
|
function util.invertColor(color)
|
||||||
if color ~= "NONE" then
|
if color ~= "NONE" then
|
||||||
if color ~= "NONE" then
|
if color ~= "NONE" then
|
||||||
local hsl = hsluv.hex_to_hsluv(color)
|
local hsl = hsluv.hex_to_hsluv(color)
|
||||||
@@ -57,21 +57,21 @@ function util.getDayColor(color)
|
|||||||
return color
|
return color
|
||||||
end
|
end
|
||||||
|
|
||||||
function util.getColor(color, config)
|
function util.getColor(color)
|
||||||
if config.style ~= "day" then return color end
|
if vim.o.background == "dark" then return color end
|
||||||
if not util.colorCache[color] then util.colorCache[color] = util.getDayColor(color) end
|
if not util.colorCache[color] then util.colorCache[color] = util.invertColor(color) end
|
||||||
return util.colorCache[color]
|
return util.colorCache[color]
|
||||||
end
|
end
|
||||||
|
|
||||||
function util.highlight(group, color, opts)
|
function util.highlight(group, color)
|
||||||
if color.fg then util.colorsUsed[color.fg] = true end
|
if color.fg then util.colorsUsed[color.fg] = true end
|
||||||
if color.bg then util.colorsUsed[color.bg] = true end
|
if color.bg then util.colorsUsed[color.bg] = true end
|
||||||
if color.sp then util.colorsUsed[color.sp] = true end
|
if color.sp then util.colorsUsed[color.sp] = true end
|
||||||
|
|
||||||
local style = color.style and "gui=" .. color.style or "gui=NONE"
|
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 fg = color.fg and "guifg=" .. util.getColor(color.fg) or "guifg=NONE"
|
||||||
local bg = color.bg and "guibg=" .. util.getColor(color.bg, opts) or "guibg=NONE"
|
local bg = color.bg and "guibg=" .. util.getColor(color.bg) or "guibg=NONE"
|
||||||
local sp = color.sp and "guisp=" .. util.getColor(color.sp, opts) or ""
|
local sp = color.sp and "guisp=" .. util.getColor(color.sp) or ""
|
||||||
|
|
||||||
local hl = "highlight " .. group .. " " .. style .. " " .. fg .. " " .. bg .. " " .. sp
|
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))
|
return (str:gsub("($%b{})", function(w) return table[w:sub(3, -2)] or w end))
|
||||||
end
|
end
|
||||||
|
|
||||||
function util.syntax(syntax, opts)
|
function util.syntax(syntax) for group, colors in pairs(syntax) do util.highlight(group, colors) end end
|
||||||
for group, colors in pairs(syntax) do util.highlight(group, colors, opts) end
|
|
||||||
end
|
|
||||||
|
|
||||||
---@param colors ColorScheme
|
---@param colors ColorScheme
|
||||||
function util.terminal(colors)
|
function util.terminal(colors)
|
||||||
@@ -166,18 +164,18 @@ end
|
|||||||
function util.load(theme)
|
function util.load(theme)
|
||||||
vim.cmd("hi clear")
|
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.background = "dark"
|
|
||||||
vim.o.termguicolors = true
|
vim.o.termguicolors = true
|
||||||
vim.g.colors_name = "tokyonight"
|
vim.g.colors_name = "tokyonight"
|
||||||
|
|
||||||
-- load base theme
|
-- load base theme
|
||||||
util.syntax(theme.base, theme.config)
|
util.syntax(theme.base)
|
||||||
|
|
||||||
-- load syntax for plugins and terminal async
|
-- load syntax for plugins and terminal async
|
||||||
local async
|
local async
|
||||||
async = vim.loop.new_async(vim.schedule_wrap(function()
|
async = vim.loop.new_async(vim.schedule_wrap(function()
|
||||||
util.terminal(theme.colors)
|
util.terminal(theme.colors)
|
||||||
util.syntax(theme.plugins, theme.config)
|
util.syntax(theme.plugins)
|
||||||
util.autocmds(theme.config)
|
util.autocmds(theme.config)
|
||||||
async:close()
|
async:close()
|
||||||
end))
|
end))
|
||||||
@@ -203,18 +201,19 @@ function util.color_overrides(colors, config)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function util.light()
|
function util.light(brightness)
|
||||||
for hl_name, hl in pairs(vim.api.nvim__get_hl_defs(0)) do
|
for hl_name, hl in pairs(vim.api.nvim__get_hl_defs(0)) do
|
||||||
local def = {}
|
local def = {}
|
||||||
for key, def_key in pairs({ foreground = "fg", background = "bg", special = "sp" }) do
|
for key, def_key in pairs({ foreground = "fg", background = "bg", special = "sp" }) do
|
||||||
if type(hl[key]) == "number" then
|
if type(hl[key]) == "number" then
|
||||||
local hex = string.format("#%06x", hl[key])
|
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)
|
table.insert(def, "gui" .. def_key .. "=" .. color)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if hl_name ~= "" and #def > 0 then
|
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
|
if hl[style] then table.insert(def, "gui=" .. style) end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user