Getting a color from a percentage in C#
Flashdeck is basically finished now, just a few more additions and bug checks to go. I’ve decided to sell it for 99p with a free trial that lets you make up to two decks with up to five cards each. The full version will have no limits.
Something I implemented into the stats page was a colour changing tile which shows the percentage of total correct answers you have given to all questions. The colour scale of the tile ranges from red (0% correct) to green (100% correct) to quickly show you how your doing in a nice visual way. I thought I would share the code to do this as it could be quite useful. Credit goes to someone on the internet when I did a search for it :P
double red = (Storage.content.totalCorrect <= 50) ? 255 : 256 - (Storage.content.totalCorrect - 50) * 5.12; double green = (Storage.content.totalCorrect >= 50) ? 255 : Storage.content.totalCorrect * 5.12; Color color = Color.FromArgb(200, (byte)red, (byte)green, 0);
That’s it! If you want to convert that to a brush for a Windows Phone Silverlight control, like a rectangle, just use this:
Rectangle.Fill = new SolidColorBrush(color);