Skip to content

Instantly share code, notes, and snippets.

@Sparkles-Laurel
Created January 12, 2025 16:01
Show Gist options
  • Save Sparkles-Laurel/4f1953c3ab77771086adc4622587dfdd to your computer and use it in GitHub Desktop.
Save Sparkles-Laurel/4f1953c3ab77771086adc4622587dfdd to your computer and use it in GitHub Desktop.
Torino scale in lua
local math = require('math')
local torino = {}
torino.colors = {
[0] = nil,
[1] = "green",
[2] = "yellow",
[3] = "yellow",
[4] = "yellow",
[5] = "orange",
[6] = "orange",
[7] = "orange",
[8] = "red",
[9] = "red",
[10] = "red"
}
local log10 = function (x) return math.log(x, 10) end
function torino.compute(energy, probability)
if (((log10(energy) + 1) / 3 + (log10(probability) + 2) / 2) < 0)
or (log10(energy) < 0) then
return 0, torino.colors[0]
elseif (((log10(energy) + 1) / 3) + ((log10(probability) + 2) / 2) >= 0)
and (log10(energy) >= 0)
and (((log10(energy) - 2) / 3) + ((log10(probability) + 2) / 2) < 0)
and (log10(probability) < -2) then
return 1, torino.colors[1]
elseif (((log10(energy) - 2) / 3) + ((log10(probability) + 2) / 2) >= 0)
and ((((log10(energy) - 5) / 3) + ((log10(probability) + 2) / 2)) < 0) then
return 2, torino.colors[2]
elseif ((log10(probability) >= -2)
and (log10(energy) >= 0)
and (probability < 0.99)
and (log10(energy) < 2)) then
return 3, torino.colors[3]
elseif ((log10(probability) >= -2)
and (log10(energy) >= 2)
and (((log10(energy - 5) / 3) + (log10(probability + 2) / 2)) < 0))
and (probability < 0.99) then
return 4, torino.colors[4]
elseif (((log10(energy - 5) / 3) + (log10(probability + 2) / 2)) >= 0)
and (probability < 0.99)
and (log10(energy) < 5) then
return 5, torino.colors[5]
elseif ((log10(energy - 5) / 3) + (log10(probability + 2) / 2) >= 0)
and (log10(probability) < -2) then
return 6, torino.colors[6]
elseif (log10(probability) >= -2)
and (log10(energy) >= 5)
and (probability < 0.99) then
return 7, torino.colors[7]
elseif (probability > 0.99)
and (log10(energy) >= 0)
and (log10(energy) < 2) then
return 8, torino.colors[8]
elseif (probability > 0.99)
and (log10(energy) >= 2)
and (log10(energy) < 5) then
return 9, torino.colors[9]
elseif (probability > 0.99) and (log10(energy) >= 5) then
return 10, torino.colors[10]
end
-- ideally unreachable
return nil, nil
end
return torino
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment