Skip to content

Commit

Permalink
feat: added cterm support
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Apr 23, 2021
1 parent 46579c9 commit c967980
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ The theme comes in two styles, `storm` and a darker variant `night`.
| tokyonight_sidebars | `{}` | Set a darker background on sidebar-like windows. For example: `["quickfix", "__vista__", "terminal"]` |
| 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 |

```lua
-- Example config in Lua
Expand Down
47 changes: 46 additions & 1 deletion lua/tokyonight/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,18 @@ function util.highlight(group, color)
local fg = color.fg and "guifg=" .. color.fg or "guifg=NONE"
local bg = color.bg and "guibg=" .. color.bg or "guibg=NONE"
local sp = color.sp and "guisp=" .. color.sp or ""
vim.cmd("highlight " .. group .. " " .. style .. " " .. fg .. " " .. bg .. " " .. sp)

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

Expand Down Expand Up @@ -151,5 +162,39 @@ function util.load(theme)

end

local xterm_colors = nil
local xterm_color_map = {}

function util.gui2cterm(color)
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
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

0 comments on commit c967980

Please sign in to comment.