From 002bae49d4957d492e0a928d947eeedb2b31faba Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Fri, 23 Apr 2021 20:21:12 +0200 Subject: [PATCH] feat: make colors configurable --- README.md | 7 ++++-- colors/tokyonight.vim | 2 +- lua/tokyonight/colors.lua | 2 ++ lua/tokyonight/config.lua | 1 + lua/tokyonight/util.lua | 53 +++++++++++---------------------------- 5 files changed, 23 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 5664862..fd9720e 100644 --- a/README.md +++ b/README.md @@ -98,16 +98,19 @@ The theme comes in two styles, `storm` and a darker variant `night`. | tokyonight_italic_variables | `false` | Make variables and identifiers italic | | tokyonight_transparent | `false` | Enable this to disable setting the background color | | tokyonight_hide_inactive_statusline | `false` | Enabling this option, will hide inactive statuslines and replace them with a thin border instead. Should work with the standard **StatusLine** and **LuaLine**. | -| tokyonight_sidebars | `{}` | Set a darker background on sidebar-like windows. For example: `["qf", "vista_kind", "terminal", "packer"]` | +| tokyonight_sidebars | `{}` | Set a darker background on sidebar-like windows. For example: `["qf", "vista_kind", "terminal", "packer"]` | | tokyonight_dark_sidebar | `true` | Sidebar like windows like `NvimTree` get a darker background | | tokyonight_dark_float | `true` | Float windows like the lsp diagnostics windows get a darker background. | -| tokyonight_cterm_colors | `false` | Enabling this, will use the `gui` colors to set `cterm` values to their nearest equivalent. Only needed when not using terminal colors | +| tokyonight_colors | `{}` | You can override specific color groups to use other groups or a hex color | ```lua -- Example config in Lua vim.g.tokyonight_style = "night" vim.g.tokyonight_italic_functions = true vim.g.tokyonight_sidebars = { "qf", "vista_kind", "terminal", "packer" } + +-- Change the "hint" color to the "orange" color, and make the "error" color bright red +vim.g.tokyonight_colors = { hint = "orange", error = "#ff0000" } ``` ```vim diff --git a/colors/tokyonight.vim b/colors/tokyonight.vim index dcb36ea..7cf3901 100644 --- a/colors/tokyonight.vim +++ b/colors/tokyonight.vim @@ -4,6 +4,6 @@ lua package.loaded['tokyonight'] = nil lua package.loaded['tokyonight.theme'] = nil lua package.loaded['tokyonight.colors'] = nil lua package.loaded['tokyonight.util'] = nil -lua package.loaded['tokyonight.config'] = nil +" lua package.loaded['tokyonight.config'] = nil lua require('tokyonight').colorscheme() diff --git a/lua/tokyonight/colors.lua b/lua/tokyonight/colors.lua index bf6a2b9..e21b181 100644 --- a/lua/tokyonight/colors.lua +++ b/lua/tokyonight/colors.lua @@ -99,6 +99,8 @@ function M.setup(config) colors.warning = colors.yellow colors.info = colors.blue2 colors.hint = colors.teal + + util.color_overrides(colors, config) return colors end diff --git a/lua/tokyonight/config.lua b/lua/tokyonight/config.lua index 15d8a64..73764bf 100644 --- a/lua/tokyonight/config.lua +++ b/lua/tokyonight/config.lua @@ -21,6 +21,7 @@ config = { hideInactiveStatusline = opt("hide_inactive_statusline", false), terminalColors = opt("terminal_colors", true), sidebars = opt("sidebars", {}), + colors = opt("colors", {}), dev = opt("dev", false), darkFloat = opt("dark_float", true), darkSidebar = opt("dark_sidebar", true), diff --git a/lua/tokyonight/util.lua b/lua/tokyonight/util.lua index 83545f0..ebb4d15 100644 --- a/lua/tokyonight/util.lua +++ b/lua/tokyonight/util.lua @@ -44,16 +44,8 @@ function util.highlight(group, color) local bg = color.bg and "guibg=" .. color.bg or "guibg=NONE" local sp = color.sp and "guisp=" .. color.sp or "" - local cfg = color.fg and "ctermfg=" .. util.gui2cterm(color.fg) or "ctermfg=NONE" - local cbg = color.bg and "ctermbg=" .. util.gui2cterm(color.bg) or "ctermbg=NONE" - local cstyle = color.style and "cterm=" .. color.style or "cterm=NONE" - local hl = "highlight " .. group .. " " .. style .. " " .. fg .. " " .. bg .. " " .. sp - if vim.g.tokyonight_cterm_colors == true or vim.g.tokyonight_cterm_colors == 1 then - hl = hl .. " " .. cfg .. " " .. cbg .. " " .. cstyle - end - vim.cmd(hl) if color.link then vim.cmd("highlight! link " .. group .. " " .. color.link) end end @@ -162,39 +154,22 @@ function util.load(theme) end -local xterm_colors = nil -local xterm_color_map = {} - -function util.gui2cterm(color) - if color == "NONE" then return color end - if not xterm_colors then - xterm_colors = {} - for i, n in ipairs({ 47, 68, 40, 40, 40, 21 }) do - for _ = 1, n, 1 do table.insert(xterm_colors, i - 1) end +---@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 end end - - if xterm_color_map[color] then return xterm_color_map[color] end - - local rgb = hexToRgb(color) - local r = rgb[1] - local g = rgb[2] - local b = rgb[3] - - local mx = math.max(r, g, b) - local mn = math.min(r, g, b) - - if (mx - mn) * (mx + mn) <= 6250 then - local c = 24 - math.floor((252 - math.floor((r + g + b) / 3)) / 10) - if 0 <= c and c <= 23 then - xterm_color_map[color] = 232 + c - return xterm_color_map[color] - end - end - - xterm_color_map[color] = 16 + 36 * xterm_colors[r + 1] + 6 * xterm_colors[g + 1] + - xterm_colors[b + 1] - return xterm_color_map[color] end return util