Logic Machine Forum
Decimal To Hex - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Decimal To Hex (/showthread.php?tid=4701)



Decimal To Hex - mkaymak - 05.04.2023

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


RE: Decimal To Hex - admin - 05.04.2023

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.