Last active
July 21, 2024 16:55
-
-
Save rakkarage/a8795397eabb8ee075094d3c0e115d8e to your computer and use it in GitHub Desktop.
kgPanels wow addon functions for changing a gradient based on health color
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Early exit if SetGradient method is not available | |
if not self.bg.SetGradient then return end | |
-- Early exit if target does not exist | |
if not UnitExists('focus') then return end | |
-- Calculate focus health percentage | |
local healthPercent = UnitHealth('focus') / UnitHealthMax('focus') | |
-- Initialize color variables for gradient start and end | |
local startColor = {r = 1, g = 0, b = 0, a = 0.8} -- Red with 80% opacity | |
local endColor = {r = 1, g = 1, b = 0, a = 0.5} -- Yellow with 50% opacity | |
-- Adjust colors based on health percentage | |
if healthPercent > 0.5 then | |
-- Adjust for the upper half of the health range | |
healthPercent = (healthPercent - 0.5) * 2 | |
startColor = {r = 1, g = 1, b = 0, a = 0.5} -- Yellow with 50% opacity | |
endColor = {r = 0, g = 0, b = 0, a = 0.25} -- Black with 25% opacity | |
else | |
-- Adjust for the lower half of the health range | |
healthPercent = healthPercent * 2 | |
end | |
-- Calculate final gradient colors based on health percentage | |
local finalColor = { | |
r = startColor.r + (endColor.r - startColor.r) * healthPercent, | |
g = startColor.g + (endColor.g - startColor.g) * healthPercent, | |
b = startColor.b + (endColor.b - startColor.b) * healthPercent, | |
a = startColor.a + (endColor.a - startColor.a) * healthPercent | |
} | |
-- Apply the calculated gradient to the background | |
self.bg:SetGradient("HORIZONTAL", finalColor, { r = 0, g = 0, b = 0, a = 0 }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
self:RegisterEvent("UNIT_HEALTH") | |
self:RegisterEvent("PLAYER_FOCUS_CHANGED") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Early exit if SetGradient method is not available | |
if not self.bg.SetGradient then return end | |
-- Calculate player health percentage | |
local healthPercent = UnitHealth('player') / UnitHealthMax('player') | |
-- Initialize color variables for gradient start and end | |
local startColor = {r = 1, g = 0, b = 0, a = 0.8} -- Red with 80% opacity | |
local endColor = {r = 1, g = 1, b = 0, a = 0.5} -- Yellow with 50% opacity | |
-- Adjust colors based on health percentage | |
if healthPercent > 0.5 then | |
-- Adjust for the upper half of the health range | |
healthPercent = (healthPercent - 0.5) * 2 | |
startColor = {r = 1, g = 1, b = 0, a = 0.5} -- Yellow with 50% opacity | |
endColor = {r = 0, g = 0, b = 0, a = 0.25} -- Black with 25% opacity | |
else | |
-- Adjust for the lower half of the health range | |
healthPercent = healthPercent * 2 | |
end | |
-- Calculate final gradient colors based on health percentage | |
local finalColor = { | |
r = startColor.r + (endColor.r - startColor.r) * healthPercent, | |
g = startColor.g + (endColor.g - startColor.g) * healthPercent, | |
b = startColor.b + (endColor.b - startColor.b) * healthPercent, | |
a = startColor.a + (endColor.a - startColor.a) * healthPercent | |
} | |
-- Apply the calculated gradient to the background | |
self.bg:SetGradient("HORIZONTAL", {r = 0, g = 0, b = 0, a = 0}, finalColor) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
self:RegisterEvent("UNIT_HEALTH") | |
self:RegisterEvent("PLAYER_ENTERING_WORLD") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Early exit if SetGradient method is not available | |
if not self.bg.SetGradient then return end | |
-- Early exit if target does not exist | |
if not UnitExists('target') then return end | |
-- Calculate target health percentage | |
local healthPercent = UnitHealth('target') / UnitHealthMax('target') | |
-- Initialize color variables for gradient start and end | |
local startColor = {r = 1, g = 0, b = 0, a = 0.8} -- Red with 80% opacity | |
local endColor = {r = 1, g = 1, b = 0, a = 0.5} -- Yellow with 50% opacity | |
-- Adjust colors based on health percentage | |
if healthPercent > 0.5 then | |
-- Adjust for the upper half of the health range | |
healthPercent = (healthPercent - 0.5) * 2 | |
startColor = {r = 1, g = 1, b = 0, a = 0.5} -- Yellow with 50% opacity | |
endColor = {r = 0, g = 0, b = 0, a = 0.25} -- Black with 25% opacity | |
else | |
-- Adjust for the lower half of the health range | |
healthPercent = healthPercent * 2 | |
end | |
-- Calculate final gradient colors based on health percentage | |
local finalColor = { | |
r = startColor.r + (endColor.r - startColor.r) * healthPercent, | |
g = startColor.g + (endColor.g - startColor.g) * healthPercent, | |
b = startColor.b + (endColor.b - startColor.b) * healthPercent, | |
a = startColor.a + (endColor.a - startColor.a) * healthPercent | |
} | |
-- Apply the calculated gradient to the background | |
self.bg:SetGradient("HORIZONTAL", finalColor, { r = 0, g = 0, b = 0, a = 0 }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
self:RegisterEvent("UNIT_HEALTH") | |
self:RegisterEvent("PLAYER_TARGET_CHANGED") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment