Getting a color3 value from a color sequence

hi, i need to get a color3 value from a color sequence in a specific position.


i want to get the color from the black arrow,

the script:

local part = workspace.part

local function GetColor(Time, Sequence) 
	--Get the color from the sequence
	--time is the position on the sequence
end

local Color1 =  Color3.fromRGB(math.random(0,255),math.random(0,255),math.random(0,255))
local Color2 = Color3.fromRGB(math.random(0,255),math.random(0,255),math.random(0,255))
local PointColor = ColorSequence.new(Color1,Color2) --Color sequence
local Pos = 0.3 --time

part.color3 = GetColor(Pos, PointColor)

what do put on the function GetColor?
btw the color sequence is only made up by 2 colors

1 Like

On the ColorSequence documentation there’s an example on how to do exactly what you’re looking for.

local function evalColorSequence(sequence: ColorSequence, time: number)
    -- If time is 0 or 1, return the first or last value respectively
    if time == 0 then
        return sequence.Keypoints[1].Value
    elseif time == 1 then
        return sequence.Keypoints[#sequence.Keypoints].Value
    end

    -- Otherwise, step through each sequential pair of keypoints
    for i = 1, #sequence.Keypoints - 1 do
        local thisKeypoint = sequence.Keypoints[i]
        local nextKeypoint = sequence.Keypoints[i + 1]
        if time >= thisKeypoint.Time and time < nextKeypoint.Time then
            -- Calculate how far alpha lies between the points
            local alpha = (time - thisKeypoint.Time) / (nextKeypoint.Time - thisKeypoint.Time)
            -- Evaluate the real value between the points using alpha
            return Color3.new(
                (nextKeypoint.Value.R - thisKeypoint.Value.R) * alpha + thisKeypoint.Value.R,
                (nextKeypoint.Value.G - thisKeypoint.Value.G) * alpha + thisKeypoint.Value.G,
                (nextKeypoint.Value.B - thisKeypoint.Value.B) * alpha + thisKeypoint.Value.B
            )
        end
    end
end

local colorSequence = ColorSequence.new{
    ColorSequenceKeypoint.new(0, Color3.fromRGB(255, 0, 0)),
    ColorSequenceKeypoint.new(0.5, Color3.fromRGB(0, 190, 200)),
    ColorSequenceKeypoint.new(1, Color3.fromRGB(190, 0, 255))
}

print(evalColorSequence(colorSequence, 0.75))  --> 0.372549, 0.372549, 0.892157

Reference:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.