Hex/string/float problem
Blitz3D Forums/Blitz3D Programming/Hex/string/float problem
| ||
I've used a bit of code before that picks the color of a pixel and then breaks it down into its component colorshired# = (rgb2 And $FF0000)/$10000 ; <--- Red value of pixel higrn# = (rgb2 And $FF00)/$100 ; <--- Grn value of pixel hiblu# = (rgb2 And $FF) ; <--- Blu value of pixel Now, rather than get my color from a pixel I want to use Constants to store color values and then just set my color from there. Const RED$ = "$FF0000" Const GREEN$ = "$00FF00" Const BLUE$ = "$0000FF" Const LPINK$ = "$FD89D8" global done while not done cls() if keyhit(1) then done = true ExtractColor(LPINK) rect 0,0,300,200 flip() wend end Function ExtractColor(Val$) R# = (val And $FF0000)/$10000 G# = (val And $FF00)/$100 B# = (val And $FF) Color r,g,b End Function This keeps giving me a value of 0 for each R,G,B. However, if I replace the 'val' in each line with the hex value of my designated pink color then it works fine. Hex values are a bit beyond my understanding and thought a simple tinker would fix this, but I really don't understand why it works with the actual value but not if I use a string variable with the value. This works - R# = ($FD89D8 and $FF0000)/$10000 but this doesn't - val$ = "$FD89D8" R# =(val and $FF0000)/$10000 Can anyone explain why and what the solution would be? |
| ||
You're comparing num instead of val$ ? num isn't defined anywhere, so it will always be zero. |
| ||
Sorry, mixed up two versions I'd tried. Num should be Val. Silly mistake on my part. I've edited it. |
| ||
. |
| ||
Blitz3D has three kinds of variables/constants. Float and Int are numbers, and there are also strings. The $ symbol marks a value as a string, so s$ is a string variable. x# is a float and n%, or simply n, is an integer. There is another use for $, as a prefix for a hexadecimal 'literal' value. Just as 123 is an integer literal in decimal notation $7B is the same integer value as a hexadecimal literal. Blitz also auto-converts between the three kinds of values. So if n is an integer variable and we do n = "123" the string "123" is converted to integer 123 and assigned to n. The conversion of string to number stops at the first non-valid character. So n = "12J3" assigns the value 12 to n. This string-to-number conversion understands only decimal. I suppose Blitz could recognize the $ prefix and 'switch gears' for hexadecimal, but it doesn't. That's why n = "$7B" results in n = 0. The conversion stops at $, an invalid symbol in a decimal number. Here is a working example. All values are integer, except for the string value returned by the Hex$( ) function. Const RED = $FF0000 Const GREEN = $00FF00 Const BLUE = $0000FF Const LPINK = $FD89D8 val = LPINK R = (val And $FF0000)/$10000 G = (val And $FF00)/$100 B = (val And $FF) Print r Print b Print g Print Print Hex(r) Print Hex(g) Print Hex(b) WaitKey |
| ||
Floyd, thanks for this. I don't know why I had so much trouble. I have to chalk it up to not much experience working directly with hex numbers. I could almost swear I started out as your example but obviously I didn't and made a wrong assumption with it somewhere that led me into the wrong direction. I knew I was misplacing symbols somewhere and that it was something simple but just couldn't get onto it correctly. You've saved me many hours of head scratching! |