Blending Colors

One of the projects that I’m working on requires getting the mid-point between two colors.  It’s actually a pretty straight-forward problem with an answer that’s simple enough, so I thought I’d post about it.

So, essentially, we have a set of RGB values, and we need to get the average of those to find our mid-point.  We start by subtracting the second value from the first value, divide that by two, then add the product to the first red value.  The whole thing looks like this:

NewRed = Red1 + ((Red2 - Red1) / 2)

So, if we put this math to work in a RealBasic method to create a new mid-point color from two end-point colors, here’s what we get:

Private Function BlendRGB(C1 as Color, C2 as Color) As Color
  dim newRed as Integer = C1.Red + ((C2.Red - C1.Red) / 2)
  dim newGreen as Integer = C1.Green + ((C2.Green - C1.Green) / 2)
  dim newBlue as Integer = C1.Blue + ((C2.Blue - C1.Blue) / 2)
  Return RGB( newRed, newGreen, newBlue )
End Function

Of course this little method shouldn’t take more than five minutes to create, and could be made smaller by throwing all of the math directly in to the RGB call at the return, but this way you can easily see what’s going on.