Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Best Way to Darken a Color Until Its Readable

Tags:

c#

c#-4.0

Whats the best way to darken a color until it is readable? I have a series of titles are have an associated color, but some of these colors are very light and any text drawn in them is unreadable. I've been messing around with HSB and I can't seem to get an algorithm down that darkens the color without making it look silverish.

I've basically just been doign this, but it doesn't seem to get what I would call "good" results:

Color c =  
  FromHSB(
    orig.A,
    orig.GetHue(),
    orig.GetSaturation(),
    orig.GetBrightness() > .9 ?
      orig.GetBrightness() - MyClass.Random(.5, .10)
      : orig.GetBrightness());

I think I want to alter the saturation too. Is there a standard way of doing this?

like image 333
Mark Avatar asked Jan 29 '12 05:01

Mark


1 Answers

I basically just hacked together a randomizer that adds components to the RGB values if the sum of the RGB values is too low, or any one item is too low. Its a non-rigourous way to do it, but it seems to produce good results.

double threshold = .8;

for (int j = 0; j < 3; j++)
{
  if (color.GetBrightness() > threshold)
  {
    color[j] -= new MyRandom(0, 20/255);
  }                   
}
like image 181
Mark Avatar answered Oct 05 '22 15:10

Mark