From 7a45a4f7fe9221a3fe681556dd5fa730411385e5 Mon Sep 17 00:00:00 2001 From: shadmansaleh Date: Mon, 2 Aug 2021 20:25:34 +0600 Subject: [PATCH] feat: Add support for useing color_name like red and cterm values to set colors in lualine --- CONTRIBUTING.md | 1 + lua/lualine/highlight.lua | 16 ++++++++++++++++ lua/lualine/utils/cterm_colors.lua | 16 ++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 84a3fc5..f53a991 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -15,6 +15,7 @@ lualine theme at lua/lualine/themes/{your_colorscheme}.lua in you repo. theme example To create a custom theme you need to define a colorscheme for each of vim's modes. Each mode has a `fg` and `bg` field for every lualine section. +To specify colors you can use #rrggbb/color_name(like: red)/cterm_color(0-255). You can add special effects with `gui`. Though the example shows a,b,c being set you can specify theme for x, y, z too. diff --git a/lua/lualine/highlight.lua b/lua/lualine/highlight.lua index 22282ec..15a7cdf 100644 --- a/lua/lualine/highlight.lua +++ b/lua/lualine/highlight.lua @@ -6,7 +6,23 @@ local utils = require 'lualine.utils.utils' local section_highlight_map = {x = 'c', y = 'b', z = 'a'} local active_theme = nil +local function sanitize_color(color) + if type(color) == 'string' then + if color:sub(1,1) == '#' then return color end -- RGB value + local converter = require 'lualine.utils.cterm_colors' + return converter.color_name2rgb(color) + elseif type(color) == 'number' then + if color > 255 then + error("What's this it can't be higher then 255 and you've given "..color) + end + local converter = require 'lualine.utils.cterm_colors' + return converter.cterm2rgb(color) + end +end + function M.highlight(name, foreground, background, gui, reload) + foreground = sanitize_color(foreground) + background = sanitize_color(background) local command = {'highlight', name} if foreground and foreground ~= 'none' then table.insert(command, 'guifg=' .. foreground) diff --git a/lua/lualine/utils/cterm_colors.lua b/lua/lualine/utils/cterm_colors.lua index e82afb5..5648b17 100644 --- a/lua/lualine/utils/cterm_colors.lua +++ b/lua/lualine/utils/cterm_colors.lua @@ -300,4 +300,20 @@ function M.get_cterm_color(hex_color) return closest_cterm_color end +function M.color_name2rgb(name) + local color_val = vim.api.nvim_get_color_by_name(name) + if color_val == -1 then + return '#'..name -- Assuming it's 'rrggbb' without # not rad instead of red + end + return string.format('#%06x', color_val) +end + +function M.cterm2rgb(color) + local color_data = color_table[color + 1] + if color_data ~= nil then + color_data = color_data[2] + return string.format("#%02x%02x%02x", color_data[1], color_data[2], color_data[3]) + end +end + return M