Skip to content

Instantly share code, notes, and snippets.

@chev2
Last active September 15, 2019 20:53
Show Gist options
  • Save chev2/df1caeca5a82695f190dcf6e25d651ba to your computer and use it in GitHub Desktop.
Save chev2/df1caeca5a82695f190dcf6e25d651ba to your computer and use it in GitHub Desktop.
Converts a hex code (#FFFFFF, #FFF, FFF, #fff, #FFFFFFFF, etc.) into a color (Color(255, 255, 255)). Supports alpha.
function string.ColorHex(hex) --Converts a hex code (#FFF, FFF or #FFFFFF, FFFFFF) into a color (Color(255, 255, 255)). Supports alpha.
local h = string.Split(string.gsub(hex, "#", ""):upper(), "") --FFF, FFFFFF, FFFFFFFF
local c = #h
for i=1, 8-c, 1 do
h[i+c] = "F"
end
if #h != 8 then return nil end
local ct = {["r"] = h[1]..h[2], ["g"] = h[3]..h[4], ["b"] = h[5]..h[6], ["a"] = h[7]..h[8]}
for k, v in pairs(ct) do
ct[k] = tonumber(v, 16)
end
local col = Color(ct["r"], ct["g"], ct["b"], ct["a"])
return col
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment