2021-09-20 11:46:38 +00:00
|
|
|
-- Copyright (c) 2020-2021 shadmansaleh
|
|
|
|
-- MIT license, see LICENSE for more details.
|
2022-01-02 11:38:39 +00:00
|
|
|
local utils = require('lualine.utils.utils')
|
|
|
|
local loader = require('lualine.utils.loader')
|
2021-08-14 06:02:30 +00:00
|
|
|
|
2021-09-03 18:28:20 +00:00
|
|
|
local color_name = vim.g.colors_name
|
2021-08-14 06:02:30 +00:00
|
|
|
if color_name then
|
|
|
|
-- Check if there's a theme for current colorscheme
|
|
|
|
-- If there is load that instead of genarating a new one
|
|
|
|
local ok, theme = pcall(loader.load_theme, color_name)
|
2021-09-03 18:28:20 +00:00
|
|
|
if ok and theme then
|
|
|
|
return theme
|
|
|
|
end
|
2021-08-14 06:02:30 +00:00
|
|
|
end
|
2021-03-02 13:01:34 +00:00
|
|
|
|
|
|
|
---------------
|
|
|
|
-- Constents --
|
|
|
|
---------------
|
|
|
|
-- fg and bg must have this much contrast range 0 < contrast_threshold < 0.5
|
|
|
|
local contrast_threshold = 0.3
|
|
|
|
-- how much brightness is changed in percentage for light and dark themes
|
|
|
|
local brightness_modifier_parameter = 10
|
|
|
|
|
|
|
|
-- truns #rrggbb -> { red, green, blue }
|
|
|
|
local function rgb_str2num(rgb_color_str)
|
2022-01-02 11:38:39 +00:00
|
|
|
if rgb_color_str:find('#') == 1 then
|
2021-03-15 23:37:46 +00:00
|
|
|
rgb_color_str = rgb_color_str:sub(2, #rgb_color_str)
|
|
|
|
end
|
|
|
|
local red = tonumber(rgb_color_str:sub(1, 2), 16)
|
|
|
|
local green = tonumber(rgb_color_str:sub(3, 4), 16)
|
|
|
|
local blue = tonumber(rgb_color_str:sub(5, 6), 16)
|
2021-09-03 18:28:20 +00:00
|
|
|
return { red = red, green = green, blue = blue }
|
2021-03-02 13:01:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- turns { red, green, blue } -> #rrggbb
|
|
|
|
local function rgb_num2str(rgb_color_num)
|
2021-09-03 18:28:20 +00:00
|
|
|
local rgb_color_str = string.format('#%02x%02x%02x', rgb_color_num.red, rgb_color_num.green, rgb_color_num.blue)
|
2021-03-02 13:01:34 +00:00
|
|
|
return rgb_color_str
|
|
|
|
end
|
|
|
|
|
|
|
|
-- returns brightness lavel of color in range 0 to 1
|
|
|
|
-- arbitary value it's basicaly an weighted average
|
|
|
|
local function get_color_brightness(rgb_color)
|
|
|
|
local color = rgb_str2num(rgb_color)
|
|
|
|
local brightness = (color.red * 2 + color.green * 3 + color.blue) / 6
|
|
|
|
return brightness / 256
|
|
|
|
end
|
|
|
|
|
|
|
|
-- returns average of colors in range 0 to 1
|
|
|
|
-- used to ditermine contrast lavel
|
|
|
|
local function get_color_avg(rgb_color)
|
|
|
|
local color = rgb_str2num(rgb_color)
|
|
|
|
return (color.red + color.green + color.blue) / 3 / 256
|
|
|
|
end
|
|
|
|
|
|
|
|
-- clamps the val between left and right
|
|
|
|
local function clamp(val, left, right)
|
2021-09-03 18:28:20 +00:00
|
|
|
if val > right then
|
|
|
|
return right
|
|
|
|
end
|
|
|
|
if val < left then
|
|
|
|
return left
|
|
|
|
end
|
2021-03-02 13:01:34 +00:00
|
|
|
return val
|
|
|
|
end
|
|
|
|
|
|
|
|
-- changes braghtness of rgb_color by percentage
|
|
|
|
local function brightness_modifier(rgb_color, parcentage)
|
|
|
|
local color = rgb_str2num(rgb_color)
|
|
|
|
color.red = clamp(color.red + (color.red * parcentage / 100), 0, 255)
|
|
|
|
color.green = clamp(color.green + (color.green * parcentage / 100), 0, 255)
|
|
|
|
color.blue = clamp(color.blue + (color.blue * parcentage / 100), 0, 255)
|
|
|
|
return rgb_num2str(color)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- changes contrast of rgb_color by amount
|
|
|
|
local function contrast_modifier(rgb_color, amount)
|
|
|
|
local color = rgb_str2num(rgb_color)
|
|
|
|
color.red = clamp(color.red + amount, 0, 255)
|
|
|
|
color.green = clamp(color.green + amount, 0, 255)
|
|
|
|
color.blue = clamp(color.blue + amount, 0, 255)
|
|
|
|
return rgb_num2str(color)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Changes brightness of foreground color to achive contrast
|
|
|
|
-- without changing the color
|
|
|
|
local function apply_contrast(highlight)
|
|
|
|
local hightlight_bg_avg = get_color_avg(highlight.bg)
|
|
|
|
local contrast_threshold_config = clamp(contrast_threshold, 0, 0.5)
|
|
|
|
local contranst_change_step = 5
|
2021-09-03 18:28:20 +00:00
|
|
|
if hightlight_bg_avg > 0.5 then
|
|
|
|
contranst_change_step = -contranst_change_step
|
|
|
|
end
|
2021-03-02 13:01:34 +00:00
|
|
|
|
|
|
|
-- donn't waste too much time here max 25 interation should be more than enough
|
|
|
|
local iteration_count = 1
|
2021-09-03 18:28:20 +00:00
|
|
|
while
|
|
|
|
math.abs(get_color_avg(highlight.fg) - hightlight_bg_avg) < contrast_threshold_config and iteration_count < 25
|
|
|
|
do
|
2021-03-02 13:01:34 +00:00
|
|
|
highlight.fg = contrast_modifier(highlight.fg, contranst_change_step)
|
|
|
|
iteration_count = iteration_count + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Get the colors to create theme
|
2021-09-03 18:28:20 +00:00
|
|
|
-- stylua: ignore
|
2021-03-02 13:01:34 +00:00
|
|
|
local colors = {
|
2021-09-17 09:16:38 +00:00
|
|
|
normal = utils.extract_color_from_hllist('bg', { 'PmenuSel', 'PmenuThumb', 'TabLineSel' }, '#000000'),
|
|
|
|
insert = utils.extract_color_from_hllist('fg', { 'String', 'MoreMsg' }, '#000000'),
|
|
|
|
replace = utils.extract_color_from_hllist('fg', { 'Number', 'Type' }, '#000000'),
|
|
|
|
visual = utils.extract_color_from_hllist('fg', { 'Special', 'Boolean', 'Constant' }, '#000000'),
|
|
|
|
command = utils.extract_color_from_hllist('fg', { 'Identifier' }, '#000000'),
|
|
|
|
back1 = utils.extract_color_from_hllist('bg', { 'Normal', 'StatusLineNC' }, '#000000'),
|
|
|
|
fore = utils.extract_color_from_hllist('fg', { 'Normal', 'StatusLine' }, '#000000'),
|
|
|
|
back2 = utils.extract_color_from_hllist('bg', { 'StatusLine' }, '#000000'),
|
2021-03-02 13:01:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
-- Change brightness of colors
|
|
|
|
-- darken incase of light theme lighten incase of dark theme
|
|
|
|
|
2021-07-29 16:47:07 +00:00
|
|
|
local normal_color = utils.extract_highlight_colors('Normal', 'bg')
|
|
|
|
if normal_color ~= nil then
|
|
|
|
if get_color_brightness(normal_color) > 0.5 then
|
|
|
|
brightness_modifier_parameter = -brightness_modifier_parameter
|
|
|
|
end
|
|
|
|
for name, color in pairs(colors) do
|
|
|
|
colors[name] = brightness_modifier(color, brightness_modifier_parameter)
|
|
|
|
end
|
2021-03-02 13:01:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- basic theme defination
|
|
|
|
local M = {
|
|
|
|
normal = {
|
2021-09-03 18:28:20 +00:00
|
|
|
a = { bg = colors.normal, fg = colors.back1, gui = 'bold' },
|
|
|
|
b = { bg = colors.back1, fg = colors.normal },
|
|
|
|
c = { bg = colors.back2, fg = colors.fore },
|
2021-03-02 13:01:34 +00:00
|
|
|
},
|
|
|
|
insert = {
|
2021-09-03 18:28:20 +00:00
|
|
|
a = { bg = colors.insert, fg = colors.back1, gui = 'bold' },
|
|
|
|
b = { bg = colors.back1, fg = colors.insert },
|
|
|
|
c = { bg = colors.back2, fg = colors.fore },
|
2021-03-02 13:01:34 +00:00
|
|
|
},
|
|
|
|
replace = {
|
2021-09-03 18:28:20 +00:00
|
|
|
a = { bg = colors.replace, fg = colors.back1, gui = 'bold' },
|
|
|
|
b = { bg = colors.back1, fg = colors.replace },
|
|
|
|
c = { bg = colors.back2, fg = colors.fore },
|
2021-03-02 13:01:34 +00:00
|
|
|
},
|
|
|
|
visual = {
|
2021-09-03 18:28:20 +00:00
|
|
|
a = { bg = colors.visual, fg = colors.back1, gui = 'bold' },
|
|
|
|
b = { bg = colors.back1, fg = colors.visual },
|
|
|
|
c = { bg = colors.back2, fg = colors.fore },
|
2021-03-02 13:01:34 +00:00
|
|
|
},
|
|
|
|
command = {
|
2021-09-03 18:28:20 +00:00
|
|
|
a = { bg = colors.command, fg = colors.back1, gui = 'bold' },
|
|
|
|
b = { bg = colors.back1, fg = colors.command },
|
|
|
|
c = { bg = colors.back2, fg = colors.fore },
|
|
|
|
},
|
2021-03-02 13:01:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
M.terminal = M.command
|
|
|
|
M.inactive = M.normal
|
|
|
|
|
|
|
|
-- Apply prpper contrast so text is readable
|
|
|
|
for _, section in pairs(M) do
|
2021-09-03 18:28:20 +00:00
|
|
|
for _, highlight in pairs(section) do
|
|
|
|
apply_contrast(highlight)
|
|
|
|
end
|
2021-03-02 13:01:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|