Skip to content

Instantly share code, notes, and snippets.

@marceloCodget
Created October 10, 2012 03:05
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marceloCodget/3862929 to your computer and use it in GitHub Desktop.
Save marceloCodget/3862929 to your computer and use it in GitHub Desktop.
RGB to Hex in Lua
-- passing a table like {255, 100, 20}
function application:rgbToHex(rgb)
local hexadecimal = '0X'
for key, value in pairs(rgb) do
local hex = ''
while(value > 0)do
local index = math.fmod(value, 16) + 1
value = math.floor(value / 16)
hex = string.sub('0123456789ABCDEF', index, index) .. hex
end
if(string.len(hex) == 0)then
hex = '00'
elseif(string.len(hex) == 1)then
hex = '0' .. hex
end
hexadecimal = hexadecimal .. hex
end
return hexadecimal
end
@atirut-w
Copy link

That's a frightening piece of code you wrote up there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment