CSharp examples for System.Drawing:Color
Calculates a color by applying the specified alpha value to the specified front color.
using System.Drawing; public class Main{ ///------------------------------------------------------------------------------------- /// <summary> /// Calculates a color by applying the specified alpha value to the specified front /// color, assuming the color behind the front color is the specified back color. The /// returned color has the alpha channel set to completely opaque, but whose alpha /// channel value appears to be the one specified. /// </summary> ///------------------------------------------------------------------------------------- public static Color CalculateColor(Color front, Color back, int alpha) {/*w ww . j a v a2 s . c o m*/ // Use alpha blending to brigthen the colors but don't use it // directly. Instead derive an opaque color that we can use. // -- if we use a color with alpha blending directly we won't be able // to paint over whatever color was in the background and there // would be shadows of that color showing through Color frontColor = Color.FromArgb(255, front); Color backColor = Color.FromArgb(255, back); float frontRed = frontColor.R; float frontGreen = frontColor.G; float frontBlue = frontColor.B; float backRed = backColor.R; float backGreen = backColor.G; float backBlue = backColor.B; float fRed = frontRed * alpha / 255 + backRed * ((float)(255 - alpha) / 255); byte newRed = (byte)fRed; float fGreen = frontGreen * alpha / 255 + backGreen * ((float)(255 - alpha) / 255); byte newGreen = (byte)fGreen; float fBlue = frontBlue * alpha / 255 + backBlue * ((float)(255 - alpha) / 255); byte newBlue = (byte)fBlue; return Color.FromArgb(255, newRed, newGreen, newBlue); } }