Indigo Rose Software

Professional Software Development Tools

 
Results 1 to 14 of 14

Thread: RGB to HEX

  1. #1
    Join Date
    Feb 2004
    Location
    Las Vegas
    Posts
    24

    RGB to HEX

    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.

  2. #2
    Corey is offline Indigo Rose Staff Alumni
    Join Date
    Aug 2002
    Posts
    9,746
    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

  3. #3
    Join Date
    Jul 2002
    Location
    USA
    Posts
    3,959
    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)

    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
    Sample Call:
    Code:
    Dialog.Message("Color", "The HEX value of decimal 255 is:  " .. DEC2HEX(255))

  4. #4
    Join Date
    Sep 2002
    Location
    Sol 3
    Posts
    3,160
    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

  5. #5
    Join Date
    Nov 2003
    Location
    Salzburg / Austria
    Posts
    312
    Solution with tables.


    Stefan_M
    Attached Files
    "With a rubber duck, one's never alone."

    Douglas Adams, The Hitchhiker's Guide to the Galaxy

  6. #6
    Join Date
    Jul 2002
    Location
    USA
    Posts
    3,959
    I tried that Tig, and the results were not what I expected.

    hexvalue = tonumber(255, 16)

    give me a value of 597

  7. #7
    Corey is offline Indigo Rose Staff Alumni
    Join Date
    Aug 2002
    Posts
    9,746
    Groovy functions Wormcat.

    Corey Milner
    Creative Director, Indigo Rose Software

  8. #8
    Join Date
    May 2003
    Location
    Pendleton, Oregon
    Posts
    1,038
    Quote Originally Posted by Worm
    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

  9. #9
    Join Date
    Feb 2004
    Location
    Las Vegas
    Posts
    24
    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

  10. #10
    Join Date
    Sep 2002
    Location
    Sol 3
    Posts
    3,160
    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

  11. #11
    Join Date
    Jul 2002
    Location
    USA
    Posts
    3,959
    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!

  12. #12
    Join Date
    May 2003
    Location
    Pendleton, Oregon
    Posts
    1,038
    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.

  13. #13
    Join Date
    Sep 2002
    Location
    Sol 3
    Posts
    3,160
    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

  14. #14
    Corey is offline Indigo Rose Staff Alumni
    Join Date
    Aug 2002
    Posts
    9,746
    That is groovy Tiggcat. I had tried, and failed, to figure that out at one point myself.

    Corey Milner
    Creative Director, Indigo Rose Software

Similar Threads

  1. Program: Binary Hex and Decimal Converter
    By Protocol in forum AutoPlay Media Studio 5.0 Examples
    Replies: 3
    Last Post: 07-15-2004, 12:11 PM
  2. Registry -- dword -- hex or decimal?
    By jassing in forum Setup Factory 6.0
    Replies: 2
    Last Post: 04-11-2003, 02:01 PM
  3. Converting hex value to decimal...can we do this in SF6??
    By Martin_SBT in forum Setup Factory 6.0
    Replies: 5
    Last Post: 02-06-2003, 02:22 PM
  4. Hex Code replacing
    By BaZZa101 in forum AutoPlay Media Studio 4.0
    Replies: 1
    Last Post: 01-06-2003, 11:21 AM
  5. Help Reading and writing Hex \ Binary
    By stagemon in forum AutoPlay Menu Studio 3.0
    Replies: 1
    Last Post: 08-09-2002, 06:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts