A Pyroguard Emberseer
Join Date: Jul 2008
Posts: 1,740
|
First set yourself up some color functions that you can use in tags later on.
Color functions
Lua Code:
-------------------------------------- --VARIABLES -------------------------------------- local PowerBarColor = PowerBarColor local RAID_CLASS_COLORS = RAID_CLASS_COLORS local FACTION_BAR_COLORS = FACTION_BAR_COLORS local abs, floor, format = abs, floor, format -------------------------------------- --COLOR LIB -------------------------------------- --gradient color lib local CS = CreateFrame("ColorSelect") --get HSV from RGB color function CS:GetHSVColor(color) self:SetColorRGB(color.r, color.g, color.b) local h,s,v = self:GetColorHSV() return {h=h,s=s,v=v} end --get RGB from HSV color function CS:GetRGBColor(color) self:SetColorHSV(color.h, color.s, color.v) local r,g,b = self:GetColorRGB() return {r=r,g=g,b=b} end --input two HSV colors and a percentage --returns new HSV color function CS:GetSmudgeColorHSV(colorA,colorB,percentage) local colorC = {} --check if the angle between the two H values is > 180 if abs(colorA.h-colorB.h) > 180 then local angle = (360-abs(colorA.h-colorB.h))*percentage if colorA.h < colorB.h then colorC.h = floor(colorA.h-angle) if colorC.h < 0 then colorC.h = 360+colorC.h end else colorC.h = floor(colorA.h+angle) if colorC.h > 360 then colorC.h = colorC.h-360 end end else colorC.h = floor(colorA.h-(colorA.h-colorB.h)*percentage) end colorC.s = colorA.s-(colorA.s-colorB.s)*percentage colorC.v = colorA.v-(colorA.v-colorB.v)*percentage return colorC end local redColor = CS:GetHSVColor({r=1,g=0,b=0}) local greenColor = CS:GetHSVColor({r=0,g=1,b=0}) -------------------------------------- --GET RGB as HEX-Color -------------------------------------- local function GetHexColor(color) return format("%02x%02x%02x", color.r*255, color.g*255, color.b*255) end -------------------------------------- --COLOR FUNCTIONS -------------------------------------- --GetPowerTypeColor func local function GetPowerTypeColor(unit) if not unit then return end local id, power, r, g, b = UnitPowerType(unit) local color = PowerBarColor[power] if color then r, g, b = color.r, color.g, color.b end return {r=r,g=g,b=b} end --GetLevelColor func local function GetLevelColor(unit) if not unit then return end return GetQuestDifficultyColor(unit) end --GetUnitColor func local function GetUnitColor(unit) if not unit then return end local color if UnitIsDeadOrGhost(unit) or not UnitIsConnected(unit) then color = {r = 0.5, g = 0.5, b = 0.5} elseif UnitIsTapped(unit) and not UnitIsTappedByPlayer(unit) then color = {r = 0.5, g = 0.5, b = 0.5} elseif UnitIsPlayer(unit) then color = RAID_CLASS_COLORS[select(2, UnitClass(unit))] else color = FACTION_BAR_COLORS[UnitReaction(unit, "player")] end return color end --GetHealthColor func local function GetHealthColor(unit) if not unit then return end local hcur, hmax = UnitHealth(unit), UnitHealthMax(unit) local hper = 0 if hmax > 0 then hper = hcur/hmax end --you may need to swap red and green color return CS:GetRGBColor(CS:GetSmudgeColorHSV(redColor,greenColor,hper)) end
After that you set up 4 tags for UnitLevel, UnitName, UnitPower and UnitHealth coloring.
Lua Code:
-------------------------------------- --TAGS -------------------------------------- --unit name tag oUF.Tags.Methods["unit:name"] = function(unit) local name = oUF.Tags.Methods["name"](unit) local color GetUnitColor(unit) if color then return "|cff"..GetHexColor(color)..name.."|r" else return "|cffff00ff"..name.."|r" end end oUF.Tags.Events["unit:name"] = "UNIT_NAME_UPDATE UNIT_CONNECTION" --unit level tag oUF.Tags.Methods["unit:level"] = function(unit) local level = oUF.Tags.Methods["level"](unit) local color GetLevelColor(unit) if color then return "|cff"..GetHexColor(color)..level.."|r" else return "|cffff00ff"..level.."|r" end end oUF.Tags.Events["unit:level"] = "UNIT_NAME_UPDATE UNIT_CONNECTION" --unit health tag oUF.Tags.Methods["unit:health"] = function(unit) local perhp = oUF.Tags.Methods["perhp"](unit) local color GetHealthColor(unit) if color then return "|cff"..GetHexColor(color)..perhp.."|r" else return "|cffff00ff"..perhp.."|r" end end oUF.Tags.Events["unit:health"] = "UNIT_HEALTH_FREQUENT UNIT_MAXHEALTH" --unit power tag oUF.Tags.Methods["unit:power"] = function(unit) local perpp = oUF.Tags.Methods["perpp"](unit) local color GetPowerColor(unit) if color then return "|cff"..GetHexColor(color)..perpp.."|r" else return "|cffff00ff"..perpp.."|r" end end oUF.Tags.Events["unit:power"] = "UNIT_DISPLAYPOWER UNIT_POWER_FREQUENT UNIT_MAXPOWER"
Once you have the tags set up you can easily bind them to any font string. So in your unit style you set up 4 font strings.
Lua Code:
-------------------------------------- --FONT STRINGS -------------------------------------- --fontstring func local function NewFontString(parent,family,size,outline,layer) local fs = parent:CreateFontString(nil, layer or "OVERLAY") fs:SetFont(family,size,outline) fs:SetShadowColor(0,0,0,1) return fs end local function CreateUnitStrings(self) local name = NewFontString(self, STANDARD_TEXT_FONT, 12, "THINOUTLINE") name:SetPoint("CENTER", self, 0, 10) self:Tag(name, "[unit:name]") local hpval = NewFontString(self, STANDARD_TEXT_FONT, 12, "THINOUTLINE") hpval:SetPoint("RIGHT", name, 5,0) self:Tag(hpval, "[unit:health]") local level = NewFontString(self, STANDARD_TEXT_FONT, 12, "THINOUTLINE") level:SetPoint("CENTER", self, 0, -10) self:Tag(level, "[unit:level]") local ppval = NewFontString(self, STANDARD_TEXT_FONT, 12, "THINOUTLINE") ppval:SetPoint("RIGHT", level, 5,0) self:Tag(ppval, "[unit:power]") end
Last part is to set up a unit template function and spawn your two units:
Lua Code:
-------------------------------------- --STYLE TEMPLATE FUNC -------------------------------------- --style func local function CreateUnitTemplate(self) --create the unit strings CreateUnitStrings(self) end -------------------------------------- --UNIT SPAWN -------------------------------------- --register the style function oUF:RegisterStyle("DefaultTemplate", CreateUnitTemplate) oUF:SetActiveStyle("DefaultTemplate") --spawn player oUF:Spawn("player","DefaultTemplate"):SetPoint("CENTER", UIParent, -100, 0) --spawn target oUF:Spawn("target","DefaultTemplate"):SetPoint("CENTER", UIParent, 100, 0)
-- Bringing everything together --------------------
Thus the final result would be the following. Careful though I drycoded the whole thing. There may be typos.
Lua Code:
-------------------------------------- --VARIABLES -------------------------------------- local PowerBarColor = PowerBarColor local RAID_CLASS_COLORS = RAID_CLASS_COLORS local FACTION_BAR_COLORS = FACTION_BAR_COLORS local abs, floor, format = abs, floor, format -------------------------------------- --COLOR LIB -------------------------------------- --gradient color lib local CS = CreateFrame("ColorSelect") --get HSV from RGB color function CS:GetHSVColor(color) self:SetColorRGB(color.r, color.g, color.b) local h,s,v = self:GetColorHSV() return {h=h,s=s,v=v} end --get RGB from HSV color function CS:GetRGBColor(color) self:SetColorHSV(color.h, color.s, color.v) local r,g,b = self:GetColorRGB() return {r=r,g=g,b=b} end --input two HSV colors and a percentage --returns new HSV color function CS:GetSmudgeColorHSV(colorA,colorB,percentage) local colorC = {} --check if the angle between the two H values is > 180 if abs(colorA.h-colorB.h) > 180 then local angle = (360-abs(colorA.h-colorB.h))*percentage if colorA.h < colorB.h then colorC.h = floor(colorA.h-angle) if colorC.h < 0 then colorC.h = 360+colorC.h end else colorC.h = floor(colorA.h+angle) if colorC.h > 360 then colorC.h = colorC.h-360 end end else colorC.h = floor(colorA.h-(colorA.h-colorB.h)*percentage) end colorC.s = colorA.s-(colorA.s-colorB.s)*percentage colorC.v = colorA.v-(colorA.v-colorB.v)*percentage return colorC end local redColor = CS:GetHSVColor({r=1,g=0,b=0}) local greenColor = CS:GetHSVColor({r=0,g=1,b=0}) -------------------------------------- --GET RGB as HEX-Color -------------------------------------- local function GetHexColor(color) return format("%02x%02x%02x", color.r*255, color.g*255, color.b*255) end -------------------------------------- --COLOR FUNCTIONS -------------------------------------- --GetPowerTypeColor func local function GetPowerTypeColor(unit) if not unit then return end local id, power, r, g, b = UnitPowerType(unit) local color = PowerBarColor[power] if color then r, g, b = color.r, color.g, color.b end return {r=r,g=g,b=b} end --GetLevelColor func local function GetLevelColor(unit) if not unit then return end return GetQuestDifficultyColor(unit) end --GetUnitColor func local function GetUnitColor(unit) if not unit then return end local color if UnitIsDeadOrGhost(unit) or not UnitIsConnected(unit) then color = {r = 0.5, g = 0.5, b = 0.5} elseif UnitIsTapped(unit) and not UnitIsTappedByPlayer(unit) then color = {r = 0.5, g = 0.5, b = 0.5} elseif UnitIsPlayer(unit) then color = RAID_CLASS_COLORS[select(2, UnitClass(unit))] else color = FACTION_BAR_COLORS[UnitReaction(unit, "player")] end return color end --GetHealthColor func local function GetHealthColor(unit) if not unit then return end local hcur, hmax = UnitHealth(unit), UnitHealthMax(unit) local hper = 0 if hmax > 0 then hper = hcur/hmax end --you may need to swap red and green color return CS:GetRGBColor(CS:GetSmudgeColorHSV(redColor,greenColor,hper)) end -------------------------------------- --TAGS -------------------------------------- --unit name tag oUF.Tags.Methods["unit:name"] = function(unit) local name = oUF.Tags.Methods["name"](unit) local color GetUnitColor(unit) if color then return "|cff"..GetHexColor(color)..name.."|r" else return "|cffff00ff"..name.."|r" end end oUF.Tags.Events["unit:name"] = "UNIT_NAME_UPDATE UNIT_CONNECTION" --unit level tag oUF.Tags.Methods["unit:level"] = function(unit) local level = oUF.Tags.Methods["level"](unit) local color GetLevelColor(unit) if color then return "|cff"..GetHexColor(color)..level.."|r" else return "|cffff00ff"..level.."|r" end end oUF.Tags.Events["unit:level"] = "UNIT_NAME_UPDATE UNIT_CONNECTION" --unit health tag oUF.Tags.Methods["unit:health"] = function(unit) local perhp = oUF.Tags.Methods["perhp"](unit) local color GetHealthColor(unit) if color then return "|cff"..GetHexColor(color)..perhp.."|r" else return "|cffff00ff"..perhp.."|r" end end oUF.Tags.Events["unit:health"] = "UNIT_HEALTH_FREQUENT UNIT_MAXHEALTH" --unit power tag oUF.Tags.Methods["unit:power"] = function(unit) local perpp = oUF.Tags.Methods["perpp"](unit) local color GetPowerColor(unit) if color then return "|cff"..GetHexColor(color)..perpp.."|r" else return "|cffff00ff"..perpp.."|r" end end oUF.Tags.Events["unit:power"] = "UNIT_DISPLAYPOWER UNIT_POWER_FREQUENT UNIT_MAXPOWER" -------------------------------------- --FONT STRINGS -------------------------------------- --fontstring func local function NewFontString(parent,family,size,outline,layer) local fs = parent:CreateFontString(nil, layer or "OVERLAY") fs:SetFont(family,size,outline) fs:SetShadowColor(0,0,0,1) return fs end local function CreateUnitStrings(self) local name = NewFontString(self, STANDARD_TEXT_FONT, 12, "THINOUTLINE") name:SetPoint("CENTER", self, 0, 10) self:Tag(name, "[unit:name]") local hpval = NewFontString(self, STANDARD_TEXT_FONT, 12, "THINOUTLINE") hpval:SetPoint("RIGHT", name, 5,0) self:Tag(hpval, "[unit:health]") local level = NewFontString(self, STANDARD_TEXT_FONT, 12, "THINOUTLINE") level:SetPoint("CENTER", self, 0, -10) self:Tag(level, "[unit:level]") local ppval = NewFontString(self, STANDARD_TEXT_FONT, 12, "THINOUTLINE") ppval:SetPoint("RIGHT", level, 5,0) self:Tag(ppval, "[unit:power]") end -------------------------------------- --STYLE TEMPLATE FUNC -------------------------------------- --style func local function CreateUnitTemplate(self) --create the unit strings CreateUnitStrings(self) end -------------------------------------- --UNIT SPAWN -------------------------------------- --register the style function oUF:RegisterStyle("DefaultTemplate", CreateUnitTemplate) oUF:SetActiveStyle("DefaultTemplate") --spawn player oUF:Spawn("player","DefaultTemplate"):SetPoint("CENTER", UIParent, -100, 0) --spawn target oUF:Spawn("target","DefaultTemplate"):SetPoint("CENTER", UIParent, 100, 0)
__________________
| Simple is beautiful.
| WoWI AddOns | GitHub | Zork (WoW)
"I wonder what the non-pathetic people are doing tonight?" - Rajesh Koothrappali (The Big Bang Theory)
|
Last edited by zork : 10-21-13 at 06:34 AM.
|