Submitter: BubbleCow
As I'm normally using fully-filled squares, the 4-step coloring
from full to low is sometimes too inaccurate for me. This sample
code provides a gradient fading from one threshold color to
another.
Notes:
-
in this sample it would always be enabled
-
not working on invert-bars
-
i'm new to lua so it's ... well a sample :)
-
ticketsystem seems to strip some things ...
-
SquaredUnitFrame.lua @ 295 (v2.7.2)
function SquaredUnitFrame:SetHealthFillColor()
local hpPercent = self.curval / self.maxval * 100
if not Squared.GetSetting("invert-bars") then
if hpPercent <= Squared.GetSetting("hp-low") then
WindowSetTintColor(self:GetName().."ForeFill", unpack(Squared.GetSetting("colorgroup-hplow")))
elseif hpPercent <= Squared.GetSetting("hp-medium") then
local ratio = self:GetRelativeRatio(Squared.GetSetting("hp-medium"), Squared.GetSetting("hp-low"), hpPercent)
local myColor = self:GetGradientColor(Squared.GetSetting("colorgroup-hpmedium"), Squared.GetSetting("colorgroup-hplow"), ratio)
WindowSetTintColor(self:GetName().."ForeFill", unpack(myColor))
elseif hpPercent <= Squared.GetSetting("hp-high") then
local ratio = self:GetRelativeRatio(Squared.GetSetting("hp-high"), Squared.GetSetting("hp-medium"), hpPercent)
local myColor = self:GetGradientColor(Squared.GetSetting("colorgroup-hphigh"), Squared.GetSetting("colorgroup-hpmedium"), ratio)
WindowSetTintColor(self:GetName().."ForeFill", unpack(myColor))
else
local ratio = self:GetRelativeRatio(100, Squared.GetSetting("hp-high"), hpPercent)
local myColor = self:GetGradientColor(self.fgcolor Squared.GetSetting("colorgroup-hphigh"), ratio)
WindowSetTintColor(self:GetName().."ForeFill", unpack(myColor))
end
else
if hpPercent <= Squared.GetSetting("hp-low") then
WindowSetTintColor(self:GetName().."BackFill", unpack(Squared.GetSetting("colorgroup-hplow")))
elseif hpPercent <= Squared.GetSetting("hp-medium") then
WindowSetTintColor(self:GetName().."BackFill", unpack(Squared.GetSetting("colorgroup-hpmedium")))
elseif hpPercent <= Squared.GetSetting("hp-high") then
WindowSetTintColor(self:GetName().."BackFill", unpack(Squared.GetSetting("colorgroup-hphigh")))
else
WindowSetTintColor(self:GetName().."BackFill", unpack(self.fgcolor
end
end
end
function SquaredUnitFrame:GetRelativeRatio(windowStart, windowStop, absolutePercent)
local windowSize = windowStop - windowStart
local localPosition = absolutePercent - windowStart
return (localPosition/windowSize) or 1
end
function SquaredUnitFrame:GetGradientColor(startColor, stopColor, ratio)
local gradientColor = {0, 0, 0}
gradientColor[1] = startColor[1] + ((stopColor[1] - startColor[1]) * (ratio))
gradientColor[2] = startColor[2] + ((stopColor[2] - startColor[2]) * (ratio))
gradientColor[3] = startColor[3] + ((stopColor[3] - startColor[3]) * (ratio))
return gradientColor
end