From 2bdae282fa374346975e36a8bccbb1b961f6336e Mon Sep 17 00:00:00 2001 From: Leon Heidelbach Date: Sat, 4 Jun 2022 20:51:10 +0200 Subject: [PATCH] feat: compute a gradient between two hex colors --- lua/colors.lua | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lua/colors.lua b/lua/colors.lua index 2ad0114..af07655 100644 --- a/lua/colors.lua +++ b/lua/colors.lua @@ -160,4 +160,28 @@ M.change_hex_lightness = function(hex, percent) return M.hsl2hex(h, s, l) end +-- Compute a gradient between two colors +-- @param hex1 The first hex color value +-- @param hex2 The second hex color value +-- @param steps The number of steps to compute +-- @return A table of hex color values +M.compute_gradient = function(hex1, hex2, steps) + local h1, s1, l1 = M.hex2hsl(hex1) + local h2, s2, l2 = M.hex2hsl(hex2) + local h, s, l + local h_step = (h2 - h1) / (steps - 1) + local s_step = (s2 - s1) / (steps - 1) + local l_step = (l2 - l1) / (steps - 1) + local gradient = {} + + for i = 0, steps - 1 do + h = h1 + (h_step * i) + s = s1 + (s_step * i) + l = l1 + (l_step * i) + gradient[i + 1] = M.hsl2hex(h, s, l) + end + + return gradient +end + return M