Created
October 10, 2012 03:05
-
-
Save marceloCodget/3862929 to your computer and use it in GitHub Desktop.
RGB to Hex in Lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
If you want padding, the last line needs to be
return string.format("%06x", rgb)
.
Do you know where can I find a list of formattings and options for C-style string formatting?
If you want padding, the last line needs to be
return string.format("%06x", rgb)
.Do you know where can I find a list of formattings and options for C-style string formatting?
What about https://en.wikipedia.org/wiki/Printf_format_string?
What about https://en.wikipedia.org/wiki/Printf_format_string?
Thanks
would u let that one slide marcelo codget
im kinda noob at coding but here is my solution:
rgb = {235,123,175}
function convert_to_hex (rgb)
local hex = ""
for i = 1 , #rgb do
local hex_table = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"}
local num = rgb[i]/16
local whole = math.floor( num )
local remainder = num - whole
hex = hex .. hex_table[whole+1] .. hex_table[remainder*16 + 1]
end
return hex
end
print(convert_to_hex(rgb))
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
This is horrible and unreliable. DO NOT USE THIS:
pairs(rgb)
guarantees absolutely no order. If you pass the RGB values as a list{r, g, b}
, it will work though, becausepairs
traverses the list part in order. There's no guarantee this is the case though. Horrible bugs will happen as soon as you pass{r = r, g = g, b = b}
tables. For these, absolutely no order is guaranteed. You could be getting RGB if you're lucky, leading you not to discover this undefined behavior, you could also get BGR, GRB, ... though, scratching your head as to why it's not working. To "fix" this,ipairs
could be used. Additionally, this is basically a footgun. You can happily pass less than 3 or more than 4 numbers, leading to invalid hex color codes. It will also happily write numbers > 255, screwing up the hex color codes by displacing the following digits and creating a length > 6.Bad code quality, uneeded complexity.
application:
which is absolutely useless;self
isn't needed.key
is unused:_
should be used. Brackets instead of spaces around conditions. The inner while loop basically just doesstring.format("#%X", value)
, but a lot slower. The followingif
s do the padding, basicallystring.format("#%02X", value)
.Bad performance. This is unnecessarily slow.
Use
string.format("#%02X%02X%02X", r, g, b)
instead.Regarding the following:
If you want padding, the last line needs to be
return string.format("%06x", rgb)
.