Example Code:
GraphicsWindow.MouseDown = md
Sub md
color = GraphicsWindow.GetPixel(GraphicsWindow.MouseX,GraphicsWindow.MouseY)
EndSub
This returns a hexadecimal value, but I need to convert this to an rgb value. How do I do so?
Example Code:
GraphicsWindow.MouseDown = md
Sub md
color = GraphicsWindow.GetPixel(GraphicsWindow.MouseX,GraphicsWindow.MouseY)
EndSub
This returns a hexadecimal value, but I need to convert this to an rgb value. How do I do so?
The trick to the conversion is dealing with those pesky letters. I have found the easiest way to do this is with a "Map" structure where you equate the hex digits to decimal values. Small Basic makes this extra easy as array's in Small Basic are actually implemented as maps.
I worked up a full example based on your snippet above. You can get it using this Small Basic import code: CJK283
The subroutine below is the important bit. It converts a two digit hex number into its decimal equivalent. It also underscores how limited subroutines are in Small Basic. Rather than a single line for each call as you would see in other languages, where a parameter is passed in and a value is returned, in Small Basic this requires juggling variables inside the subroutine and at least three lines to call the subroutine.
'Call to the ConvertToHex Subroutine
hex = Text.GetSubText(color,2,2)
DecimalFromHex()
red = decimal
Convert a Hex string to Decimal
Sub DecimalFromHex
'Set an array as a quick and dirty way of converting a hex value into a decimal value
hexValues = "0=0;1=1;2=2;3=3;4=4;5=5;6=6;7=7;8=8;9=9;A=10;B=11;C=12;D=13;E=14;F=15"
hiNibble = Text.GetSubText(hex,1,1) 'The high order nibble of this byte
loNibble = Text.GetSubText(hex,2,1) 'The low order nibble of this byte
hiVal = hexValues[hiNibble] * 16 'Combine the nibbles into a decimal value
loVal = hexValues[loNibble]
decimal = hiVal + loVal
EndSub