Click here to Skip to main content
15,879,348 members
Articles / Programming Languages / C#
Tip/Trick

Color tinting algorithm in C#

Rate me:
Please Sign up or sign in to vote.
4.00/5 (5 votes)
19 Nov 2009CPOL 18K   5  
I love Code Project, this site has helped me quite a number of times. So whenever I have something I feel is worthwhile I will post it.The other day I needed an algorithm to tint one color with another by a percentage amount. So here it is if anyone needs it. private static Color Tint(Color

I love Code Project, this site has helped me quite a number of times. So whenever I have something I feel is worthwhile I will post it.

The other day I needed an algorithm to tint one color with another by a percentage amount. So here it is if anyone needs it.

private static Color Tint(Color source, Color tint, decimal alpha)
{
  //(tint -source)*alpha + source
  var red = Convert.ToInt32(((tint.R - source.R) * alpha + source.R));
  var blue = Convert.ToInt32(((tint.B - source.B) * alpha + source.B));
  var green = Convert.ToInt32(((tint.G - source.G) * alpha + source.G));
  return Color.FromArgb(255, red, green, blue);
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --