User-created Colour Gradient function - WoWInterface
01-02-14, 05:50 PM   #1
Aanson
A Flamescale Wyrmkin
Join Date: Aug 2009
Posts: 124

Hey all.

I used to use this user-created function which I'd picked up from another wow website:

Lua Code:
`local function ColorGradient(perc, ...)    if perc >= 1 then        local r, g, b = select(select('#', ...) - 2, ...)        return r, g, b    elseif perc <= 0 then        local r, g, b = ...        return r, g, b    end        local num = select('#', ...) / 3     local segment, relperc = math.modf(perc*(num-1))    local r1, g1, b1, r2, g2, b2 = select((segment*3)+1, ...)     return r1 + (r2-r1)*relperc, g1 + (g2-g1)*relperc, b1 + (b2-b1)*relpercend`

I've stripped it to it's bare bones for the sake of speed and simplicity. The three colours which make up it's range are red (0), yellow (50) and green (100). As with the above function, any number passed to the function which is inbetween those numbers will return a colour of the appropriate gradient (ie 25 will return an orangy-red colour):

Lua Code:
`local GetPercentageColourGradient = function(percent)    local _, x = mod(percent * 0.02);    return (percent <= 50) and 1 or (percent >= 100) and 0 or (1 - x), (percent >= 50) and 1 or (percent <= 0) and 0 or x, 0;end`

Hope this comes in handy for someone.

Aanson
__________________
__________________

Last edited by Aanson : 01-02-14 at 05:53 PM.

01-02-14, 06:13 PM   #2
Wimpface
A Molten Giant

Join Date: Oct 2008
Posts: 648
It will come in handy, thanks for sharing!
__________________
All I see is strobe lights blinding me in my hindsight.

01-08-14, 12:31 PM   #3
Aanson
A Flamescale Wyrmkin
Join Date: Aug 2009
Posts: 124
Ooops

Sorry, I'd been mucking about with it before I posted it. 'mod' should be replaced by 'math.modf' for it to work as intended!

Lua Code:
`local GetPercentageColourGradient = function(percent)    local _, x = math.modf(percent * 0.02);    return (percent <= 50) and 1 or (percent >= 100) and 0 or (1 - x), (percent >= 50) and 1 or (percent <= 0) and 0 or x, 0;end`

Aanson
__________________
__________________

01-08-14, 01:37 PM   #4
humfras
A Flamescale Wyrmkin
Join Date: Oct 2009
Posts: 125
CursorCastBar is using a gradient function similar to yours.

The advantage of the extended function is that it will deviate between 3 given colors rather than simply green/yellow/red.
__________________
Author of VuhDo CursorCastBar OptiTaunt Poisoner RaidMobMarker

01-08-14, 02:34 PM   #5
Aanson
A Flamescale Wyrmkin
Join Date: Aug 2009
Posts: 124
 Originally Posted by humfras CursorCastBar is using a gradient function similar to yours. The advantage of the extended function is that it will deviate between 3 given colors rather than simply green/yellow/red.
Yeah absolutely. I had just wanted to put this out there for people who would like to use a colour gradient function relatively often in their code where the only 'scope' needed was green through yellow through red.

It's cleaner and bound to run faster... not much faster I bet, but faster all the same which is going to be essential if it's getting called for AURA events or OnUpdate (when required for a fluid change between gradients) etc.

Health status bars or timers for example.
__________________
__________________

02-07-14, 04:27 AM   #6
suicidalkatt
A Rage Talon Dragon Guard

Join Date: Mar 2008
Posts: 326
Just wanted to point to another thread that had a similar idea: