I have always implemented my own way of having colorschemes, earlier I used to do something like this
The main thing here is the line local colors = require('themes.colorschemes.' .. opts.theme).get_colors(). Observe that we have to require the theme file everytime. Instead of doing this everytime we can compile the colors into a cache file so that it can be called faster the next time. This is what we will try to implement in this blog. At the end I will also teach you how to make a custom telescope prompt for selecting your custom themes
Setup
In this setup we will create some files required for this. Make a directory .config/nvim/lua/themes. Throughout this blog, all the files would be in this directory
init.lua
Explaination of the blend function:
It takes in three parametres:
foreground : string - in the form of hex
background : string - also in the form of hex
alpha : float - between 0 and 1, representing the blending alpha or transparency level.
The purpose of this function is to blend the foreground color over the background color with the given transparency level (alpha).
If alpha is a string, it assumes it’s a hexadecimal color string and converts it to a numeric value by dividing it by 0xff (255). If alpha is a number, it uses it as is.
It then converts both the foreground and background colors from hexadecimal format to RGB format using a helper function called hexToRgb which is responsible for converting a hexadecimal color string to an RGB color representation.
The function defines another inner function called blendChannel(i) which blends the color channels (red, green, and blue) individually based on the alpha value and returns the blended channel value. It ensures that the resulting channel value is clamped between 0 and 255 and rounds it to the nearest integer.
Finally, the function constructs a new hexadecimal color string using the blended RGB color channels and returns it.
Finally we will also have a function to fetch the themes colors for setting the highlights
scheme
let us make a file at lua/themes/schemes/onedark.lua
for highlights, we will create folder lua/themes/hls. In later sections, we will create a function that will loop through all the files in the repo, so you can name files in it however you want. But each file should return a table with highlights. For example for the dashboard -
Instead of manually typing out all the highlights, get them all from here
implementation
now let us actually start implementing what we were here for, colorscheme caching. All of this code is in themes/init.lua
Explaination:
M.mergeTb: This function merges multiple tables into one using vim.tbl_deep_extend("force", ...). It takes any number of tables as arguments and returns a single merged table.
M.loadTb: This function loads a highlights table from a file.
making cache
this function creates the cache and saves the file at ~/.local/share/nvim/colors_data/
Explaination:
M.tableToStr: This function converts a table of color scheme definitions into a string that can be written to a Lua file. It iterates over the table and its sub-tables, converting the data into a format suitable for use with vim.api.nvim_set_hl.
M.toCache: This function takes a filename and a table of color scheme definitions, converts the table into a Lua code string using M.tableToStr, and writes this code to a cache file. The resulting file is written in binary mode to the path specified by vim.g.theme_cache.
applying
Explaination:
M.compile: This function compiles color scheme definitions into cache files. It checks if the cache directory specified in vim.g.theme_cache exists and creates it if it doesn’t. It then iterates through a list of color scheme files, converts them to Lua code using M.toCache, and writes them to cache files.
M.load: This function loads cached color scheme definitions into Neovim. It first compiles the color schemes using M.compile and then iterates through the cache files in the cache directory, using dofile to load each one into Neovim
This is it basically, now the
last step
create a file ~/.config/nvim/colors/onedark.lua
and now just run this command:
bonus - telescope picker
now if you have multiple schemes in themes/schemes, we can create a telescope picker for just them. For this you will first need to install Telescope