Thread Tools Display Modes
Prev Previous Post   Next Post Next
01-02-14, 05:50 PM   #1
Aanson
A Flamescale Wyrmkin
Join Date: Aug 2009
Posts: 124
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.
  Reply With Quote
 

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


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