Jump to content

Failed color conversion to Hex


Recommended Posts

For some reason, I can't convert to Hex a Decimal color that has been calculated.

For example, this works:

Local $iColor = 3310546
ConsoleWrite("$iColor (dec): " & $iColor & ", $iColor (hex): " & Hex($iColor, 6) & @CRLF)

giving the expected output:

$iColor (dec): 3310546, $iColor (hex): 3283D2

But if I want to take an average of several colors, I have a problem.  For example this doesn't work:

$iColor = (3310546 + 3310546) / 2
ConsoleWrite("$iColor (dec): " & $iColor & ", $iColor (hex): " & Hex($iColor, 6) & @CRLF)

giving the wrong output:

$iColor (dec): 3310546, $iColor (hex): 000000

I observe this behavior after any processing of a decimal color.

Is this a bug?

Link to comment
Share on other sites

9 minutes ago, jmor said:

Is this a bug?

No.  After your calculation, $iColor's data type is a double not an integer. They are stored differently in memory.  The Hex function did exactly what you told it to do,  Maybe the example below will help illuminate the issue:

 

$iColor = (3310546 + 3310546) / 2
ConsoleWrite("$iColor is a " & VarGetType($iColor) & @CRLF)
ConsoleWrite("$iColor = 0x" & Hex($iColor) & @CRLF)
ConsoleWrite("$iColor (dec): " & $iColor & ", $iColor (hex): " & Hex($iColor, 6) & @CRLF)

ConsoleWrite(@CRLF)

$iColor = Int((3310546 + 3310546) / 2)
ConsoleWrite("$iColor is a " & VarGetType($iColor) & @CRLF)
ConsoleWrite("$iColor = 0x" & Hex($iColor) & @CRLF)
ConsoleWrite("$iColor (dec): " & $iColor & ", $iColor (hex): " & Hex($iColor, 6) & @CRLF)

Console

$iColor is a Double
$iColor = 0x414941E900000000
$iColor (dec): 3310546, $iColor (hex): 000000

$iColor is a Int32
$iColor = 0x003283D2
$iColor (dec): 3310546, $iColor (hex): 3283D2

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...