I have been updating some of the addons I use and decided to see if I could update the color gradient function that most people use to CreateColor. While it isn't a new thing Blizzard is continuing to move towards using it in their own code so I figured I would get with the times. The new version seems to work so I decided to share it here.
Once you get the color object back you can use any of the mixin functions such as GetRGB(), GetRGBA(), or GenerateHexColor().
Lua Code:
local gradientColor = {
[0] = CreateColor(0, 1, 0, 1),
[1] = CreateColor(1, 1, 0, 1),
[2] = CreateColor(1, 0, 0, 1)
}
local function ColorGradient(perc, colors)
local num = #colors
if ( perc >= 1 ) then
return colors[num]
elseif ( perc <= 0 ) then
return colors[0]
end
local segment, relperc = modf(perc*num)
local r1, g1, b1, r2, g2, b2
r1, g1, b1 = colors[segment]:GetRGB()
r2, g2, b2 = colors[segment+1]:GetRGB()
if ( not r2 or not g2 or not b2 ) then
return colors[0]
else
local r = r1 + (r2-r1)*relperc
local g = g1 + (g2-g1)*relperc
local b = b1 + (b2-b1)*relperc
return CreateColor(r, g, b, 1)
end
end
PS: If this is the wrong place to post this or someone has already done something like this I apologize in advanced but I felt like sharing this bit of code.