Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
6

How can I transition between random colors? [closed]

Asked by
Zerio920 285 Moderation Voter
9 years ago

I can use

    PointLight.Color = Color3.new(math.random(),math.random(),math.random())

to generate random light colors. But if I try it again, it might give me a vastly different shade than what it picked before. How can I create a smooth gradient-like transition between one random color to the next?

Locked by adark, TofuBytes, and evaera

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

2 answers

Log in to vote
6
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

This is called interpolating (smoothly moving between two options).

We first generate both results, and then figure out how far in time we are between the two. If we're 80% through the transitioning time, we figure out a way to be 80% towards the second color.


The simplest way to interpolate is called linear interpolation. It's called linear since the rate is constant through the transition, so the graph of any of the values is a line.


If we use ROBLOX's Vector3s to store the colors, it becomes a little briefer to do the math, so that's what I'll do.

begin = Vector3.new(math.random(),math.random(),math.random())
finish = Vector3.new(math.random(),math.random(),math.random())

startTime = tick()
duration = 5 -- seconds

while tick() - startTime < duration do
    local progress = (tick() - startTime) / duration
    -- progress is now between 0 (completely `begin` color)
    -- and 1 (completely `end` color)
    local intermediate = finish * progress + ( 1 - progress ) * begin
    -- The above is just a "weighted average" of `finish` and `begin`
    local color = Color3.new(intermediate.x, intermediate.y, intermediate.z)
    -- Use color3 for whatever
    wait()
end

To make this loop, we just have to make a new finish but move finish into begin:

begin, finish = finish, Vector3.new(math.random(),math.random(),math.random())
-- follow the previous snippet,
-- then wrap the whole thing in a `while true do` loop
0
You can use Color3 Objects directly, since you can access the components as c3.r, c3.g, and c3.b. Directly equivalent to v3.x, v3.y, v3.z adark 5487 — 9y
2
Yes, but you lose the ability to use + and *, meaning the math becomes 3 times longer than necessary. BlueTaslem 18071 — 9y
Ad
Log in to vote
-4
Answered by 9 years ago

First off, you never described where PointLight is Do either

local PL = game.Workspace.PointLight

so for BrickColor all you would do is

PL.BrickColor = BrickColor:Random()

But you want a PointLight Color so try this ( not sure if this is correct, just my guess )

PL.Color = Color:Random()