示例代码:
GraphicsWindow.MouseDown = md
Sub md
color = GraphicsWindow.GetPixel(GraphicsWindow.MouseX,GraphicsWindow.MouseY)
EndSub
这将返回一个十六进制值,但我需要将其转换为 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