Mix two Color - CSharp System.Drawing

CSharp examples for System.Drawing:Color

Description

Mix two Color

Demo Code


using Windows.UI.Xaml;
using Windows.UI;

public class Main{
        // Original taken from http://stackoverflow.com/a/9955317/6843321
        public static Color Mix(this Color background, Color foreground)
        {/*from w ww  .  jav  a  2  s  .  c om*/
            if (foreground.A == 0xFF)
            {
                return foreground;
            }
            else if (foreground.A == 0x00)
            {
                return background;
            }

            float alpha      = foreground.A / 255.0f;
            float difference = 1.0f - alpha;

            byte r = (byte)((foreground.R * alpha) + (background.R * difference));
            byte g = (byte)((foreground.G * alpha) + (background.G * difference));
            byte b = (byte)((foreground.B * alpha) + (background.B * difference));

            return Color.FromArgb(0xFF, r, g, b);
        }
}

Related Tutorials