From ef9f4519f12da0b95f36097c7691266cb6ab218c Mon Sep 17 00:00:00 2001 From: shagu Date: Tue, 21 May 2019 22:46:57 +0200 Subject: [PATCH] api: fix cache and limits in gradient color --- api/api.lua | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/api/api.lua b/api/api.lua index 47b0ec69..1cc4a688 100644 --- a/api/api.lua +++ b/api/api.lua @@ -943,11 +943,13 @@ end -- return r,g,b and hexcolor local gradientcolors = {} function pfUI.api.GetColorGradient(perc) - -- fallback to black on bad numbers - if perc < 0 or perc > 1 then return 0,0,0,"|cff000000" end + perc = perc > 1 and 1 or perc + perc = perc < 0 and 0 or perc - if not gradientcolors[perc] then + local index = perc + if not gradientcolors[index] then local r1, g1, b1, r2, g2, b2 + if perc <= 0.5 then perc = perc * 2 r1, g1, b1 = 1, 0, 0 @@ -958,22 +960,22 @@ function pfUI.api.GetColorGradient(perc) r2, g2, b2 = 0, 1, 0 end - local r = r1 + (r2 - r1)*perc - local g = g1 + (g2 - g1)*perc - local b = b1 + (b2 - b1)*perc + local r = round(r1 + (r2 - r1) * perc, 4) + local g = round(g1 + (g2 - g1) * perc, 4) + local b = round(b1 + (b2 - b1) * perc, 4) local h = pfUI.api.rgbhex(r,g,b) - gradientcolors[perc] = {} - gradientcolors[perc].r = r - gradientcolors[perc].g = g - gradientcolors[perc].b = b - gradientcolors[perc].h = h + gradientcolors[index] = {} + gradientcolors[index].r = r + gradientcolors[index].g = g + gradientcolors[index].b = b + gradientcolors[index].h = h end - return gradientcolors[perc].r, - gradientcolors[perc].g, - gradientcolors[perc].b, - gradientcolors[perc].h + return gradientcolors[index].r, + gradientcolors[index].g, + gradientcolors[index].b, + gradientcolors[index].h end -- [ GetNoNameObject ] --