我正在使用以下函数来增亮颜色值(它是我的代码中的 lambda,但这不应该有所不同):
Function ReduceDistanceTo255(ByVal i As Byte) As Byte
Return i + (255 - i) \ 2
End Function
它不会编译,因为编译器将255
and解释2
为整数而不是字节,从而产生 type 的结果Integer
。不幸的是,没有字节类型的字符,所以我不能只写255B
或类似的东西。
该问题有一些明显的解决方法:
Function ReduceDistanceTo255(ByVal i As Byte) As Byte
Return i + (CByte(255) - i) \ CByte(2)
End Function
和
Function ReduceDistanceTo255(ByVal i As Byte) As Byte
Return CByte(i + (255 - i) \ 2)
End Function
和
Function ReduceDistanceTo255(ByVal i As Byte) As Byte
Dim FF As Byte = 255
Dim two As Byte = 2
Return i + (FF - i) \ two
End Function
第一个只是丑陋且难以阅读,因为每个文字都需要CByte
d。第二个以整数进行计算,然后将结果转换为字节,这还可以,但不如纯字节操作那么优雅。第三种解决方法不需要CByte
s,但它的缺点很明显。
我是否错过了一些(优雅的)第四个选项,它允许我做 Byte-only-math 而不会用 CBools 弄乱我的公式?