HelperLib: add makeGradient function by ssdaniel24. #12
This commit is contained in:
@@ -16,3 +16,42 @@ function Smc.fixColorString(color)
|
|||||||
|
|
||||||
return colorString
|
return colorString
|
||||||
end
|
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
|
||||||
Reference in New Issue
Block a user