This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Decimal To Hex
#1
Hi I'm trying to get decimal R,G,B colors and convert to hex with this code
Code:
function rgbToHex(r,g,b)
 
rgb = (r * 0x10000) + (g * 0x100) + b
    return string.format("%x", rgb)
end

The problem is if i use big numbers then 160 function returns 0.
I think "%x" formatter doesn't format the number but couldn't solve yet
Reply
#2
You can do it this way:
Code:
function rgbToHex(r,g,b)
  return string.format('%02x%02x%02x', r, g, b)
end

Your original function should work if color values are in the 0..255 range. But the output value width will vary unless the format is changed to %06x.
Reply


Forum Jump: