I’m trying to make a looping gradient like
External Mediaas seen in UIGradientPlus. I don’t want to use that module since it has way more logic than I’ll ever need.
Currently, my code calculates new ColorSequences for each frame to render, and then loops through them.
return function(sequence: ColorSequence)
local cache: { ColorSequence } = { [0] = sequence }
for delta = 1 / 128, 1, 1 / 128 do
local seq = {}
for index, keypoint in sequence.Keypoints do
if keypoint.Time == 1 or keypoint.Time + delta == 0 then
continue
end
seq[#seq + 1] = ColorSequenceKeypoint.new((keypoint.Time + delta) % 1, keypoint.Value)
end
table.sort(seq, function(a, b)
return a.Time < b.Time
end)
table.insert(seq, 1, ColorSequenceKeypoint.new(0, seq[1].Value:Lerp(seq[#seq].Value, delta)))
seq[#seq + 1] = ColorSequenceKeypoint.new(1, seq[1].Value)
cache[delta] = ColorSequence.new(seq)
end
local self = Instance.new("UIGradient")
local loop = coroutine.create(function()
local i = 0
while true do
self.Color = cache[i]
task.wait()
i = (i + 1 / 128) % 1
end
end)
self.Destroying:Connect(function()
coroutine.close(loop)
end)
coroutine.resume(loop)
return self
end
The issue is finding the appropriate values of alpha in my :Lerp() function (near the bottom of the for delta = loop).
Using just delta as the alpha value gets me really close
But there’s a flashing that happens with points in the middle when they go from one side to the other.
I’m invoking my code in this example with
require(gradient)(ColorSequence.new({
ColorSequenceKeypoint.new(0, Color3.new(1)),
ColorSequenceKeypoint.new(0.25, Color3.new(0, 1)),
ColorSequenceKeypoint.new(1, Color3.new(1))
})).Parent = ...
Any ideas? I assume the delta works so well because it’s the proper result when the end keypoint gets close to the border.