HelperLib: add makeGradient function by ssdaniel24. #12

This commit is contained in:
Van
2026-01-15 15:18:21 +03:00
parent 43bee3dae0
commit cabe562dc4

View File

@@ -15,4 +15,43 @@ function Smc.fixColorString(color)
end end
return colorString return colorString
end
---Makes gradient on given text with many of core.colorize.
---@param colorFrom ColorString
---@param colorTo ColorString
---@param msg string text
---@return string
function Smc.makeGradient(colorFrom, colorTo, msg)
-- Original feature author: @ssdaniel24 (github)
msg = core.strip_colors(msg)
local colorFromTable = core.colorspec_to_table(colorFrom)
local colorToTable = core.colorspec_to_table(colorTo)
if colorFromTable == nil or colorToTable == nil then
error('Incorrect color')
end
local gradientStepTable = core.colorspec_to_table('#00000000') or {}
for k, _ in pairs(gradientStepTable) do
gradientStepTable[k] = (colorToTable[k] - colorFromTable[k]) / string.len(msg)
end
local result = ''
local currentColorTable = table.copy(colorFromTable)
for i = 1,#msg do
local char = msg:sub(i,i)
local color = core.colorspec_to_colorstring(currentColorTable) or ''
result = result .. core.colorize(color, char)
for k,_ in pairs(gradientStepTable) do
currentColorTable[k] = currentColorTable[k] + gradientStepTable[k]
end
end
return result
end end