fix named blend styles

ref #70
This commit is contained in:
mvllow 2022-02-18 11:13:00 -06:00
commit 3fb07a770f
2 changed files with 39 additions and 35 deletions

View file

@ -1,3 +1,5 @@
local util = require('rose-pine.util')
local M = {}
local show_init_messages = true
@ -165,37 +167,9 @@ function M.colorscheme()
show_init_messages = false
end
---@param color string
local function get_palette_color(color)
color = color:lower()
local p = require('rose-pine.palette')
if color and not color:find('#') and color ~= 'none' then
return p[color]
end
return color
end
---@param group string
---@param color table<string, string>
local function highlight(group, color)
local style = color.style and 'gui=' .. color.style or 'gui=NONE'
local fg = color.fg and 'guifg=' .. get_palette_color(color.fg) or 'guifg=NONE'
local bg = color.bg and 'guibg=' .. get_palette_color(color.bg) or 'guibg=NONE'
local sp = color.sp and 'guisp=' .. get_palette_color(color.sp) or ''
local hl = 'highlight ' .. group .. ' ' .. style .. ' ' .. fg .. ' ' .. bg .. ' ' .. sp
vim.cmd(hl)
if color.link then
vim.cmd('highlight! link ' .. group .. ' ' .. color.link)
end
end
local theme = require('rose-pine.theme').get(config)
for group, color in pairs(theme) do
highlight(group, color)
util.highlight(group, color)
end
require('rose-pine.galaxyline.theme')

View file

@ -1,25 +1,39 @@
local util = {}
local function get_byte(value, offset)
local function byte(value, offset)
return bit.band(bit.rshift(value, offset), 0xFF)
end
local function get_color(color)
local function rgb(color)
color = vim.api.nvim_get_color_by_name(color)
if color == -1 then
color = vim.opt.background:get() == 'dark' and 000 or 255255255
end
return { get_byte(color, 16), get_byte(color, 8), get_byte(color, 0) }
return { byte(color, 16), byte(color, 8), byte(color, 0) }
end
local function parse_color(color)
if color == nil then
return print('invalid color')
end
color = color:lower()
if not color:find('#') and color ~= 'none' then
color = require('rose-pine.palette')[color] or vim.api.nvim_get_color_by_name(color)
end
return color
end
---@param fg string foreground color
---@param bg string background color
---@param alpha number number between 0 (background) and 1 (foreground)
function util.blend(fg, bg, alpha)
bg = get_color(bg)
fg = get_color(fg)
util.blend = function(fg, bg, alpha)
fg = rgb(parse_color(fg))
bg = rgb(parse_color(bg))
local function blend_channel(i)
local ret = (alpha * fg[i] + ((1 - alpha) * bg[i]))
@ -29,4 +43,20 @@ function util.blend(fg, bg, alpha)
return string.format('#%02X%02X%02X', blend_channel(1), blend_channel(2), blend_channel(3))
end
---@param group string
---@param color table<string, string>
util.highlight = function(group, color)
local style = color.style and 'gui=' .. color.style or 'gui=NONE'
local fg = color.fg and 'guifg=' .. parse_color(color.fg) or 'guifg=NONE'
local bg = color.bg and 'guibg=' .. parse_color(color.bg) or 'guibg=NONE'
local sp = color.sp and 'guisp=' .. parse_color(color.sp) or ''
vim.cmd(string.format('highlight %s %s %s %s %s', group, style, fg, bg, sp))
if color.link then
vim.cmd(string.format('highlight! link %s %s', group, color.link))
-- vim.cmd('highlight! link ' .. group .. ' ' .. color.link)
end
end
return util