From 3de9dadb96c71e7c020b6cc320fc1f6d6f2fdd4c Mon Sep 17 00:00:00 2001 From: mvllow Date: Fri, 18 Feb 2022 11:13:00 -0600 Subject: [PATCH] fix named blend styles ref #70 --- lua/rose-pine/init.lua | 32 +++----------------------------- lua/rose-pine/util.lua | 42 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 39 insertions(+), 35 deletions(-) diff --git a/lua/rose-pine/init.lua b/lua/rose-pine/init.lua index 634254a7..af5bae48 100644 --- a/lua/rose-pine/init.lua +++ b/lua/rose-pine/init.lua @@ -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 - 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') diff --git a/lua/rose-pine/util.lua b/lua/rose-pine/util.lua index 67b5e08e..113d6738 100644 --- a/lua/rose-pine/util.lua +++ b/lua/rose-pine/util.lua @@ -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 +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