add and sum two colors. - CSharp System.Drawing

CSharp examples for System.Drawing:Color

Description

add and sum two colors.

Demo Code


using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.Drawing;
using System.Collections.Generic;
using System;/*  w ww  .  ja v a2s  . c om*/

public class Main{
        /// <summary>
        /// Method to add/sum two colors.
        /// </summary>
        /// <param name="hitColor"></param>
        /// <param name="tintColor"></param>
        /// <returns></returns>
        public static Color AddColor(Color hitColor, Color tintColor)
        {
            float brightness = tintColor.GetBrightness();
            var result = Color.FromArgb(
                    (int)Cap((int)((1 - brightness) * hitColor.R) + CapMin(tintColor.R - 20, 0) * 255 / 205, 255),
                    (int)Cap((int)((1 - brightness) * hitColor.G) + CapMin(tintColor.G - 20, 0) * 255 / 205, 255),
                    (int)Cap((int)((1 - brightness) * hitColor.B) + CapMin(tintColor.B - 20, 0) * 255 / 205, 255)
                );
            return result;
        }
        private static int CapMin(int x, int min)
        {
            return x < min ? min : x;
        }
}

Related Tutorials