api: trim down color gradient function

* avoid division through zero in unitframes
* make use of gradient func in unitframes
This commit is contained in:
shagu
2018-09-28 22:36:06 +02:00
parent aef2a61c6b
commit 2daddc2f43
3 changed files with 33 additions and 66 deletions
+29 -53
View File
@@ -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