Thread Tools Display Modes
02-28-13, 08:00 AM   #1
Aanson
A Flamescale Wyrmkin
Join Date: Aug 2009
Posts: 124
Get percentage colours.

I've asked a lot of questions over the last few months, so I thought I'd give something back to the community.

Below is a function I've created which utilizes two user created functions which are listed here. (This link leaves wowinterface).

The two functions it uses are RGBPercToHex and ColorGradient and all credit for this function and their original functions go to the original authors.

As an example, if you were monitoring threat as a tank and your current threatPercentage is 80%, you specify colour 1 as red, 2 as yellow and 3 as green...

Lua Code:
  1. myFrame:SetBackdropBorderColor:(ReturnPcColourGradient(threatPercentage, "rgb", 1, 0, 0,  1, 1, 0,  0, 1, 0));

... myFrame's border will now be a colour somewhere between yellow and green. Or...

Lua Code:
  1. print("I have "..ReturnPcColourGradient(threatPercentage, "pc", 1, 0, 0,  1, 1, 0,  0, 1, 0).."|r% of threat");

... 'I have 80% of threat' will be printed (with '80' being a yellowy-green colour).

The function will return one of 3 things based on what is provided in the outputType arg:

if outputType is "rgb", it will return r, g, b.
if outputType is "pc", it will return your percentage value in the appropriate colour.
if outputType is "hex", it will return a hex value only.

I'd imagine that the function will normally always be provided with the colours red, yellow and green. If pc is 100, a bright green will be returned, if it's 0, a bright red will be returned, if it's 50, it'll return yellow. If pc is somewhere between those colours, it'll return the appropriate mix of whichever two colours are required ( ie if pc is 25, it'll mix red and yellow and return an orange colour).

Function code:

Lua Code:
  1. ReturnPcColourGradient = function(pc, outputType, r1, g1, b1, r2, g2, b2, r3, g3, b3)
  2.  
  3.     local red1, green1, blue1, red2, green2, blue2;
  4.     local pc = ( pc / 100 );
  5.         local outputType = ( outputType or "pc" );
  6.  
  7.     if ( pc >= 1 ) then
  8.         red1, green1, blue1 = r3, g3, b3;
  9.     elseif ( pc <= 0 ) then
  10.         red1, green1, blue1 = r1, g1, b1;
  11.     else
  12.  
  13.         local spectrumEnd, relativePc = math.modf( pc * 2 );
  14.  
  15.         if ( spectrumEnd <= 0.999 ) then
  16.             red1, green1, blue1, red2, green2, blue2 = r1, g1, b1, r2, g2, b2;
  17.         else
  18.             red1, green1, blue1, red2, green2, blue2 = r2, g2, b2, r3, g3, b3;
  19.         end
  20.  
  21.         red1, green1, blue1 = ( red1 + ( red2 - red1 ) * relativePc ), ( green1 + ( green2 - green1 ) * relativePc ), ( blue1 + ( blue2 - blue1 ) * relativePc );
  22.  
  23.     end
  24.  
  25.     if lower(outputType):find("rgb") then
  26.         return red1, green1, blue1;
  27.     elseif lower(outputType):find("pc") then
  28.         return "|cFF"..format("%02x%02x%02x", ( red1 * 255 ), ( green1 * 255 ), ( blue1 * 255 ))..( pc * 100 );
  29.     elseif lower(outputType):find("hex") then
  30.         return "|cFF"..format("%02x%02x%02x", ( red1 * 255 ), ( green1 * 255 ), ( blue1 * 255 ));
  31.     end
  32.  
  33.     return red1, green1, blue1;
  34.  
  35. end

To improve the functions performance, it can no longer accept a variable number of colours; you must provide 3.

I hope that this comes in handy to anyone who reads this. Enjoy!


Aanson.

EDIT - Version of func without colour args and tidied up a bit.

I've changed the func a little for scenarios where the colours are always going to be red, yellow, green. I've also edited it in such a way that the only arg it requires now is the percentage value (0/100).

Function code:

Lua Code:
  1. ReturnPcColourGradient = function(pc, outputType)
  2.  
  3.     local r1, g1, b1;
  4.     local pc = ( pc / 100 );
  5.     local outputType = outputType and string.lower(outputType) or "pc";
  6.  
  7.     if ( pc >= 1 ) then
  8.         r1, g1, b1 = 0, 1, 0;
  9.     elseif ( pc <= 0 ) then
  10.         r1, g1, b1 = 1, 0, 0;
  11.     else
  12.  
  13.         local r2, g2, b2;
  14.         local spectrumEnd, relativePc = math.modf( pc * 2 );
  15.  
  16.         if ( spectrumEnd < 1 ) then
  17.             r1, g1, b1, r2, g2, b2 = 1, 0, 0, 1, 1, 0;
  18.         else
  19.             r1, g1, b1, r2, g2, b2 = 1, 1, 0, 0, 1, 0;
  20.         end
  21.  
  22.         r1, g1, b1 = ( r1 + ( r2 - r1 ) * relativePc ), ( g1 + ( g2 - g1 ) * relativePc ), ( b1 + ( b2 - b1 ) * relativePc );
  23.  
  24.     end
  25.  
  26.     if outputType:find("pc") then
  27.         return "|cFF"..format("%02x%02x%02x", ( r1 * 255 ), ( g1 * 255 ), ( b1 * 255 ))..( pc * 100 );
  28.     elseif outputType:find("rgb") then
  29.         return r1, g1, b1;
  30.     end
  31.  
  32.     return "|cFF"..format("%02x%02x%02x", ( r1 * 255 ), ( g1 * 255 ), ( b1 * 255 )); -- hex
  33.  
  34. end

ReturnPcColourGradient(20) is now sufficient to get output: '20' in a dark orange colour.


Aanson
__________________
__________________

Last edited by Aanson : 02-28-13 at 02:17 PM. Reason: New func added with fewer args
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Get percentage colours.

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