Quantcast Color Gradients using CreateColor - WoWInterface
Thread Tools Display Modes
06-10-18, 02:59 AM   #1
thomasjohnshannon
A Theradrim Guardian
 
thomasjohnshannon's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 60
Color Gradients using CreateColor

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:
  1. local gradientColor = {
  2.  [0] = CreateColor(0, 1, 0, 1),
  3.  [1] = CreateColor(1, 1, 0, 1),
  4.  [2] = CreateColor(1, 0, 0, 1)
  5. }
  6.  
  7. local function ColorGradient(perc, colors)
  8.     local num = #colors
  9.  
  10.     if ( perc >= 1 ) then
  11.         return colors[num]
  12.     elseif ( perc <= 0 ) then
  13.         return colors[0]
  14.     end
  15.  
  16.     local segment, relperc = modf(perc*num)
  17.  
  18.     local r1, g1, b1, r2, g2, b2
  19.     r1, g1, b1 = colors[segment]:GetRGB()
  20.     r2, g2, b2 = colors[segment+1]:GetRGB()
  21.  
  22.     if ( not r2 or not g2 or not b2 ) then
  23.         return colors[0]
  24.     else
  25.         local r = r1 + (r2-r1)*relperc
  26.         local g = g1 + (g2-g1)*relperc
  27.         local b = b1 + (b2-b1)*relperc
  28.  
  29.         return CreateColor(r, g, b, 1)
  30.     end
  31. 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.
__________________
Thomas aka Urnn

Last edited by thomasjohnshannon : 06-10-18 at 03:21 AM.
  Reply With Quote

WoWInterface » PTR » PTR API and Graphics Changes » Color Gradients using CreateColor

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