Sub color2RGB(myColor As Long, RGBComponents() As Byte) RGBComponents(0) = (myColor And &HFF0000) \ &H10000 RGBComponents(1) = (myColor And &HFF00&) \ &H100 RGBComponents(2) = (myColor And &HFF&) End Sub
Sub color2RGB(myColor As Long, RGBComponents() As Byte) RGBComponents(0) = (myColor And &HFF0000) \ &H10000 RGBComponents(1) = (myColor And &HFF00&) \ &H100 RGBComponents(2) = (myColor And &HFF&) End Sub
Function offsetColor(myColor As Long, Optional R As Integer = 0, Optional G As Integer = 0, Optional B As Integer = 0) As Long Dim RGBComponents(2) As Byte Call color2RGB(myColor, RGBComponents()) R = (R + RGBComponents(0)) Mod &HFF If R < 0 Then R = 0 G = (G + RGBComponents(0)) Mod &HFF If G < 0 Then G = 0 B = (B + RGBComponents(0)) Mod &HFF If B < 0 Then B = 0 offsetColor = RGB(R, G, B) End Function
?Hex(offsetColor(myColor:=0, R:=1, G:=2, B:=3)) 30201
MyObject.Color = RGB(255, 0, 0) ' Set the Color property of MyObject to Red.
Egy régi progranyozó kollégámnak elmeséltem, hogy TTCN-3-ban fejlesztek. A fejét fogta, hogy az valami “black magic” lehet, ő marad a C-nél. Hát, a VBA sem semmi állat.
Valami lenne ez TTCN3-ban:
module bitshift
{
type record length (3) of integer IntegerTriplet ;
function color2RGB(in integer myColor, out IntegerTriplet rgbComponents) {
var octetstring myByteTripletString:=int2oct(myColor,3)
rgbComponents[0] :=oct2int(myByteTripletString[0]);
rgbComponents[1] :=oct2int(myByteTripletString[1]);
rgbComponents[2] :=oct2int(myByteTripletString[2]);
}
function RGB(in integer r, in integer g, in integer b) return integer {
return oct2int(int2oct(r,1) & int2oct(g,1) & int2oct(b,1))
}
function offsetColor(
in integer myColor,
in integer r := 0,
in integer g:=0,
in integer b := 0)
return integer
{
var IntegerTriplet rgbComponents;
color2RGB(myColor, rgbComponents)
r:=r+rgbComponents[0]
g:=g+rgbComponents[1]
b:=b+rgbComponents[2]
return RGB(r,g,b)
}
control {
log(int2oct(offsetColor(myColor:=0, r:=1, g:=2, b:=3),3))
}
} // end of module
Jah, a log() eredménye:
Na ezaz – 0x010203 az normális kockának olvasható – a francnak kell fordítva tárolniuk?
Hát, már Gulliver is ezen elmélkedett…
http://en.wikipedia.org/wiki/Endianness 🙂