diff --git a/lua/rose-pine/theme.lua b/lua/rose-pine/theme.lua index 3337bb9..029c6f6 100644 --- a/lua/rose-pine/theme.lua +++ b/lua/rose-pine/theme.lua @@ -1,4 +1,5 @@ local config = require('rose-pine.config') +local util = require('rose-pine.util') local p = require('rose-pine.palette') -- TODO: Refactor `maybe` logic @@ -38,10 +39,11 @@ local theme = { CursorLineNr = { fg = p.text }, DarkenedPanel = { bg = p.surface }, DarkenedStatusline = { bg = p.surface }, - DiffAdd = { fg = p.foam }, - DiffChange = { fg = p.rose }, - DiffDelete = { fg = p.love }, - DiffText = { fg = p.text }, + -- TODO: Allow diff overrides. This is a good reason to refactor our config logic to allow setting both fg and bg + DiffAdd = { bg = util.blend(p.foam, p.base, 0.1) }, + DiffChange = { bg = util.blend(p.rose, p.base, 0.1) }, + DiffDelete = { bg = util.blend(p.love, p.base, 0.1) }, + DiffText = { bg = util.blend(p.subtle, p.base, 0.1) }, diffAdded = { link = 'DiffAdd' }, diffChanged = { link = 'DiffChange' }, diffRemoved = { link = 'DiffDelete' }, diff --git a/lua/rose-pine/util.lua b/lua/rose-pine/util.lua new file mode 100644 index 0000000..fe60e27 --- /dev/null +++ b/lua/rose-pine/util.lua @@ -0,0 +1,37 @@ +local util = {} + +local function get_byte(value, offset) + return bit.band(bit.rshift(value, offset), 0xFF) +end + +local function get_color(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) } +end + +---@param fg string foreground color +---@param bg string background color +---@param alpha number number between 0 and 1. 0 results in bg, 1 results in fg +function util.blend(fg, bg, alpha) + bg = get_color(bg) + fg = get_color(fg) + + local blendChannel = function(i) + local ret = (alpha * fg[i] + ((1 - alpha) * bg[i])) + return math.floor(math.min(math.max(0, ret), 255) + 0.5) + end + + return string.format( + '#%02X%02X%02X', + blendChannel(1), + blendChannel(2), + blendChannel(3) + ) +end + +return util