Skip to content

Commit

Permalink
feat(colors): allow color overrides for shades with predefined colors
Browse files Browse the repository at this point in the history
  • Loading branch information
ramojus committed Jun 23, 2024
1 parent fc2a4ca commit a076228
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 27 deletions.
4 changes: 0 additions & 4 deletions lua/mellifluous/color.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ function color_meta:with_lightness(val)
return M.new(oklch_to_hex(oklch))
end

function color_meta:get_hsl()
return hex_to_okhsl(self.hex)
end

function color_meta:saturated(val)
local okhsl = hex_to_okhsl(self.hex)
okhsl.s = clip(okhsl.s + val, 0, 100)
Expand Down
53 changes: 35 additions & 18 deletions lua/mellifluous/colors/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,41 @@ function M.get_color_sets_table()
}
end

local function tbl_extend_non_nil(base_table, overlay_table)
for key, _ in pairs(overlay_table) do
base_table[key] = overlay_table[key] or base_table[key]
local function get_color_overrides_bg()
local config = require('mellifluous.config').config
local global_color_overrides_bg = vim.tbl_get(config,
'color_overrides', config.is_bg_dark and 'dark' or 'light', 'bg')
local color_overrides_bg = vim.tbl_get(config, config.color_set,
'color_overrides', config.is_bg_dark and 'dark' or 'light', 'bg')

if color_overrides_bg == nil then
color_overrides_bg = global_color_overrides_bg
end

return color_overrides_bg
end

local function get_color_overrides(is_bg_dark, color_set_name)
local function apply_color_overrides(colors)
local config = require('mellifluous.config').config
local color_overrides = vim.tbl_get(config, color_set_name,
'color_overrides', is_bg_dark and 'dark' or 'light') or {}

for key, color in pairs(color_overrides) do
if color.hex then -- overrides were already converted to mellifluous.color
break
end
color_overrides[key] = require('mellifluous.color').new(color)
local global_color_overrides_fn = vim.tbl_get(config,
'color_overrides', config.is_bg_dark and 'dark' or 'light', 'colors')
local color_overrides_fn = vim.tbl_get(config, config.color_set,
'color_overrides', config.is_bg_dark and 'dark' or 'light', 'colors')

if global_color_overrides_fn ~= nil then
colors = vim.tbl_deep_extend('force', colors, global_color_overrides_fn(colors))
end
if color_overrides_fn ~= nil then
colors = vim.tbl_deep_extend('force', colors, color_overrides_fn(colors))
end
return colors
end

return color_overrides
local function ensure_correct_color_types(colors)
local color_lib = require('mellifluous.color')
for key, color in pairs(colors) do
colors[key] = color_lib.new(color.hex or color)
end
end

function M.get_is_bg_dark(color_set_name)
Expand Down Expand Up @@ -59,19 +75,20 @@ function M.get_colors()
require('mellifluous').return_error("Color set '" .. config.color_set .. "' not found")
end

local color_overrides = get_color_overrides(config.is_bg_dark, config.color_set)
local color_overrides_bg = get_color_overrides_bg()

local color_set_functions = require('mellifluous.colors.sets.' .. config.color_set)
local colors
if config.is_bg_dark then
colors = color_set_functions.get_colors_dark(color_overrides.bg or color_set_functions.get_bg_dark())
colors = color_set_functions.get_colors_dark(color_overrides_bg or color_set_functions.get_bg_dark())
else
colors = color_set_functions.get_colors_light(color_overrides.bg or color_set_functions.get_bg_light())
colors = color_set_functions.get_colors_light(color_overrides_bg or color_set_functions.get_bg_light())
end

tbl_extend_non_nil(colors, color_overrides)
colors = apply_color_overrides(colors)
ensure_correct_color_types(colors)

colors = require'mellifluous.colors.shades'.extend_with_shades(colors)
colors = require('mellifluous.colors.shades').extend_with_shades(colors)

return colors
end
Expand Down
10 changes: 5 additions & 5 deletions lua/mellifluous/colors/shades.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ function M.extend_with_shades(colors)
fg5 = fg:darkened(54),
dark_bg = colors.bg:darkened(2.5),
bg2 = colors.bg:lightened(4),
bg3 = colors.bg:lightened(7),
bg4 = colors.bg:lightened(10),
bg5 = colors.bg:lightened(13),
bg3 = colors.bg:lightened(6),
bg4 = colors.bg:lightened(8),
bg5 = colors.bg:lightened(10),
}
else
shades = {
Expand All @@ -41,8 +41,8 @@ function M.extend_with_shades(colors)
}
end

colors = vim.tbl_extend('force', colors, shared_shades)
return vim.tbl_extend('force', colors, shades)
colors = vim.tbl_extend('keep', colors, shared_shades)
return vim.tbl_extend('keep', colors, shades)
end

return M

0 comments on commit a076228

Please sign in to comment.