api: add and use rgbhex function

* A memoizing method to cache and return hex colorstrings from rgb input.
* Refactor all occurences of adhoc rgb to hex string concatenation to use the new method
* Refactor chat module to use it
This commit is contained in:
Road-block
2019-05-01 15:23:05 +02:00
committed by shagu
parent 0ea4d54f24
commit cf4c9ac361
8 changed files with 58 additions and 28 deletions
+41 -8
View File
@@ -87,7 +87,7 @@ function pfUI.api.GetUnitColor(unitstr)
r, g, b = RAID_CLASS_COLORS[class].r, RAID_CLASS_COLORS[class].g, RAID_CLASS_COLORS[class].b
end
return string.format("|cff%02x%02x%02x", r * 255, g * 255, b * 255), r, g, b
return pfUI.api.rgbhex(r,g,b), r, g, b
end
-- [ strvertical ]
@@ -680,7 +680,7 @@ function pfUI.api.SetAutoPoint(frame, parent, spacing)
end
end
-- [ GetDefaultColors ]
-- [ GetStringColor ]
-- Queries the pfUI setting strings and extract its color codes
-- returns r,g,b,a
local color_cache = {}
@@ -692,6 +692,39 @@ function pfUI.api.GetStringColor(colorstr)
return unpack(color_cache[colorstr])
end
-- [ rgbhex ]
-- Returns color format from color info
-- 'r' [table or number] color table or r color component
-- 'g' [number] optional g color component
-- 'b' [number] optional b color component
-- 'a' [number] optional alpha component
-- returns color string in the form of '|caaxxyyzz'
local hexcolor_cache = {}
function pfUI.api.rgbhex(r, g, b, a)
local key
if type(r)=="table" then
local _r,_g,_b,_a
if r.r then
_r,_g,_b,_a = r.r, r.g, r.b, r.a or 1
elseif table.getn(r) >= 3 then
_r,_g,_b,_a = r[1], r[2], r[3], r[4] or 1
end
if _r and _g and _b and _a then
key = string.format("%s%s%s%s",_r,_g,_b,_a)
if hexcolor_cache[key] == nil then
hexcolor_cache[key] = string.format("|c%02x%02x%02x%02x", _a*255, _r*255, _g*255, _b*255)
end
end
elseif tonumber(r) and g and b then
a = a or 1
key = string.format("%s%s%s%s",r,g,b,a)
if hexcolor_cache[key] == nil then
hexcolor_cache[key] = string.format("|c%02x%02x%02x%02x", a*255, r*255, g*255, b*255)
end
end
return hexcolor_cache[key] or ""
end
-- [ Create Backdrop ]
-- Creates a pfUI compatible frame as backdrop element
-- 'f' [frame] the frame which should get a backdrop.
@@ -887,19 +920,19 @@ function pfUI.api.GetColoredTimeString(remaining)
if not remaining then return "" end
if remaining > 99 * 60 * 60 then
local r,g,b,a = pfUI.api.GetStringColor(C.appearance.cd.daycolor)
return "|cff" .. string.format("%02x%02x%02x", r*255, g*255, b*255) .. round(remaining / 60 / 60 / 24) .. "|rd"
return pfUI.api.rgbhex(r,g,b) .. round(remaining / 60 / 60 / 24) .. "|rd"
elseif remaining > 99 * 60 then
local r,g,b,a = pfUI.api.GetStringColor(C.appearance.cd.hourcolor)
return "|cff" .. string.format("%02x%02x%02x", r*255, g*255, b*255) .. round(remaining / 60 / 60) .. "|rh"
return pfUI.api.rgbhex(r,g,b) .. round(remaining / 60 / 60) .. "|rh"
elseif remaining > 99 then
local r,g,b,a = pfUI.api.GetStringColor(C.appearance.cd.minutecolor)
return "|cff" .. string.format("%02x%02x%02x", r*255, g*255, b*255) .. round(remaining / 60) .. "|rm"
return pfUI.api.rgbhex(r,g,b) .. round(remaining / 60) .. "|rm"
elseif remaining <= 5 then
local r,g,b,a = pfUI.api.GetStringColor(C.appearance.cd.lowcolor)
return "|cff" .. string.format("%02x%02x%02x", r*255, g*255, b*255) .. string.format("%.1f", round(remaining,1))
return pfUI.api.rgbhex(r,g,b) .. string.format("%.1f", round(remaining,1))
elseif remaining >= 0 then
local r, g, b, a = pfUI.api.GetStringColor(C.appearance.cd.normalcolor)
return "|cff" .. string.format("%02x%02x%02x", r*255, g*255, b*255) .. round(remaining)
return pfUI.api.rgbhex(r,g,b) .. round(remaining)
else
return ""
end
@@ -928,7 +961,7 @@ function pfUI.api.GetColorGradient(perc)
local r = r1 + (r2 - r1)*perc
local g = g1 + (g2 - g1)*perc
local b = b1 + (b2 - b1)*perc
local h = string.format("|cff%02x%02x%02x", r*255, g*255, b*255)
local h = pfUI.api.rgbhex(r,g,b)
gradientcolors[perc] = {}
gradientcolors[perc].r = r
+2 -2
View File
@@ -1412,7 +1412,7 @@ function pfUI.uf:RefreshUnit(unit, component)
pfUI.uf:AddIcon(unit, pos, icon)
end
-- hide unued icon slots
-- hide unused icon slots
for pos=table.getn(active)+1, 6 do
pfUI.uf:HideIcon(unit, pos)
end
@@ -2062,5 +2062,5 @@ function pfUI.uf.GetColor(self, preset)
g = ( g + .75 ) * .5
b = ( b + .75 ) * .5
return "|cff" .. string.format("%02x%02x%02x", r*255, g*255, b*255)
return rgbhex(r,g,b)
end
+1 -1
View File
@@ -53,7 +53,7 @@ local getFontString = function(obj)
segment = segment and segment ~= "" and segment or nil
if segment then
r, g, b, a = left:GetTextColor()
segment = "|c" .. string.format("ff%02x%02x%02x", r*255, g*255, b*255) .. segment .. "|r"
segment = rgbhex(r,g,b) .. segment .. "|r"
text = text and text .. "\n" .. segment or segment
end
end
+2 -2
View File
@@ -64,8 +64,8 @@ pfUI:RegisterModule("afkcam", 20400, function ()
local class = GetUnitData(arg2 or "")
local author, msg
if class and class ~= UNKNOWN then
local class_color = string.format("%02x%02x%02x", RAID_CLASS_COLORS[class].r * 255, RAID_CLASS_COLORS[class].g * 255, RAID_CLASS_COLORS[class].b * 255)
author = string.format("|cff%s%s|r",class_color,arg2)
local class_color = rgbhex(RAID_CLASS_COLORS[class])
author = string.format("%s%s|r",class_color,arg2)
else
author = arg2
end
+1 -1
View File
@@ -315,7 +315,7 @@ pfUI:RegisterModule("buffwatch", function ()
sr, sg, sb, sa = 1, .3, .3, 1
end
if frame.config.colorstacks == "1" then
frame.bars[bar].stacks:SetText("|cff" .. string.format("%02x%02x%02x", sr*255, sg*255, sb*255) .. data[5])
frame.bars[bar].stacks:SetText(rgbhex(sr,sg,sb) .. data[5])
else
frame.bars[bar].stacks:SetText(data[5])
end
+9 -12
View File
@@ -601,7 +601,7 @@ pfUI:RegisterModule("chat", 20400, function ()
-- read and parse whisper color settings
local cr, cg, cb, ca = strsplit(",", C.chat.global.whisper)
cr, cg, cb = tonumber(cr), tonumber(cg), tonumber(cb)
local wcol = string.format("%02x%02x%02x",cr * 255,cg * 255, cb * 255)
local wcol = rgbhex(cr, cg, cb)
-- read and parse chat bracket settings
local left = "|r" .. string.sub(C.chat.text.bracket, 1, 1)
@@ -626,15 +626,15 @@ pfUI:RegisterModule("chat", 20400, function ()
_G.CHAT_YELL_GET = left .. "Y" .. right ..default
if C.chat.global.whispermod == "1" then
_G.CHAT_WHISPER_GET = '|cff' .. wcol .. '[W]' .. default
_G.CHAT_WHISPER_GET = wcol .. '[W]' .. default
_G.CHAT_WHISPER_INFORM_GET = '[W]' .. default
end
local r,g,b,a = strsplit(",", C.chat.text.timecolor)
local timecolorhex = string.format("%02x%02x%02x%02x", a*255, r*255, g*255, b*255)
local timecolorhex = rgbhex(r,g,b,a)
local r,g,b = strsplit(",", C.chat.text.unknowncolor)
local unknowncolorhex = string.format("%02x%02x%02x", r*255, g*255, b*255)
local unknowncolorhex = rgbhex(r,g,b)
for i=1,NUM_CHAT_WINDOWS do
if not _G["ChatFrame"..i].HookAddMessage then
@@ -668,17 +668,14 @@ pfUI:RegisterModule("chat", 20400, function ()
local class = GetUnitData(name)
if class then
if class ~= UNKNOWN then
color = string.format("%02x%02x%02x",
RAID_CLASS_COLORS[class].r * 255,
RAID_CLASS_COLORS[class].g * 255,
RAID_CLASS_COLORS[class].b * 255)
color = rgbhex(RAID_CLASS_COLORS[class])
match = true
end
end
if C.chat.text.tintunknown == "1" or match then
text = string.gsub(text, "|Hplayer:"..name.."|h%["..name.."%]|h(.-:-)",
left.."|cff"..color.."|Hplayer:"..name.."|h" .. name .. "|h|r"..right.."%1")
left..color.."|Hplayer:"..name.."|h" .. name .. "|h|r"..right.."%1")
end
end
end
@@ -695,13 +692,13 @@ pfUI:RegisterModule("chat", 20400, function ()
-- show timestamp in chat
if C.chat.text.time == "1" then
text = "|c" .. timecolorhex .. tleft .. date(C.chat.text.timeformat) .. tright .. "|r " .. text
text = timecolorhex .. tleft .. date(C.chat.text.timeformat) .. tright .. "|r " .. text
end
if C.chat.global.whispermod == "1" then
-- patch incoming whisper string to match the colors
if string.find(text, '|cff'..wcol, 1) == 1 then
text = string.gsub(text, "|r", "|r|cff" .. wcol)
if string.find(text, wcol, 1) == 1 then
text = string.gsub(text, "|r", "|r" .. wcol)
end
end
+1 -1
View File
@@ -133,7 +133,7 @@ pfUI:RegisterModule("chatcopy", 20400, function ()
local AddMessage = frame.AddMessage
frame.AddMessage = function(self,msg,r,g,b)
if msg and r and g and b then
local col = string.format("|cff%02x%02x%02x", r*255, g*255, b*255)
local col = rgbhex(r,g,b)
table.insert(history, 1, col..string.gsub(msg, "|r", col))
elseif msg then
table.insert(history, 1, "|cffffffff"..msg)
+1 -1
View File
@@ -75,7 +75,7 @@ pfUI:RegisterModule("socialmod", function ()
local lcolor = GetDifficultyColor(tonumber(level)) or { 1, 1, 1 }
zone = ( zone == playerzone and "|cffffffff" or "|cffaaaaaa" ) .. zone .. "|r"
name = "|cff" .. string.format("%02x%02x%02x", ccolor.r*255, ccolor.g*255, ccolor.b*255) .. name .. "|r"
name = RgbHex(ccolor) .. name .. "|r"
_G["FriendsFrameFriendButton"..i..FRIENDS_NAME_LOCATION]:SetText(format(TEXT(FRIENDS_LIST_TEMPLATE), name, zone, status))
_G["FriendsFrameFriendButton"..i.."ButtonTextInfo"]:SetText(format(TEXT(FRIENDS_LEVEL_TEMPLATE), level, class))