Laggy ColorSequence.new?

I made a simple pixel editing script using UIGradients, and while it does work, I noticed how laggy ColorSequence.new() is the bigger the canvas I use (noticeable if the canvas is tall). So I tried looping the thing in a RenderStepped event, 256 times (how wide my canvas is in pixels) every time it fires

When I tried making a one-keypoint ColorSequence, I get less than 1% of script activity. But the second I added two keypoints or more, activity shoots up to around 11%. I don’t know why is it that slow.

How do I get around this?

Looks like your trying to draw pixels, I would recommend greedy canvas to optimize it.

I haven’t made one of these yet so apologies if it dosent work

Well, I was using UIGradients already, and my previous attempts for greedy meshing were failures. If they did even work, the performance was sucky, seeing that GUIs seem to be laggier than parts.

The current method I was using was faster after reading about optimizations stuff, but the bottleneck looks to be creating many ColorSequences.

Read the Performance Considerations section here:
https://developer.roblox.com/en-us/api-reference/class/UIGradient

Your issue with adding more keypoints is because you should be using the least amount possible. Above it recommends using less than 6

Thanks for letting me know about this, but that did not make drawing any faster even when I made the script limit ColorSequence keypoints to 5. As I’ve said, when looping 256 times, making one keypoint in a ColorSequence was instant, but as soon as I made two keypoints, the activity spiked.

Show the code for how you’re looping

Code for rendering:

-- Designations - a nested array for caching frames with UIGradients
-- CSeqNew - ColorSequenceKeypoint.new()
-- To_Color3 - takes a table with 3 numbers ({16, 2, 13}) and converts to Color3.
-- Compare - compares two tables with 3 numbers each.
-- 
local function Recolor_Strip(Index, Colors, Y)
	local Col_Num = #Colors
	
	if Col_Num == 0 then
		return false
	end
	
	local Strip = Designations[Y][Index]
	
	if not Strip then
		error("Gradient strip doesn't exist.")
	end
	
	local Gradient = Strip:FindFirstChildOfClass("UIGradient")
	
	local Proportion = 1 / Col_Num
	
	local StartKey = CSeqNew(0, To_Color3(Colors[1]))
	local EndKey = CSeqNew(1, To_Color3(Colors[Col_Num]))
	
	local Keys = {StartKey}
	
	for CIndex = 1, Col_Num - 1 do
		local IndexM1 = CIndex + 1
		
		local Previous = Colors[CIndex]
		local Current = Colors[IndexM1]
		
		local PreviousColor = To_Color3(Previous)
		local CurrentColor = To_Color3(Current)

		if Compare(Previous, Current) then
			continue
		end
		
		local PC_Key = CSeqNew(
			(Proportion * CIndex) - Tiny_Not_Zero,
			PreviousColor
		)
		local CC_Key = CSeqNew(
			(Proportion * CIndex) + Tiny_Not_Zero,
			CurrentColor
		)

		table.insert(Keys, PC_Key)
		table.insert(Keys, CC_Key)

		--print(Proportion * IndexM1)
	end
	
	table.insert(Keys, EndKey)
	
	local NewSeq = ColorSequence.new(Keys) -- laggy
	
	if Gradient.Color ~= NewSeq then
		Gradient.Color = NewSeq
	end
	if Strip.Parent == script then
		Strip.Parent = Canvas
	end
	
	--Canvas.TextLabel.Text = "Objects: " .. #Canvas:GetChildren() - 1
end

And a quickly made test function to check how laggy ColorSequence.new is:

Run_Service.RenderStepped:Connect(function()
	for _ = 1, Canvas_PX_Size.X do.
		local Col = ColorSequenceKeypoint.new(0, To_Color3(
			{1,
				2,
				3}
			)
		)
		local Col2 = ColorSequenceKeypoint.new(1, To_Color3(
			{4,
				5,
				6}
			)
		)
		local Col3 = ColorSequenceKeypoint.new(1, To_Color3(
			{7,
				8,
				9}
			)
		)
		local Col4 = ColorSequenceKeypoint.new(1, To_Color3(
			{10,
				11,
				12}
			)
		)
		local Col5 = ColorSequenceKeypoint.new(1, To_Color3(
			{13,
				14,
				15}
			)
		)
		local ColEnd = ColorSequence.new({Col, Col2, Col3, Col4, Col5})
	end
end)