Debian Codegraph @

lua/lualine/themes/auto.lua

raw
1 -- Copyright (c) 2020-2021 shadmansaleh
2 -- MIT license, see LICENSE for more details.
3 local utils = require('lualine.utils.utils')
4 local loader = require('lualine.utils.loader')
5
6 local color_name = vim.g.colors_name
7 if color_name then
8 -- All base16 colorschemes share the same theme
9 if 'base16' == color_name:sub(1, 6) then
10 color_name = 'base16'
11 end
12
13 -- Check if there's a theme for current colorscheme
14 -- If there is load that instead of generating a new one
15 local ok, theme = pcall(loader.load_theme, color_name)
16 if ok and theme then
17 return theme
18 end
19 end
20
21 ---------------
22 -- Constants --
23 ---------------
24 -- fg and bg must have this much contrast range 0 < contrast_threshold < 0.5
25 local contrast_threshold = 0.3
26 -- how much brightness is changed in percentage for light and dark themes
27 local brightness_modifier_parameter = 10
28
29 -- Turns #rrggbb -> { red, green, blue }
30 local function rgb_str2num(rgb_color_str)
31 if rgb_color_str:find('#') == 1 then
32 rgb_color_str = rgb_color_str:sub(2, #rgb_color_str)
33 end
34 local red = tonumber(rgb_color_str:sub(1, 2), 16)
35 local green = tonumber(rgb_color_str:sub(3, 4), 16)
36 local blue = tonumber(rgb_color_str:sub(5, 6), 16)
37 return { red = red, green = green, blue = blue }
38 end
39
40 -- Turns { red, green, blue } -> #rrggbb
41 local function rgb_num2str(rgb_color_num)
42 local rgb_color_str = string.format('#%02x%02x%02x', rgb_color_num.red, rgb_color_num.green, rgb_color_num.blue)
43 return rgb_color_str
44 end
45
46 -- Returns brightness level of color in range 0 to 1
47 -- arbitrary value it's basically an weighted average
48 local function get_color_brightness(rgb_color)
49 local color = rgb_str2num(rgb_color)
50 local brightness = (color.red * 2 + color.green * 3 + color.blue) / 6
51 return brightness / 256
52 end
53
54 -- returns average of colors in range 0 to 1
55 -- used to determine contrast level
56 local function get_color_avg(rgb_color)
57 local color = rgb_str2num(rgb_color)
58 return (color.red + color.green + color.blue) / 3 / 256
59 end
60
61 -- Clamps the val between left and right
62 local function clamp(val, left, right)
63 if val > right then
64 return right
65 end
66 if val < left then
67 return left
68 end
69 return val
70 end
71
72 -- Changes brightness of rgb_color by percentage
73 local function brightness_modifier(rgb_color, percentage)
74 local color = rgb_str2num(rgb_color)
75 color.red = clamp(color.red + (color.red * percentage / 100), 0, 255)
76 color.green = clamp(color.green + (color.green * percentage / 100), 0, 255)
77 color.blue = clamp(color.blue + (color.blue * percentage / 100), 0, 255)
78 return rgb_num2str(color)
79 end
80
81 -- Changes contrast of rgb_color by amount
82 local function contrast_modifier(rgb_color, amount)
83 local color = rgb_str2num(rgb_color)
84 color.red = clamp(color.red + amount, 0, 255)
85 color.green = clamp(color.green + amount, 0, 255)
86 color.blue = clamp(color.blue + amount, 0, 255)
87 return rgb_num2str(color)
88 end
89
90 -- Changes brightness of foreground color to achieve contrast
91 -- without changing the color
92 local function apply_contrast(highlight)
93 local highlight_bg_avg = get_color_avg(highlight.bg)
94 local contrast_threshold_config = clamp(contrast_threshold, 0, 0.5)
95 local contrast_change_step = 5
96 if highlight_bg_avg > 0.5 then
97 contrast_change_step = -contrast_change_step
98 end
99
100 -- Don't waste too much time here max 25 iteration should be more than enough
101 local iteration_count = 1
102 while math.abs(get_color_avg(highlight.fg) - highlight_bg_avg) < contrast_threshold_config and iteration_count < 25 do
103 highlight.fg = contrast_modifier(highlight.fg, contrast_change_step)
104 iteration_count = iteration_count + 1
105 end
106 end
107
108 -- Get the colors to create theme
109 -- stylua: ignore
110 local colors = {
111 normal = utils.extract_color_from_hllist('bg', { 'PmenuSel', 'PmenuThumb', 'TabLineSel' }, '#000000'),
112 insert = utils.extract_color_from_hllist('fg', { 'String', 'MoreMsg' }, '#000000'),
113 replace = utils.extract_color_from_hllist('fg', { 'Number', 'Type' }, '#000000'),
114 visual = utils.extract_color_from_hllist('fg', { 'Special', 'Boolean', 'Constant' }, '#000000'),
115 command = utils.extract_color_from_hllist('fg', { 'Identifier' }, '#000000'),
116 back1 = utils.extract_color_from_hllist('bg', { 'Normal', 'StatusLineNC' }, '#000000'),
117 fore = utils.extract_color_from_hllist('fg', { 'Normal', 'StatusLine' }, '#000000'),
118 back2 = utils.extract_color_from_hllist('bg', { 'StatusLine' }, '#000000'),
119 }
120
121 -- Change brightness of colors
122 -- Darken if light theme (or) Lighten if dark theme
123 local normal_color = utils.extract_highlight_colors('Normal', 'bg')
124 if normal_color ~= nil then
125 if get_color_brightness(normal_color) > 0.5 then
126 brightness_modifier_parameter = -brightness_modifier_parameter
127 end
128 for name, color in pairs(colors) do
129 colors[name] = brightness_modifier(color, brightness_modifier_parameter)
130 end
131 end
132
133 -- Basic theme definition
134 local M = {
135 normal = {
136 a = { bg = colors.normal, fg = colors.back1, gui = 'bold' },
137 b = { bg = colors.back1, fg = colors.normal },
138 c = { bg = colors.back2, fg = colors.fore },
139 },
140 insert = {
141 a = { bg = colors.insert, fg = colors.back1, gui = 'bold' },
142 b = { bg = colors.back1, fg = colors.insert },
143 c = { bg = colors.back2, fg = colors.fore },
144 },
145 replace = {
146 a = { bg = colors.replace, fg = colors.back1, gui = 'bold' },
147 b = { bg = colors.back1, fg = colors.replace },
148 c = { bg = colors.back2, fg = colors.fore },
149 },
150 visual = {
151 a = { bg = colors.visual, fg = colors.back1, gui = 'bold' },
152 b = { bg = colors.back1, fg = colors.visual },
153 c = { bg = colors.back2, fg = colors.fore },
154 },
155 command = {
156 a = { bg = colors.command, fg = colors.back1, gui = 'bold' },
157 b = { bg = colors.back1, fg = colors.command },
158 c = { bg = colors.back2, fg = colors.fore },
159 },
160 }
161
162 M.terminal = M.command
163 M.inactive = M.normal
164
165 -- Apply proper contrast so text is readable
166 for _, section in pairs(M) do
167 for _, highlight in pairs(section) do
168 apply_contrast(highlight)
169 end
170 end
171
172 return M