PDA

View Full Version : RGB to HEX


jwarrent
08-25-2004, 02:29 PM
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. :)

Corey
08-25-2004, 02:54 PM
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 (http://www.indigorose.com)

Worm
08-25-2004, 04:06 PM
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)


--[[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


Sample Call:

Dialog.Message("Color", "The HEX value of decimal 255 is: " .. DEC2HEX(255))

TJ_Tigger
08-25-2004, 04:33 PM
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)

Stefan_M
08-25-2004, 04:49 PM
Solution with tables.


Stefan_M

Worm
08-25-2004, 04:51 PM
I tried that Tig, and the results were not what I expected.

hexvalue = tonumber(255, 16)

give me a value of 597

Corey
08-25-2004, 04:57 PM
Groovy functions Wormcat. :yes

Corey Milner
Creative Director, Indigo Rose Software (http://www.indigorose.com)

JimS
08-25-2004, 05:06 PM
There are only 10 types of people in the world:
Those who understand binary, and those who don't!

That's funny!

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

jwarrent
08-25-2004, 05:19 PM
Thanks a bunch guys! Worm, those two functions work great, but there are a couple of misplaced variables.. :) Here's the changed 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

TJ_Tigger
08-25-2004, 05:21 PM
sorry should have tested before I post

decnum = tonumber("ff",16);
Dialog.Message("decnum",decnum);

Worm
08-25-2004, 05:31 PM
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!

JimS
08-25-2004, 05:34 PM
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.

TJ_Tigger
08-25-2004, 05:44 PM
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);

Corey
08-25-2004, 05:49 PM
That is groovy Tiggcat. I had tried, and failed, to figure that out at one point myself. :o

Corey Milner
Creative Director, Indigo Rose Software (http://www.indigorose.com)