View Full Version : Hex 2 R,g,b
Does anyone know a formula for converting a Hex value or a Decimal value for a color, into the correct R,G,B color value?
.
Corey
07-23-2005, 05:43 AM
I believe the script "rgb.lua" found in your AMS script gallery has a function to do that. :)
Thanks Corey, that Lua script is similar to what I’m after, but not quite. It will let me go from R,G,B to Decimal, or from R,G,B to Hex, but I want to go in the other direction. I want to change Hex to R,G,B and/or Decimal to R,G,B
.
You can use this to get HEX to RGB, then use the other RGB.LUA to go from there
function Hex2RGB(sHexString)
if String.Length(sHexString) ~= 6 then
return 0,0,0
else
red = String.Left(sHexString,2)
green = String.Mid(sHexString,3,2)
blue = String.Right(sHexString,2)
red = tonumber(red, 16).."";
green = tonumber(green, 16).."";
blue = tonumber(blue, 16).."";
return red, green, blue
end
end
red, green, blue = Hex2RGB("FFCCFF")
Dialog.Message("", red..", "..green..", "..blue)
That’s it! Thank you so much. It is so cool to have smart friends like you guys, thanks. :) :yes
.
one thing on that is that the LUA function returns a number (thanks Tig)
If you want the returned values to be numbers, remove the .."" in the function. Also as an after thought, you might want to return -1, -1, -1 if the Hex string isn't the correct length so you can test if the function was successful.
function Hex2RGB(sHexString)
if String.Length(sHexString) ~= 6 then
return -1, -1, -1
else
red = String.Left(sHexString,2)
green = String.Mid(sHexString,3,2)
blue = String.Right(sHexString,2)
red = tonumber(red, 16);
green = tonumber(green, 16);
blue = tonumber(blue, 16);
return red, green, blue
end
end
red, green, blue = Hex2RGB("FFCCFF")
if red ~= -1 then
Dialog.Message("", red..", "..green..", "..blue)
else
Dialog.Message("","Function failed")
end
Wow! Thanks again Worm, your kindness is amazing.
I had looked through the forums before I posted, and I noticed functions that convert in other directions, but I couldn’t find this one, the one I most needed. Your help on this is very much appreciated.
.
vBulletin® v3.7.3, Copyright ©2000-2009, Jelsoft Enterprises Ltd.