WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Tutorials & Other Helpful Info. (https://www.wowinterface.com/forums/forumdisplay.php?f=12)
-   -   User-created Colour Gradient function (https://www.wowinterface.com/forums/showthread.php?t=48754)

Aanson 01-02-14 05:50 PM

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

Wimpface 01-02-14 06:13 PM

It will come in handy, thanks for sharing!

Aanson 01-08-14 12:31 PM

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

humfras 01-08-14 01:37 PM

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.

Aanson 01-08-14 02:34 PM

Quote:

Originally Posted by humfras (Post 289396)
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.

suicidalkatt 02-07-14 04:27 AM

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


All times are GMT -6. The time now is 12:31 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI