65.9K
CodeProject is changing. Read more.
Home

Color tinting algorithm in C#

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (5 votes)

Nov 17, 2009

CPOL
viewsIcon

18291

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);
}