ROBLOX Color3
objects use the RGB color model.
A "rainbow" is parameterized in terms of hue which is not a component of the RGB color model, and which ROBLOX doesn't directly support.
There are several color models supporting hue -- HSL, HSV/HSB. Here I will describe HSV.
The Hue - Saturation - Value Color Model
Here is the Wikipedia article I am referencing
For a fully saturated (vivid color) rainbow, we use saturation S
as 1 and value (brightness) V
as 1 as well.
We thus get chroma C
as C = S * V
.
For a hue measuring between 0 and 360 degrees, we define a scaled-down version
We use the intermediate value X
to be the following:
1 | X = C * ( 1 - math.abs( (Hp % 2 ) - 1 ) ) |
From here, we have an unfortunately complicated transformation into RGB that models the zigzags in this figure.
1 | local f = math.floor(Hp) + 1 |
3 | local Cp = { 1 , 2 , 2 , 3 , 3 , 1 } |
4 | local Xp = { 2 , 1 , 3 , 2 , 1 , 3 } |
9 | R 1 , G 1 , B 1 = unpack (color) |
We now add an amount to each to give it its value (brightness)
2 | R,G,B = R 1 + m, G 1 + m, B 1 + m |
I have confirmed that this works!
Altogether here's a function to do it, and a rainbow maker:
01 | function RGBfromHSV(H,S,V) |
04 | X = C * ( 1 - math.abs( (Hp % 2 ) - 1 ) ) |
05 | local f = math.floor(Hp) + 1 |
07 | local Cp = { 1 , 2 , 2 , 3 , 3 , 1 } |
08 | local Xp = { 2 , 1 , 3 , 2 , 1 , 3 } |
12 | R 1 , G 1 , B 1 = unpack (color) |
14 | return R 1 + m, G 1 + m, B 1 + m |
21 | table.insert(rainbow, Color 3. new( RGBfromHSV(i, 1 , 1 ) ) ) |