Thread Tools Display Modes
09-10-16, 03:00 AM   #1
Aftermathhqt
A Molten Giant
 
Aftermathhqt's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 784
Gradient Color Help

Hi, i'm trying to gradient color my FPS/MS when its dropping but its not working that great!

Its either i only get it to either green or red..

Lua Code:
  1. A.ColorGradient = function(a, b, ...)
  2.     local Percent
  3.  
  4.     if b == 0 then
  5.         Percent = 0
  6.     else
  7.         Percent = a / b
  8.     end
  9.  
  10.     if Percent >= 1 then
  11.         local R, G, B = select(select("#", ...) - 2, ...)
  12.  
  13.         return R, G, B
  14.     elseif Percent <= 0 then
  15.         local R, G, B = ...
  16.  
  17.         return R, G, B
  18.     end
  19.  
  20.     local Num = (select("#", ...) / 3)
  21.     local Segment, RelPercent = modf(Percent * (Num - 1))
  22.     local R1, G1, B1, R2, G2, B2 = select((Segment * 3) + 1, ...)
  23.  
  24.     return R1 + (R2 - R1) * RelPercent, G1 + (G2 - G1) * RelPercent, B1 + (B2 - B1) * RelPercent
  25. end

Lua Code:
  1. A.RGBToHex = function(R, G, B)
  2.     R = R <= 1 and R >= 0 and R or 0
  3.     G = G <= 1 and G >= 0 and G or 0
  4.     B = B <= 1 and B >= 0 and B or 0
  5.  
  6.     return format("|cff%02x%02x%02x", R * 255, G * 255, B * 255)
  7. end

Lua Code:
  1. local AStatsFrame = CreateFrame("Frame", nil, UIParent)
  2. AStatsFrame:SetSize(160, 50)
  3. AStatsFrame:SetPoint("BOTTOMLEFT", UIParent, -35, -15.5)
  4.  
  5. local AStatsText = AStatsFrame:CreateFontString(nil, "OVERLAY")
  6. AStatsText:SetPoint("CENTER", AStatsFrame, 0, 0)
  7. AStatsText:SetFont(C.Media.Font2, 12, "THINOUTLINE")
  8. AStatsText:SetShadowOffset(1, -1)
  9. AStatsText:SetShadowColor(0,0,0)
  10. AStatsText:SetTextColor(A.ClassColor.r, A.ClassColor.g, A.ClassColor.b)
  11.  
  12. local FPS = GetFramerate()
  13. local MS = select(3, GetNetStats())
  14.  
  15. local FR, FG, FB = A.ColorGradient(FPS, 15, 100, .8, 0, .8, .8, 0, .8, 0, 0)
  16. local MR, MG, MB = A.ColorGradient(MS, 1000, 100, .8, 0, .8, .8, 0, .8, 0, 0)
  17.  
  18. local ColorFPS = A.RGBToHex(FR, FG, FB)
  19. local ColorMS = A.RGBToHex(MR, MG, MB)
  20.  
  21. local Int = 0
  22.  
  23. AStatsFrame:SetScript("OnUpdate", function(self, elapsed)
  24.     Int = Int - elapsed
  25.     if Int <= 0 then
  26.  
  27.         local FPS = GetFramerate()
  28.         local MS = select(3, GetNetStats())
  29.        
  30.         --AStatsText:SetText(string.format("%s%d|r fps %s%d|r ms", ColorFPS(FPS), FPS, ColorLag(MS), MS))
  31.         AStatsText:SetText(string.format("%s%d|r fps %s%d|r ms", ColorFPS, FPS, ColorMS, MS))
  32.        
  33.         Int = 1
  34.     end
  35. end)
  Reply With Quote
09-10-16, 05:46 AM   #2
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
This function looks extremely convoluted and not very readable. I don't know if you wrote it yourself, but in any case you should really fix it. I'm pretty sure there's no reason to use ... here, and all it does it make it impossible for an external party to interpret the code. I'm not even going to try to understand it! Just define all the variables, for starters.

That being said, debugging is the first aid of programming! print the values on every line (or at least on two lines) where they change, and you should find out where the logic went wrong soon enough.
Lua Code:
  1. A.ColorGradient = function(a, b, ...)
  2.     local Percent
  3.  
  4.     if b == 0 then
  5.         Percent = 0
  6.     else
  7.         Percent = a / b
  8.     end
  9.  
  10.     if Percent >= 1 then
  11.         local R, G, B = select(select("#", ...) - 2, ...)
  12.  
  13.         return R, G, B
  14.     elseif Percent <= 0 then
  15.         local R, G, B = ...
  16.  
  17.         return R, G, B
  18.     end
  19.  
  20.     local Num = (select("#", ...) / 3)
  21.     local Segment, RelPercent = modf(Percent * (Num - 1))
  22.     local R1, G1, B1, R2, G2, B2 = select((Segment * 3) + 1, ...)
  23.  
  24.     return R1 + (R2 - R1) * RelPercent, G1 + (G2 - G1) * RelPercent, B1 + (B2 - B1) * RelPercent
  25. end
