diff --git a/api/api.lua b/api/api.lua index 3cda60de..056cbd04 100644 --- a/api/api.lua +++ b/api/api.lua @@ -849,61 +849,37 @@ end -- [ GetColorGradient ] -- -- 'perc' percentage (0-1) --- 'color1' table or r,g,b tuple --- 'color2' table or r,g,b tuple --- .. --- 'colorN' table or r,g,b tuple -- return r,g,b and hexcolor -local color_tuples = {} -function pfUI.api.GetColorGradient(perc, ...) - local num = arg.n - local r,g,b,hex - assert(tonumber(perc), "GetColorGradient: "..tostring(perc).." is not a number") - assert((type(arg[1])=="number" and num>5) or (type(arg[1])=="table" and num>1), "GetColorGradient: needs at least 2 colors") - perc = pfUI.api.clamp(perc,0,1) - if type(arg[1])=="number" then -- called with r,g,b tuples - -- shortcircuit the edge cases - if perc == 1 then - r,g,b = arg[num-2], arg[num-1], arg[num] - hex = string.format("|cff%02x%02x%02x", r*255, g*255, b*255) - return r,g,b,hex - elseif perc == 0 then - r,g,b = arg[1], arg[2], arg[3] - hex = string.format("|cff%02x%02x%02x", r*255, g*255, b*255) - return r,g,b,hex - end - num = num/3 - local segment, relativepercent = pfUI.api.modf(perc*(num-1)) - local r1,g1,b1, r2,g2,b2 - r1,g1,b1 = arg[segment*3+1], arg[segment*3+2], arg[segment*3+3] - r2,g2,b2 = arg[segment*3+4], arg[segment*3+5], arg[segment*3+6] - if not r2 or not g2 or not b2 then - r,g,b = r1,g1,b1 - hex = string.format("|cff%02x%02x%02x", r*255, g*255, b*255) - return r,g,b,hex +local gradientcolors = {} +function pfUI.api.GetColorGradient(perc) + if perc < 0 or perc > 1 then return nil,nil,nil,nil end + + if not gradientcolors[perc] then + local r1, g1, b1, r2, g2, b2 + if perc <= 0.5 then + perc = perc * 2 + r1, g1, b1 = 1, 0, 0 + r2, g2, b2 = 1, 1, 0 else - r,g,b = r1 + (r2-r1)*relativepercent, g1 + (g2-g1)*relativepercent, b1 + (b2-b1)*relativepercent - hex = string.format("|cff%02x%02x%02x", r*255, g*255, b*255) - return r,g,b,hex + perc = perc * 2 - 1 + r1, g1, b1 = 1, 1, 0 + r2, g2, b2 = 0, 1, 0 end - elseif type(arg[1])=="table" then -- called with color tables - -- shortcircuit the edge cases - if perc == 1 then - r,g,b = arg[num][1] or arg[num].r, arg[num][2] or arg[num].g, arg[num][3] or arg[num].b - hex = string.format("|cff%02x%02x%02x", r*255, g*255, b*255) - return r,g,b,hex - elseif perc == 0 then - r,g,b = arg[1][1] or arg[1].r, arg[1][2] or arg[1].g, arg[1][3] or arg[1].b - hex = string.format("|cff%02x%02x%02x", r*255, g*255, b*255) - return r,g,b,hex - end - pfUI.api.wipe(color_tuples) - for _,c in ipairs(arg) do - local r,g,b = c[1] or c.r, c[2] or c.g, c[3] or c.b - color_tuples[table.getn(color_tuples)+1] = r - color_tuples[table.getn(color_tuples)+1] = g - color_tuples[table.getn(color_tuples)+1] = b - end - return pfUI.api.GetColorGradient(perc,unpack(color_tuples)) + + 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) + + gradientcolors[perc] = {} + gradientcolors[perc].r = r + gradientcolors[perc].g = g + gradientcolors[perc].b = b + gradientcolors[perc].h = h end + + return gradientcolors[perc].r, + gradientcolors[perc].g, + gradientcolors[perc].b, + gradientcolors[perc].h end diff --git a/api/unitframes.lua b/api/unitframes.lua index 628daf3a..a848726a 100644 --- a/api/unitframes.lua +++ b/api/unitframes.lua @@ -1892,20 +1892,11 @@ function pfUI.uf.GetColor(self, preset) b = UnitReactionColor[UnitReaction(unitstr, "player")].b elseif preset == "health" and config["healthcolor"] == "1" then - local perc = UnitHealth(unitstr) / UnitHealthMax(unitstr) - local r1, g1, b1, r2, g2, b2 - if perc <= 0.5 then - perc = perc * 2 - r1, g1, b1 = 1, 0, 0 - r2, g2, b2 = 1, 1, 0 + if UnitHealthMax(unitstr) > 0 then + r, g, b = GetColorGradient(UnitHealth(unitstr) / UnitHealthMax(unitstr)) else - perc = perc * 2 - 1 - r1, g1, b1 = 1, 1, 0 - r2, g2, b2 = 0, 1, 0 + r, g, b = 0, 0, 0 end - r = r1 + (r2 - r1)*perc - g = g1 + (g2 - g1)*perc - b = b1 + (b2 - b1)*perc elseif preset == "power" and config["powercolor"] == "1" then r = ManaBarColor[UnitPowerType(unitstr)].r diff --git a/modules/panel.lua b/modules/panel.lua index 1c844fa1..d1676795 100644 --- a/modules/panel.lua +++ b/modules/panel.lua @@ -357,7 +357,7 @@ pfUI:RegisterModule("panel", function() repPercent = math.floor(lval / rval * 100) if repPercent < 100 then local link = GetInventoryItemLink("player",id) - local r,g,b,hex = GetColorGradient(repPercent/100, 1,0,0, 1,1,0, 0,1,0) -- red to green through yellow + local r,g,b,hex = GetColorGradient(repPercent/100) local cPercent = string.format("%s%s%%|r",hex,repPercent) widget.itemLines[table.getn(widget.itemLines)+1]={link, cPercent} end