Rainbow RGB Algorithm?

I have looked around for this quite a bit but could only find HSV Rainbow Codes. I am looking for a Color3.fromRGB algorithm. Help would be much appreciated!

Are you trying to find a RGB Value for a rainbow? You can change the color after every second or so.

No? Like this How would I go about making a rainbow cycling brick? - #5 by The_PieDude Only using Color3.fromRGB

1 Like


num is a number between 0 and 1. I wrote the function based on this rgb color wheel picture I found on the internet. But why don’t you want to use Color3.fromHSV?

local function getPositiveAngleDiff(a, b)
    return a < b and a+360-b or a-b
end

-- returns r, g and b
local function getRainbowRGB(num)
    local angle = num*360
    local components = {}
    for i = 1, 3 do
        local startAngle = ((i+1)*120)%360
        local diffFromStart = getPositiveAngleDiff(angle, startAngle)
        if diffFromStart < 60 then
            components[i] = diffFromStart/60*255
        elseif diffFromStart <= 180 then
            components[i] = 255
        elseif diffFromStart < 240 then
            components[i] = (240-diffFromStart)/60*255
        else
            components[i] = 0
        end
    end
    return components[1], components[2], components[3]
end
2 Likes

So the problem is that Different Parts of the rainbow are different mixtures of rgb.

color: rgb: color3 : hue
Red: 255,0,0 : 1,0,0 : 0
Orange: 255,165,0 : 1,.5,0 : 29
Yellow: 255,255,0 : 1,1,0 : 60
Green: 0,255,0 : 0,1,0 : 120
Blue: 0,0,255 : 0,0,1 : 240
Purple: 165,0,255 : 0.5,0,1 : 270

Edit: the hue numbers are wrong but it’s still the same concept

Notice how the hue just keeps going up but the rgb doesn’t really have an easy pattern? That’s why everyone just uses hue to do a rainbow effect.

If you really need to use from rgb could you tell us why so that we can help?

In my game I wanted to make rainbow tags in chat and here’s most of the code that does that:

-- from: https://devforum.roblox.com/t/how-to-create-a-simple-rainbow-effect-using-tweenservice/221849/2
local t = 10; --how long does it take to go through the rainbow
local r = math.random() * t --so that anytime we use this all rainbows start at different values
while wait() do
   local hue = (tick()+r) % t / t
   local color = Color3.fromHSV(hue, 1, 1)
   TagLabel.TextColor3 = color
end
1 Like