__________________
Grab your sword and fight the Horde!
  Reply With Quote
09-10-16, 08:07 AM   #3
Aftermathhqt
A Molten Giant
 
Aftermathhqt's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 784
Originally Posted by Lombra View Post
This function looks extremely convoluted and not very readable. I don't know if you wrote it yourself, but in any case you should really fix it. I'm pretty sure there's no reason to use ... here, and all it does it make it impossible for an external party to interpret the code. I'm not even going to try to understand it! Just define all the variables, for starters.
That's really arrogant of you, I didn't ask your opinion about the code. Why you even bother to post something..
  Reply With Quote
09-10-16, 09:22 AM   #4
Ketho
A Pyroguard Emberseer
 
Ketho's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,026
Game92, I honestly think Lombra is giving constructive feedback
  Reply With Quote
09-10-16, 09:35 AM   #5
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
I bothered to post because I wanted to help you figure out how to troubleshoot things on your own, and also to make it easier for people to help you, which honestly is not easy with that function. Variables should have meaningful names, and by using ... you hide almost all parameters, making it very hard to follow the flow.

I can guarantee you that you will have forgotten what this code does before long:
Code:
    local Num = (select("#", ...) / 3)
    local Segment, RelPercent = modf(Percent * (Num - 1))
    local R1, G1, B1, R2, G2, B2 = select((Segment * 3) + 1, ...)
 
    return R1 + (R2 - R1) * RelPercent, G1 + (G2 - G1) * RelPercent, B1 + (B2 - B1) * RelPercent
If you refuse to take any of this to heart, then at least use this:
Originally Posted by Lombra View Post
That being said, debugging is the first aid of programming! print the values on every line (or at least on two lines) where they change, and you should find out where the logic went wrong soon enough.
Debugging is an extremely simple and useful method of troubleshooting. Everyone uses it.

For example, you could do something like this.
Lua Code:
  1. A.ColorGradient = function(a, b, ...)
  2.     local Percent
  3.  
  4.     if b == 0 then
  5.         Percent = 0
  6.     else
  7.         Percent = a / b
  8.     end
  9.  
  10.     print(Percent) -- does the Percent value look fine here?
  11.     if Percent >= 1 then
  12.         local R, G, B = select(select("#", ...) - 2, ...)
  13.  
  14.         print(R, G, B) -- are these the expected color values?
  15.         return R, G, B
  16.     elseif Percent <= 0 then
  17.         local R, G, B = ...
  18.  
  19.         print(R, G, B) -- are these the expected color values?
  20.         return R, G, B
  21.     end
  22.  
  23.     local Num = (select("#", ...) / 3)
  24.     local Segment, RelPercent = modf(Percent * (Num - 1))
  25.     local R1, G1, B1, R2, G2, B2 = select((Segment * 3) + 1, ...)
  26.     print(R1, G1, B1, R2, G2, B2) -- are these the expected color values?
  27.  
  28.     return R1 + (R2 - R1) * RelPercent, G1 + (G2 - G1) * RelPercent, B1 + (B2 - B1) * RelPercent
  29. end

Edit: I may have been needlessly hostile, and for that I apologize. I stand by my opinion that the function code is not at all readable, however. Don't use varargs (...) nor select unless you have a reason to do so, use meaningful variable names, and I would also say the last few lines are a bit too "mathy", but you may be forced to change that if you don't use varargs anyway, I suppose.
__________________
Grab your sword and fight the Horde!

Last edited by Lombra : 09-10-16 at 09:42 AM.
  Reply With Quote
09-10-16, 02:22 PM   #6
Aftermathhqt
A Molten Giant
 
Aftermathhqt's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 784
Originally Posted by Lombra View Post
Edit: I may have been needlessly hostile, and for that I apologize. I stand by my opinion that the function code is not at all readable, however. Don't use varargs (...) nor select unless you have a reason to do so, use meaningful variable names, and I would also say the last few lines are a bit too "mathy", but you may be forced to change that if you don't use varargs anyway, I suppose.
No worries. Thanks for the tips though.
  Reply With Quote
09-11-16, 01:32 AM   #7
zork
A Pyroguard Emberseer
 
zork's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 1,740
You can do this with the ColorSelect frame. It has HSV colors.

