我试图让倒数计时器(已经有部分计算出来)在5秒内从深红色转换为浅绿色。
颜色可以由3个值的组合表示,每个值的范围为0-255。这些值表示颜色的RGB值。第一个数字表示颜色为“多少”红色,第二个表示绿色,第三个表示蓝色。
以下是RGB颜色的一些示例,
您可以使用Color3在Roblox中构建颜色。有一个构造函数可以使用Color3.fromRGB(number r, number g, number b)
轻松地将RGB颜色转换为Color3。因此,如果你想在5秒内从深红色(50,0,0)到亮绿色(100,255,100),你可以使用Color3:lerp(Color3 targetColor, number alpha)
。
local startingColor = Color3.fromRGB(50, 0, 0) -- dark red
local targetColor = Color3.fromRGB(100, 255, 100) -- bright green
local testBrick = Instance.new("Part", game.workspace)
testBrick.Color = startingColor
-- make a loop to change the color, lerp() expects a number between 0 - 1.
local timeToComplete = 5.0 -- seconds
local framesPerSecond = 60.0
local waitAmt = 1.0 / framesPerSecond
local stepAmt = 1.0 / (timeToComplete * framesPerSecond)
local i = 0.0
while (i < 1.0)
testBrick.Color = startingColor:lerp(targetColor, i)
i = i + stepAmt
wait(waitAmt)
end
-- by now the color should be close to the target color, ensure it is done
testBrick.Color = targetColor
在引擎盖下,这只是做一些简单的线性代数,将3个值从起点转换到终点。希望这可以帮助。