refactor: rename cterm_color.lua -> color_utils.lua

This commit is contained in:
shadmansaleh 2021-08-30 18:07:24 +06:00
parent 1439ba6d37
commit 1afc45294a
3 changed files with 7 additions and 9 deletions

View File

@ -5,7 +5,7 @@ local lualine_require = require'lualine_require'
local require = lualine_require.require
local modules = lualine_require.lazy_require{
utils = 'lualine.utils.utils',
color_utils = 'lualine.utils.cterm_colors',
color_utils = 'lualine.utils.color_utils',
}
local section_highlight_map = {x = 'c', y = 'b', z = 'a'}
local active_theme = nil
@ -35,14 +35,14 @@ function M.highlight(name, foreground, background, gui, link, reload)
table.insert(command, 'guifg=' .. foreground)
if create_cterm_colors then
table.insert(command,
'ctermfg=' .. modules.color_utils.get_cterm_color(foreground))
'ctermfg=' .. modules.color_utils.rgb2cterm(foreground))
end
end
if background and background ~= 'none' then
table.insert(command, 'guibg=' .. background)
if create_cterm_colors then
table.insert(command,
'ctermbg=' .. modules.color_utils.get_cterm_color(background))
'ctermbg=' .. modules.color_utils.rgb2cterm(background))
end
end
if gui then
@ -59,9 +59,7 @@ end
function M.create_highlight_groups(theme)
modules.utils.clear_highlights()
active_theme = theme
if not vim.go.termguicolors then
create_cterm_colors = true
end
create_cterm_colors = not vim.go.termguicolors
for mode, sections in pairs(theme) do
for section, color in pairs(sections) do
local highlight_group_name = {'lualine', section, mode}

View File

@ -274,7 +274,7 @@ local color_table = {
}
-- LuaFormatter on
function M.get_cterm_color(hex_color)
function M.rgb2cterm(hex_color)
local function get_color_distance(color1, color2)
-- returns how much color2 deviates from color1
local dr = math.abs(color1[1] - color2[1]) / (color1[1] + 1) * 100

View File

@ -58,12 +58,12 @@ describe('Utils', function()
end)
describe('Cterm genarator', function()
local cterm = require 'lualine.utils.cterm_colors'
local cterm = require 'lualine.utils.color_utils'
it('can convert rgb to cterm', function()
local colors = {['#112233'] = 235, ['#7928ae'] = 97, ['#017bdc'] = 68}
for rgb, ct in pairs(colors) do
eq(cterm.get_cterm_color(rgb), tostring(ct))
eq(cterm.rgb2cterm(rgb), tostring(ct))
end
end)
end)