Lua Code:
  1. local CS = CreateFrame("ColorSelect")
  2.      
  3.     --GetSmudgeColorRGB function
  4.     --arg1: color table in RGB {r=0,g=0,b=0}
  5.     --arg2: color table in RGB {r=1,g=1,b=1}
  6.     --arg3: percentage 0-1
  7.     function CS:GetSmudgeColorRGB(colorA,colorB,percentage)
  8.       self:SetColorRGB(colorA.r, colorA.g, colorA.b)
  9.       colorA.h, colorA.s, colorA.v = self:GetColorHSV()
  10.       self:SetColorRGB(colorB.r, colorB.g, colorB.b)
  11.       colorB.h, colorB.s, colorB.v = self:GetColorHSV()
  12.       local colorC = {}
  13.       --check if the angle between the two H values is > 180
  14.       if abs(colorA.h-colorB.h) > 180 then
  15.         local angle = (360-abs(colorA.h-colorB.h))*percentage
  16.         if colorA.h < colorB.h then
  17.           colorC.h = floor(colorA.h-angle)
  18.           if colorC.h < 0 then
  19.             colorC.h = 360+colorC.h
  20.           end
  21.         else
  22.           colorC.h = floor(colorA.h+angle)
  23.           if colorC.h > 360 then
  24.             colorC.h = colorC.h-360
  25.           end
  26.         end
  27.       else
  28.         colorC.h = floor(colorA.h-(colorA.h-colorB.h)*percentage)
  29.       end    
  30.       colorC.s = colorA.s-(colorA.s-colorB.s)*percentage
  31.       colorC.v = colorA.v-(colorA.v-colorB.v)*percentage
  32.       self:SetColorHSV(colorC.h, colorC.s, colorC.v)
  33.       colorC.r, colorC.g, colorC.b = self:GetColorRGB()
  34.       return colorC
  35.     end
  36.      
  37.     --GetSmudgeColorRGB function call
  38.     local color = CS:GetSmudgeColorRGB({r=1,g=0,b=0}, {r=0,g=1,b=0}, 0.5)

http://www.wowinterface.com/download...lorSmudge.html
__________________
| Simple is beautiful.
| WoWI AddOns | GitHub | Zork (WoW)

"I wonder what the non-pathetic people are doing tonight?" - Rajesh Koothrappali (The Big Bang Theory)
  Reply With Quote
09-11-16, 03:15 AM   #8
Aftermathhqt
A Molten Giant
 
Aftermathhqt's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 784
Originally Posted by zork View Post
You can do this with the ColorSelect frame. It has HSV colors.

Lua Code:
  1. local CS = CreateFrame("ColorSelect")
  2.      
  3.     --GetSmudgeColorRGB function
  4.     --arg1: color table in RGB {r=0,g=0,b=0}
  5.     --arg2: color table in RGB {r=1,g=1,b=1}
  6.     --arg3: percentage 0-1
  7.     function CS:GetSmudgeColorRGB(colorA,colorB,percentage)
  8.       self:SetColorRGB(colorA.r, colorA.g, colorA.b)
  9.       colorA.h, colorA.s, colorA.v = self:GetColorHSV()
  10.       self:SetColorRGB(colorB.r, colorB.g, colorB.b)
  11.       colorB.h, colorB.s, colorB.v = self:GetColorHSV()
  12.       local colorC = {}
  13.       --check if the angle between the two H values is > 180
  14.       if abs(colorA.h-colorB.h) > 180 then
  15.         local angle = (360-abs(colorA.h-colorB.h))*percentage
  16.         if colorA.h < colorB.h then
  17.           colorC.h = floor(colorA.h-angle)
  18.           if colorC.h < 0 then
  19.             colorC.h = 360+colorC.h
  20.           end
  21.         else
  22.           colorC.h = floor(colorA.h+angle)
  23.           if colorC.h > 360 then
  24.             colorC.h = colorC.h-360
  25.           end
  26.         end
  27.       else
  28.         colorC.h = floor(colorA.h-(colorA.h-colorB.h)*percentage)
  29.       end    
  30.       colorC.s = colorA.s-(colorA.s-colorB.s)*percentage
  31.       colorC.v = colorA.v-(colorA.v-colorB.v)*percentage
  32.       self:SetColorHSV(colorC.h, colorC.s, colorC.v)
  33.       colorC.r, colorC.g, colorC.b = self:GetColorRGB()
  34.       return colorC
  35.     end
  36.      
  37.     --GetSmudgeColorRGB function call
  38.     local color = CS:GetSmudgeColorRGB({r=1,g=0,b=0}, {r=0,g=1,b=0}, 0.5)

http://www.wowinterface.com/download...lorSmudge.html
Cheers, will try!
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Gradient Color Help


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