Wednesday, May 5, 2010

ASP.NET : Change forecolor based on backcolor

One of the applications we were developing in asp.net needed use of random background colors for table cells. This was easy in vb.net
Just create an array of allcolors like this :

   Dim allColors As String()
Now populate the array using this :
   allColors = [Enum].GetNames(GetType(System.Drawing.KnownColor))

and then use this array for randomly coloring the cell backcolor.

However, a new problem arose. It was found that if the forecolor is retained as black and the backcolor is a dark color, cell text is not readable.  This can be handled by using the cell.backcolor.getBrightness property. Change the forecolor to white if brightness is less than 0.5 otherwise retain forecolor as black.