Skip to content

Commit

Permalink
fix: get color theme
Browse files Browse the repository at this point in the history
The new default theme of neovim doesn't set terminal colors, so we need
another way to get colors.
  • Loading branch information
zztrieuzz committed Jul 12, 2024
1 parent 47b53bb commit bef7357
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 42 deletions.
76 changes: 36 additions & 40 deletions lua/windline/themes/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,24 @@ local M = {}

local cache_theme = {}

M.default_theme = nil

local get_default_theme = function()
if vim.o.background == 'light' then
return vim.deepcopy(M.default_theme or require('windline.themes.wind_light'))
end
return vim.deepcopy(M.default_theme or require('windline.themes.wind'))
end

M.load_theme = function(name)
name = name or vim.g.colors_name
if not name then return get_default_theme() end
if not name then return M.get_default_theme() end
local cache_name = name .. '_' .. (vim.o.background or '')
if cache_theme[cache_name] then
return cache_theme[cache_name]
end
local colors
if vim.o.background == 'light' then
local ok, light_theme = pcall(require, 'windline.themes.' .. name ..'_light')
local ok, light_theme = pcall(require, 'windline.themes.' .. name .. '_light')
if ok then
colors = vim.deepcopy(light_theme)
end
end
if not colors then
local ok, themes_color = pcall(require, 'windline.themes.' .. name)
if not ok then
ok, colors = pcall(M.generate_theme)
if not ok then colors = get_default_theme() end
colors = M.get_default_theme()
else
colors = vim.deepcopy(themes_color)
end
Expand All @@ -43,56 +33,62 @@ M.clear_cache = function()
end

M.get_hl_color = function(group_name)
local ok, hl = pcall(vim.api.nvim_get_hl, 0, { name = group_name, link = false})
local ok, hl = pcall(vim.api.nvim_get_hl, 0, { name = group_name, link = false })
if not ok then return nil, nil end
local fg = hl.fg and '#' .. bit.tohex(hl.fg, 6)
local bg = hl.bg and '#' .. bit.tohex(hl.bg, 6)
return fg, bg
end

local function is_dark(color_value)

M.is_dark = function(color_value)
local bg_numeric_value = 0;
for s in color_value:gmatch("[a-fA-F0-9][a-fA-F0-9]") do
bg_numeric_value = bg_numeric_value + tonumber("0x"..s);
bg_numeric_value = bg_numeric_value + tonumber("0x" .. s);
end
local is_dark_bg = (bg_numeric_value < 383)
return is_dark_bg
end

M.generate_theme = function()
local default = get_default_theme()
local default_theme = require('windline.themes.wind')
local function c(color)
vim.api.nvim_set_hl(0, "WindLineTcolor", { fg = color })
local fg = M.get_hl_color('WindLineTcolor')
return fg or default_theme[color]
end

M.get_default_theme = function()
local colors = {
black = vim.g.terminal_color_0,
red = vim.g.terminal_color_1,
green = vim.g.terminal_color_2,
yellow = vim.g.terminal_color_3,
blue = vim.g.terminal_color_4,
magenta = vim.g.terminal_color_5,
cyan = vim.g.terminal_color_6,
white = vim.g.terminal_color_7,
black_light = vim.g.terminal_color_8,
red_light = vim.g.terminal_color_9,
green_light = vim.g.terminal_color_10,
yellow_light = vim.g.terminal_color_11,
blue_light = vim.g.terminal_color_12,
magenta_light = vim.g.terminal_color_13,
cyan_light = vim.g.terminal_color_14,
white_light = vim.g.terminal_color_15,
black = vim.g.terminal_color_0 or c 'black',
red = vim.g.terminal_color_1 or c 'red',
green = vim.g.terminal_color_2 or c 'green',
yellow = vim.g.terminal_color_3 or c 'yellow',
blue = vim.g.terminal_color_4 or c 'blue',
magenta = vim.g.terminal_color_5 or c 'magenta',
cyan = vim.g.terminal_color_6 or c 'cyan',
white = vim.g.terminal_color_7 or c 'white',
black_light = vim.g.terminal_color_8 or c 'darkgrey',
red_light = vim.g.terminal_color_9 or c 'lightred',
yellow_light = vim.g.terminal_color_10 or c 'lightyellow',
blue_light = vim.g.terminal_color_11 or c 'lightblue',
magenta_light = vim.g.terminal_color_12 or c 'lightmagenta',
green_light = vim.g.terminal_color_13 or c 'lightgreen',
cyan_light = vim.g.terminal_color_14 or c 'lightcyan',
white_light = vim.g.terminal_color_15 or c 'lightgrey',
}

if is_dark(colors.black) and vim.o.background == 'light'then
if M.is_dark(colors.black) and vim.o.background == 'light' then
-- swap some color to match light theme
local tmp = colors.black
colors.black = colors.white
colors.white = tmp
tmp = colors.black_light
colors.black_light = colors.white_light
colors.white_light =tmp
colors.white_light = tmp
end

local fgNormal, bgNormal = M.get_hl_color('Normal')
colors.NormalFg = fgNormal or colors.white_light
colors.NormalBg = bgNormal or colors.black_light
colors.NormalFg = fgNormal or colors.white
colors.NormalBg = bgNormal or colors.black

local fgInactive, bgInactive = M.get_hl_color('StatusLineNC')
colors.InactiveFg = fgInactive or colors.white_light
Expand All @@ -102,7 +98,7 @@ M.generate_theme = function()
colors.ActiveFg = fgActive or colors.white
colors.ActiveBg = bgActive or colors.black

return vim.tbl_extend('keep', colors, default)
return colors
end

return M
13 changes: 11 additions & 2 deletions lua/wlanimation/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@ local M = {}

---@return HSL
M.rgb_to_hsl = function(rgb)
local h, s, l = hsl.rgb_string_to_hsl(rgb)
return hsl.new(h, s, l, rgb)
if rgb == nil or #rgb ~= 7 then return hsl.new(0, 0, 0, '') end
return hsl.new(rgb)
end

---@return string
M.shade_or_tint = function(rgb, value)
if rgb == nil or #rgb ~= 7 then return rgb end
if vim.o.background == 'light' then
return hsl.new(rgb):shade(value):to_rgb()
end
return hsl.new(rgb):tint(value):to_rgb()
end

M.get_hsl_color = function(hl)
Expand Down

0 comments on commit bef7357

Please sign in to comment.