Refactor meta -> shared
This commit is contained in:
66
FreshShit/_Shared/Colorer.lua
Normal file
66
FreshShit/_Shared/Colorer.lua
Normal file
@@ -0,0 +1,66 @@
|
||||
---@alias Color {r: number, g: number, b: number}
|
||||
---@class Colorer
|
||||
---@field colors table<number, Color>
|
||||
---@field breakpoints table<number>
|
||||
Colorer = {
|
||||
--- Make sure colors and breakpoints always have the same number of entries! VERY IMPORTANT!
|
||||
---@type table<number, Color>
|
||||
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<number, table<number, number>>
|
||||
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 })
|
||||
Reference in New Issue
Block a user