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:
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:
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:
ReturnPcColourGradient = function(pc, outputType, r1, g1, b1, r2, g2, b2, r3, g3, b3)
local red1, green1, blue1, red2, green2, blue2;
local pc = ( pc / 100 );
local outputType = ( outputType or "pc" );
if ( pc >= 1 ) then
red1, green1, blue1 = r3, g3, b3;
elseif ( pc <= 0 ) then
red1, green1, blue1 = r1, g1, b1;
else
local spectrumEnd, relativePc = math.modf( pc * 2 );
if ( spectrumEnd <= 0.999 ) then
red1, green1, blue1, red2, green2, blue2 = r1, g1, b1, r2, g2, b2;
else
red1, green1, blue1, red2, green2, blue2 = r2, g2, b2, r3, g3, b3;
end
red1, green1, blue1 = ( red1 + ( red2 - red1 ) * relativePc ), ( green1 + ( green2 - green1 ) * relativePc ), ( blue1 + ( blue2 - blue1 ) * relativePc );
end
if lower(outputType):find("rgb") then
return red1, green1, blue1;
elseif lower(outputType):find("pc") then
return "|cFF"..format("%02x%02x%02x", ( red1 * 255 ), ( green1 * 255 ), ( blue1 * 255 ))..( pc * 100 );
elseif lower(outputType):find("hex") then
return "|cFF"..format("%02x%02x%02x", ( red1 * 255 ), ( green1 * 255 ), ( blue1 * 255 ));
end
return red1, green1, blue1;
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:
ReturnPcColourGradient = function(pc, outputType)
local r1, g1, b1;
local pc = ( pc / 100 );
local outputType = outputType and string.lower(outputType) or "pc";
if ( pc >= 1 ) then
r1, g1, b1 = 0, 1, 0;
elseif ( pc <= 0 ) then
r1, g1, b1 = 1, 0, 0;
else
local r2, g2, b2;
local spectrumEnd, relativePc = math.modf( pc * 2 );
if ( spectrumEnd < 1 ) then
r1, g1, b1, r2, g2, b2 = 1, 0, 0, 1, 1, 0;
else
r1, g1, b1, r2, g2, b2 = 1, 1, 0, 0, 1, 0;
end
r1, g1, b1 = ( r1 + ( r2 - r1 ) * relativePc ), ( g1 + ( g2 - g1 ) * relativePc ), ( b1 + ( b2 - b1 ) * relativePc );
end
if outputType:find("pc") then
return "|cFF"..format("%02x%02x%02x", ( r1 * 255 ), ( g1 * 255 ), ( b1 * 255 ))..( pc * 100 );
elseif outputType:find("rgb") then
return r1, g1, b1;
end
return "|cFF"..format("%02x%02x%02x", ( r1 * 255 ), ( g1 * 255 ), ( b1 * 255 )); -- hex
end
ReturnPcColourGradient(20) is now sufficient to get output: '20' in a dark orange colour.
Aanson