WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   PTR API and Graphics Changes (https://www.wowinterface.com/forums/forumdisplay.php?f=175)
-   -   Color Gradients using CreateColor (https://www.wowinterface.com/forums/showthread.php?t=56274)

thomasjohnshannon 06-10-18 02:59 AM

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.


All times are GMT -6. The time now is 03:30 PM.

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