Skip to content

Instantly share code, notes, and snippets.

@saantonandre
saantonandre / utils.lua
Last active June 1, 2026 20:17
Lua HEX to RGB, short hex and alpha channel support, validation, fractional/integer output
local utils = {}
--- Converts a hex color string to rgb/rgba values
--- @param hexColor string
--- @param asFraction? boolean
function utils.hexToRgb(hexColor, asFraction)
if (not utils.isValidHexColor(hexColor)) then error("Invalid hex color string: " .. hexColor) end
local parse = function(f, t)
local value = tonumber("0x" .. hexColor:sub(f, t or f)) * (t and 1 or 17)
return (asFraction and value / 255) or value