Hey guys, I'm hoping someone out there can help me find a good way to convert an RGB value to HEX. I've searched the forums and can't seem to find anything that fits this task quite right.![]()
Professional Software Development Tools
Hey guys, I'm hoping someone out there can help me find a good way to convert an RGB value to HEX. I've searched the forums and can't seem to find anything that fits this task quite right.![]()
Here's a good design time converter:
http://www.321webmaster.com/colorconverter.php
Here's a good explanation if you want to make a function to do this at runtime:
http://www.katsueydesignworks.com/wmp_rgb_hex.htm
Corey Milner
Creative Director, Indigo Rose Software
When you say RGB, I'm assuming you have each color in the decimal equivalant of 0-255. Send each color (Red, Green, and Blue) to this function
if your RGB is 255, 0, 255 then you could do something like this:
Hex = "#" .. DEC2HEX(255) .. DEC2HEX(0) .. DEC2HEX(255)
Sample Call:Code:--[[Global Functions]]-- function DEC2HEX(nRGB) --This only works for number 255 or less nRGB = String.ToNumber(nRGB) nBase= Math.Floor(nRGB / 16) nPlus= Math.Mod(nRGB, 16) return GetHexValue(nRGB)..GetHexValue(nPlus) end function GetHexValue(nNum) nNum=String.ToNumber(nNum) if nNum == 10 then nNum="A" elseif nBase == 11 then nNum = "B" elseif nBase == 12 then nNum = "C" elseif nBase == 13 then nNum = "D" elseif nBase == 14 then nNum = "E" elseif nBase == 15 then nNum = "F" end return nNum end
Code:Dialog.Message("Color", "The HEX value of decimal 255 is: " .. DEC2HEX(255))
There is also a predefined LUA function called tonumber. Here are the details from the LUA reference guide.
tonumber (e [, base])
Receives one argument, and tries to convert it to a number. If the argument is already a number or a string convertible to a number, then tonumber returns that number; otherwise, it returns nil.
An optional argument specifies the base to interpret the numeral. The base may be any integer between 2 and 36 inclusive. In bases above 10, the letter `A' (either upper or lower case) represents 10, `B' represents 11, and so forth, with `Z' representing 35.
In base 10 (the default), the number may have a decimal part, as well as an optional exponent part (see Section 4.3). In other bases, only integers are accepted.
decimalval = tonumber(FF, 10)
hexvalue = tonumber(255, 16)
TJ-Tigger
"A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools."
"Draco dormiens nunquam titillandus."
Map of IR Forum Users - IR Project CodeViewer - Online Help - TiggTV - QuizEngine
Solution with tables.
Stefan_M
"With a rubber duck, one's never alone."
Douglas Adams, The Hitchhiker's Guide to the Galaxy
I tried that Tig, and the results were not what I expected.
hexvalue = tonumber(255, 16)
give me a value of 597
Groovy functions Wormcat.
Corey Milner
Creative Director, Indigo Rose Software
That's funny!Originally Posted by Worm
Worm, thanks for the function.
Tigger, I saw that tonumber function also, but I can't seem to get it to work 'correctly' or at least the way I expect it to. Have you?
I get errors with:
decimalval = tonumber(FF, 10);
I'm not sure what I'm doing wrong.
Edit: Sorry, I didn't mean to repost the same question as Worm did
Thanks a bunch guys! Worm, those two functions work great, but there are a couple of misplaced variables..Here's the changed code:
Code:function DEC2HEX(nRGB) --This only works for number 255 or less nRGB = String.ToNumber(nRGB) nBase= Math.Floor(nRGB / 16) nPlus= Math.Mod(nRGB, 16) return GetHexValue(nBase)..GetHexValue(nPlus) end function GetHexValue(nNum) nNum=String.ToNumber(nNum) if nNum == 10 then nNum="A" elseif nNum == 11 then nNum = "B" elseif nNum == 12 then nNum = "C" elseif nNum == 13 then nNum = "D" elseif nNum == 14 then nNum = "E" elseif nNum == 15 then nNum = "F" end return nNum end
sorry should have tested before I post
decnum = tonumber("ff",16);
Dialog.Message("decnum",decnum);
TJ-Tigger
"A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools."
"Draco dormiens nunquam titillandus."
Map of IR Forum Users - IR Project CodeViewer - Online Help - TiggTV - QuizEngine
Thanks for catching my mistakes jwarrent.
But it looks like the better way to go is how Tig has shown using the tonumber function in LUA. Check out his last post.
You go Tig'man!
Good job Tigger! That fixed it. I’m sure glad one of us is smart enough to get it to work. Nope, I didn’t even think of quotation marks. Thanks, this looks like it’s probably the easiest solution for most things.
Stefan, that’s a nice example. Lately I’ve found myself downloading other of your examples too. Good job, thanks a lot. It’s great that the Forums has yet another great contributor.
Here is how you can do it the other way too.
hexnum = string.format("%X", 255);
Dialog.Message("hexnum",hexnum);
or for lowercase
hexnum = string.format("%x", 255);
Dialog.Message("hexnum",hexnum);
TJ-Tigger
"A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools."
"Draco dormiens nunquam titillandus."
Map of IR Forum Users - IR Project CodeViewer - Online Help - TiggTV - QuizEngine
That is groovy Tiggcat. I had tried, and failed, to figure that out at one point myself.
Corey Milner
Creative Director, Indigo Rose Software