SmallBasic:如何将getpixel函数的十六进制值转换为rgb值?
问题描述:
示例代码:SmallBasic:如何将getpixel函数的十六进制值转换为rgb值?
GraphicsWindow.MouseDown = md
Sub md
color = GraphicsWindow.GetPixel(GraphicsWindow.MouseX,GraphicsWindow.MouseY)
EndSub
这会返回一个十六进制值,但我需要将其转换为RGB值。我该怎么做?
答
转换的诀窍是处理那些讨厌的字母。我发现最简单的方法是使用“地图”结构,将十六进制数字等同于十进制值。 Small Basic使得这非常容易,因为Small Basic中的数组实际上被实现为地图。
我根据你上面的代码片段编写了一个完整的例子。您可以使用此Small Basic导入代码获得它:CJK283
下面的子例程是重要的一点。它将一个两位数的十六进制数转换为十进制数。它还强调了Small Basic中有限的子例程。对于每次调用,您将看到其他语言(而参数被传入并返回一个值),而不是每个调用的单行。在Small Basic中,这需要在子例程内部调用变量,并且至少需要三行来调用子例程。
'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