Skip to content

Instantly share code, notes, and snippets.

@demonnic
Created October 24, 2021 03:56
Show Gist options
  • Save demonnic/bf50cd8c80a0d96766478143251561b2 to your computer and use it in GitHub Desktop.
Save demonnic/bf50cd8c80a0d96766478143251561b2 to your computer and use it in GitHub Desktop.
getAlertGradientTable(max): Create a table of r,g,b color values for an alert scale 1 - max
local function getBadnessLevel(num, max)
local halfWay = (max+1) / 2
local stepSize = 255 / (halfWay - 1)
local r,g,b = 0,255,0
if num <= 1 then
return r,g,b
end
if num > max then
return 255,0,0
end
if num < halfWay then
r = math.floor((num - 1) * stepSize)
return r,g,b
else
r = 255
g = 255 - math.floor((num - halfWay) * stepSize)
return r,g,b
end
end
function getAlertGradientTable(max)
local result = {}
for i = 1, max do
result[i] = {getBadnessLevel(i, max)}
end
return result
end
--[[
-- code below this creates a Mudlet demonstration of what the colors look like on a 1-10 scale.
echo("\n\n")
local grads = getAlertGradientTable(10)
for lvl, clr in ipairs(grads) do
local r,g,b = unpack(clr)
local txt = f"<{r},{g},{b}>Badness level is {lvl}\n"
decho(txt)
end
--]]
@demonnic
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment