♻️ refactor(rooticolor): move color utilities to module

Create a rooticolor module for color utilities that are used in multiple locations in config
This commit is contained in:
2024-08-07 08:19:21 -04:00
parent 4674922b23
commit 25352b81a0
5 changed files with 61 additions and 108 deletions
+10 -39
View File
@@ -3,44 +3,15 @@
-- ╰─────────────────────────────────────────────────────────╯
local M = {}
-- Function to convert RGB to hexadecimal
local function rgb_to_hex(rgb)
return string.format("#%02x%02x%02x", rgb[1], rgb[2], rgb[3])
end
local function get_bg_color(hlgroup)
local hl = vim.api.nvim_get_hl(0, { name = hlgroup, link = false })
local bg = hl.bg
if bg then
return rgb_to_hex({
bit.rshift(bit.band(bg, 0xFF0000), 16),
bit.rshift(bit.band(bg, 0x00FF00), 8),
bit.band(bg, 0x0000FF),
})
end
return nil
end
local function get_fg_color(hlgroup)
local hl = vim.api.nvim_get_hl(0, { name = hlgroup, link = false })
local fg = hl.fg
if fg then
return rgb_to_hex({
bit.rshift(bit.band(fg, 0xFF0000), 16),
bit.rshift(bit.band(fg, 0x00FF00), 8),
bit.band(fg, 0x0000FF),
})
end
return nil
end
local rooticolor = require("utils.rooticolor")
-- Function to set up indent highlights
function M.setup_indent_highlight()
-- Get the background color of CursorLine
local cursorline_bg_hex = get_bg_color("CursorLine")
local cursorline_bg_hex = rooticolor.get_bg_color("CursorLine")
-- Get the foreground color of Error
local miniiconsred_fg_hex = get_fg_color("Error")
local miniiconsred_fg_hex = rooticolor.get_fg_color("Error")
local hooks = require("ibl.hooks")
-- create the highlight groups in the highlight setup hook, so they are reset
@@ -139,27 +110,27 @@ function M.setup_mode_highlight()
local bg_color
if current_mode == "n" then
bg_color = get_bg_color("MiniStatuslineModeNormal")
bg_color = rooticolor.get_bg_color("MiniStatuslineModeNormal")
vim.api.nvim_set_hl(0, "SmoothCursor", { fg = bg_color })
vim.fn.sign_define("smoothcursor", { text = "" })
elseif current_mode == "v" then
bg_color = get_bg_color("MiniStatuslineModeVisual")
bg_color = rooticolor.get_bg_color("MiniStatuslineModeVisual")
vim.api.nvim_set_hl(0, "SmoothCursor", { fg = bg_color })
vim.fn.sign_define("smoothcursor", { text = "" })
elseif current_mode == "V" then
bg_color = get_bg_color("MiniStatuslineModeVisual")
bg_color = rooticolor.get_bg_color("MiniStatuslineModeVisual")
vim.api.nvim_set_hl(0, "SmoothCursor", { fg = bg_color })
vim.fn.sign_define("smoothcursor", { text = "" })
elseif current_mode == "R" or current_mode == "r" then
bg_color = get_bg_color("MiniStatuslineModeReplace")
bg_color = rooticolor.get_bg_color("MiniStatuslineModeReplace")
vim.api.nvim_set_hl(0, "SmoothCursor", { fg = bg_color })
vim.fn.sign_define("smoothcursor", { text = "" })
elseif current_mode == "i" then
bg_color = get_bg_color("MiniStatuslineModeInsert")
bg_color = rooticolor.get_bg_color("MiniStatuslineModeInsert")
vim.api.nvim_set_hl(0, "SmoothCursor", { fg = bg_color })
vim.fn.sign_define("smoothcursor", { text = "" })
else
bg_color = get_bg_color("MiniStatuslineModeNormal")
bg_color = rooticolor.get_bg_color("MiniStatuslineModeNormal")
vim.api.nvim_set_hl(0, "SmoothCursor", { fg = bg_color })
vim.fn.sign_define("smoothcursor", { text = "" })
end
@@ -167,7 +138,7 @@ end
function M.setup_dashboard_highlight()
if not vim.g.DashboardHeaderColor then
local dash_color = get_fg_color("Error")
local dash_color = rooticolor.get_fg_color("Error")
vim.api.nvim_set_hl(0, "DashboardHeader", { fg = dash_color })
else
vim.api.nvim_set_hl(