You got an OverflowException
"Arithmetic operation resulted in an overflow.". The problem is that the A, R, G, B values are bytes and those can only have values between 0 and 255. Adding two of them might exceed this range. Therefore convert them to integers before adding them.
Public Function BlendColors(ByVal c1 As Color, ByVal c2 As Color) As Color
Return Color.FromArgb((CInt(c1.A) + CInt(c2.A)) \ 2, _
(CInt(c1.R) + CInt(c2.R)) \ 2, _
(CInt(c1.G) + CInt(c2.G)) \ 2, _
(CInt(c1.B) + CInt(c2.B)) \ 2)
End Function
Note: If you have the Strict
option set to On
, which is a good thing, VB will not do all conversions automatically as did VB6. Why is this good? Because you know what you are doing. Otherwise the compiler would introduce conversions you are not aware of and which might be unexpected or even harmful. VB still does widening conversions when it is known that a larger type is appropriate. For instance, you could simplify the above calculation by converting only one of the terms: CInt(c1.R) + c2.R
. In this case c2.R
will automatically be converted to Integer
to make it match the type of CInt(c1.R)
.