Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Interpolation and Color3 help?

I'm trying to make a gui which background changes color, which is randomly generated, and the gui will interpolate between the colors.

But, this is what happens

Inside the color changing script

A = game.Workspace.WallReception:GetChildren()

while true do
  newColor = Color3.new(math.random(0,255)/255,math.random(0,255)/255,math.random(0,255)/255)
	game.Workspace.NewColor.Value = newColor
	wait(10)
end

Inside the GUI/Color Interpolation

local function linearInterpolation(v0, v1, alpha)
    return v0*(1-alpha) + v1*alpha
end

local gui = script.Parent.FrameA
local color0 = script.Parent.FrameA.BackgroundColor3
local color1 = game.Workspace.NewColor.Value

local duration = 1
local elapsed = 0
local alpha = 0

while (alpha < 1) do
	color0 = script.Parent.FrameA.BackgroundColor3
	color1 = game.Workspace.NewColor.Value
    elapsed = elapsed + wait()
    alpha = math.min(elapsed/duration, 1)

    local newColor = Color3.new(
        linearInterpolation(color0.r, color1.r, alpha),
        linearInterpolation(color0.g, color1.g, alpha),
        linearInterpolation(color0.b, color1.b, alpha)
    )
    gui.BackgroundColor3 = newColor
end

eLunate's update:

local nwColor = game.Workspace.NewColor
local gui = script.Parent.FrameA
local color0 = script.Parent.FrameA.BackgroundColor3
local color1 = game.Workspace.NewColor.Value

nwColor.Changed:connect(function()
	script.Parent.FrameA.BackgroundColor3.lerp(color0,color1,1)
end)

1 answer

✓ Accepted answer

According to some very useful sources, Color3:lerp() now exists. You may find it useful to use that, assuming it is enabled currently.

Color3.lerp (
  Color3 startColor,
  Color3 endColor,
  number alpha
) -> Color3 InterpolatedColor

Color3:lerp (
  Color3 endColor,
  number alpha
) -> Color3 InterpolatedColor

3 comments

0

Like CFrame:lerp() but for colors? Cool

0

Huh, didn't work. I'm probably doing something wrong, but please check my update.

0

Your current code will literally just set it to color1

This discussion is preserved as it was. New answers, comments, and votes are closed.