What Is The Right Foreground For a Given Background

I am working on a SQL Server 2005 Reporting Services (SSRS) report that has differing row colors based on a value in each data row.  The color value is defined in the database.  When I initially created the report, each row had a variable background color but the foreground color was black.  The first time I ran the report, my dark blue background didn’t contrast well with my black foreground.  I quickly realized that I needed a way to vary the foreground color programmatically based on the background color.  After first discussing things over with Nate, here is the expression I came up with for the Color property of the table row:

=IIF(
((
((CInt(Fields!Status_Color.Value) And &HFF) * 299) +
((CInt(Fields!Status_Color.Value) << 8 And &HFF) * 587) +
((CInt(Fields!Status_Color.Value) << 16) * 114)
) / 1000) &lt; 125,
"White",
"Black"
)

Let me explain where this all comes from.  First off, the color that is stored in the database is used by a VB6 program.  VB6 stores colors as BGR and .NET stores colors as RGB (well, technically aRGB).  The first step is to break down the value from the database to its constituent parts (red, green, and blue) using bitshift operations I learned from Keith Peters and then apply the contrast formula I found from Colin Lieberman‘s website. I then determine that if the blackground is a dark color, then we use white and for a light background, black.  This appears to working like a charm.