Desaturate a System.Drawing.Color value

  • Thread starter Thread starter Etienne Boucher
  • Start date Start date
Etienne Boucher said:
Anyone has a quick way to desaturate a single Color value?

How about:

double greyLevel = original.R*0.299+
original.G*0.587+
original.B*0.144;

if (greyLevel > 255)
{
greyLevel = 255;
}

Color desaturated = new Color.FromArgb((byte)greyLevel,
(byte)greyLevel,
(byte)greyLevel);

(The numbers there are taken from a web page, btw - they're supposedly
the NTSC standard weights.)
 
An alternative to Jon's solution would be:

int b=(int)(255*myColor.GetBrightness());
Color desaturatedColor=Color.FromArgb(b,b,b);


--
Bob Powell [MVP]
Visual C#, System.Drawing

The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml
 
Back
Top