From 5e872647fc993645249f784a462da26eec356693 Mon Sep 17 00:00:00 2001 From: Dave Date: Fri, 15 Mar 2024 21:22:24 +0100 Subject: [PATCH] Refactor meta -> shared --- FreshShit/{__Meta => _Shared}/CLEUParser.lua | 0 FreshShit/_Shared/Colorer.lua | 66 ++++++++++++++++++++ 2 files changed, 66 insertions(+) rename FreshShit/{__Meta => _Shared}/CLEUParser.lua (100%) create mode 100644 FreshShit/_Shared/Colorer.lua diff --git a/FreshShit/__Meta/CLEUParser.lua b/FreshShit/_Shared/CLEUParser.lua similarity index 100% rename from FreshShit/__Meta/CLEUParser.lua rename to FreshShit/_Shared/CLEUParser.lua diff --git a/FreshShit/_Shared/Colorer.lua b/FreshShit/_Shared/Colorer.lua new file mode 100644 index 0000000..c2e828f --- /dev/null +++ b/FreshShit/_Shared/Colorer.lua @@ -0,0 +1,66 @@ +---@alias Color {r: number, g: number, b: number} +---@class Colorer +---@field colors table +---@field breakpoints table +Colorer = { + --- Make sure colors and breakpoints always have the same number of entries! VERY IMPORTANT! + ---@type table + colors = { + { r = 0.62, g = 0.62, b = 0.62 }, -- Grey + { r = 1, g = 1, b = 1 }, -- White + { r = 0.12, g = 1, b = 0 }, -- Green + { r = 0, g = 0.44, b = 0.87 }, -- Blue + { r = 0.64, g = 0.21, b = 0.93 }, -- Purple + { r = 1, g = 0.5, b = 0 }, -- Orange + { r = 0.9, g = 0.8, b = 0.5 }, -- Light Gold + { r = 0, g = 0.8, b = 1.0 }, -- Blizzard Blue + }, + breakpoints = { -999, -10, -5, -2, 2, 5, 10, 999 }, + + ---@param value number + ---@return Color, nil|string + Interpolate = function(value) + local color = { r = 0, g = 0, b = 0 } + + ---@type table> + local bracket = { { 0, 0 }, { 1, 1 } } + for i = 1, #Colorer.breakpoints do + if value < Colorer.breakpoints[i] then + bracket[2] = { i, Colorer.breakpoints[i] } + break + end + bracket[1] = { i, Colorer.breakpoints[i] } + end + + ---@type Color + local startColor = Colorer.colors[bracket[1][1]] + ---@type Color + local endColor = Colorer.colors[bracket[2][1]] + + local fraction = (value - bracket[1][2]) / (bracket[2][2] - bracket[1][2]) + + for k, v in pairs(startColor) do + color[k] = Colorer.lerp(v, endColor[k], fraction) + end + + return color, nil + end, + + ---@param color Color + ---@return string + RGBToHex = function(color) + local r = math.floor(color.r * 255) + local g = math.floor(color.g * 255) + local b = math.floor(color.b * 255) + return string.format("%02x%02x%02x", r, g, b) + end, + + ---@param a number + ---@param b number + ---@param t number + ---@return number + lerp = function(a, b, t) + return a * (1 - t) + b * t + end +} +setmetatable(Colorer, { __index = Colorer }) \ No newline at end of file