Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 2.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 10-13-2005, 04:08 PM   #1
AniComUS
Registered User
 
AniComUS's Avatar
 
Join Date: Feb 2005
Location: Connecticut, USA
Posts: 79
Default RGB COLOR Hex/Decimal Conversion Without Bitwise Code

I successfully built a HEX to DEC and DEC to HEX RGB color converter using intermediary Binary number conversion and Bitwise parsing as suggested by a response to a prior posting, and consistent with the Bitwise conversion instructions in the Flash ActionScript Bible. Thank you so much.

However, Binary numbers and Bitwise work is fairly opaque to me. Thinking and reading in Binary does not come easy. So I asked a former software partner about it. At first, he was amazed that there was an issue, because his core skill is LotusScript which has a built-in HEX/DEC converter object. However, he rembered Assembler language algorithms that did not require a Binary intermediary - one that works directly between HEX and DEC.


Here are his comments. Any thoughts about one method over the other for ActionScript?. It seems easier to code and probably easier to read when the code is revisited some time later.


* * * *

As I correctly recalled, there is an easy conversion formula that bypasses
converting to binary and allows direct conversion from hex to decimal (the
book shows a couple methods of direct conversion between number systems -
including hex to binary and decimal to binary).

For hexadecimal numbers, the Least Significant Bit (the rightmost character
in the hex value - we'll call it LSB) will always have a decimal value of
0-15 (hex values 0-F). Each additional hex value to left of the LSB will
have a decimal value of 16 to power of it's position away from the LSB
times the hex value decimal equivalent (if it's one position to the left of
the LSB, it will have a value of 16 to the power of 1 times the decimal
equivalent of 0-F, if it's 2 positions to the left it will have a value of
16 to the power of 2 times the decimal equivalent of 0-F, and so on). The
decimal value is the sum of all computed decimal values for each hex value.
This rule holds true for all hex values - regardless of size.

Since the decimal color pallette only ranges from 0-255 (that equates to
hex 0-FF), your conversion formula would be fairly simple. Set up either
an array or even simpler, a set of 16 variables where LSB hex values are
defined as their decimal equivalents (0=0, 1=1,2=2...A=10, B=11...F=15).
If the length of your hex value is one character, there really is no
conversion that has to be done - just use the value that is defined in the
array or the variable that you set up. If the length of the hex value is
two characters, the formula above can be used to convert directly to hex.
For example, if you have hex 8A, the routine would go something like this:

LSB = A (hex) = 10 (decimal)
MSB (Most Sig. Bit) = 8 (hex) = 128 (decimal) = 16 to the power of 1 times
8 = 16 * 8
Decimal Value of 8A hex = 138 (10+128)

Expanding on these calculations, a loop could be built to do this
conversion for any size hex value. It would look something like this
pseudocode:

set computed decimal value to zero
set significant bit counter to zero
get LSB of the hex value
while
there are significant bits to be processed in the hex value
compute 16 to the power of significant bit counter
multiply our decimal equivalent of the hex value (from the table or
array) by the computed value above
add this value to the computed decimal value
get next significant bit
increment significant bit counter by one
while end

I think the larger the hex value is, the more apparent the equation
becomes. Here's a mathematical example of converting 2A5:

2A5 = 16pwr0 * 5 + 16pwr1 * A + 16pwr2 * 2
= 1 * 5 + 16 * 10 + 16 * 16 * 2
= 5 + 160 + 512 = 677

Last edited by AniComUS; 10-13-2005 at 04:09 PM. Reason: spelling
AniComUS is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT. The time now is 06:41 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.