What Is The Right Foreground For a Given Background

| No Comments

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) < 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.

Leave a comment

Bookmark and Share
Powered by Movable Type 5.02

Twitter Updates

    Follow me on Twitter

    About this Entry

    This page contains a single entry by DrFooMod2 published on May 11, 2009 5:18 PM.

    Request Tracker RT_SiteConfig.pm was the previous entry in this blog.

    Upgraded MT is the next entry in this blog.

    Find recent content on the main index or look in the archives to find all content.