Reply
Thread Tools Display Modes
Unread 01-02-14, 05:50 PM   #1
Aanson
A Flamescale Wyrmkin
Join Date: Aug 2009
Posts: 123
User-created Colour Gradient function

Hey all.

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

Lua Code:
  1. local function ColorGradient(perc, ...)
  2.     if perc >= 1 then
  3.         local r, g, b = select(select('#', ...) - 2, ...)
  4.         return r, g, b
  5.     elseif perc <= 0 then
  6.         local r, g, b = ...
  7.         return r, g, b
  8.     end
  9.    
  10.     local num = select('#', ...) / 3
  11.  
  12.     local segment, relperc = math.modf(perc*(num-1))
  13.     local r1, g1, b1, r2, g2, b2 = select((segment*3)+1, ...)
  14.  
  15.     return r1 + (r2-r1)*relperc, g1 + (g2-g1)*relperc, b1 + (b2-b1)*relperc
  16. end

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:
  1. local GetPercentageColourGradient = function(percent)
  2.     local _, x = mod(percent * 0.02);
  3.     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;
  4. end

Hope this comes in handy for someone.

Aanson
__________________
__________________

Last edited by Aanson : 01-02-14 at 05:53 PM.
Aanson is offline   Reply With Quote
Unread 01-02-14, 06:13 PM   #2
Wimpface
A Molten Giant
 
Wimpface's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 643
It will come in handy, thanks for sharing!
__________________
All I see is strobe lights blinding me in my hindsight.
Wimpface is offline   Reply With Quote
Unread 01-08-14, 12:31 PM   #3
Aanson
A Flamescale Wyrmkin
Join Date: Aug 2009
Posts: 123
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:
  1. local GetPercentageColourGradient = function(percent)
  2.     local _, x = math.modf(percent * 0.02);
  3.     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;
  4. end

Aanson
__________________
__________________
Aanson is offline   Reply With Quote
Unread 01-08-14, 01:37 PM   #4
humfras
A Warpwood Thunder Caller
AddOn Author - Click to view addons
Join Date: Oct 2009
Posts: 98
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
humfras is offline   Reply With Quote
Unread 01-08-14, 02:34 PM   #5
Aanson
A Flamescale Wyrmkin
Join Date: Aug 2009
Posts: 123
Originally Posted by humfras View Post
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.
__________________
__________________
Aanson is offline   Reply With Quote
Unread 02-07-14, 04:27 AM   #6
suicidalkatt
A Rage Talon Dragon Guard
 
suicidalkatt's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 316
Just wanted to point to another thread that had a similar idea:

http://www.wowinterface.com/forums/s...ad.php?t=48236

Here we came to a completed addon that creates a very smooth transition from one color to another in the shortest possible way.

Zork being awesome came up with this: ColorSmudge
suicidalkatt is offline   Reply With Quote
Reply

Go BackWoWInterface » Developer Discussions » Tutorials & Other Helpful Info. » User-created Colour Gradient function

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off