Multiply colors - CSharp System.Drawing

CSharp examples for System.Drawing:Color Calculation

Description

Multiply colors

Demo Code


using System.Text;
using System.Collections.Generic;
using System;//from  ww w . j ava 2  s  .  c  o  m
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

public class Main{
      /// <summary>
      /// Multiply colors
      /// </summary>
      /// <param name="color1">Color 1</param>
      /// <param name="color2">Color 2</param>
      /// <returns>Return color</returns>
      public static Color MultiplyColors(Color color1, Color color2)
      {
         // Quick check if any of the colors is white,
         // multiplying won't do anything then.
         if (color1 == Color.White)
            return color2;
         if (color2 == Color.White)
            return color1;

         // Get values from color1
         float redValue1 = color1.R / 255.0f;
         float greenValue1 = color1.G / 255.0f;
         float blueValue1 = color1.B / 255.0f;
         float alphaValue1 = color1.A / 255.0f;

         // Get values from color2
         float redValue2 = color2.R / 255.0f;
         float greenValue2 = color2.G / 255.0f;
         float blueValue2 = color2.B / 255.0f;
         float alphaValue2 = color2.A / 255.0f;

         // Multiply everything using our floats
         return new Color(
            (byte)(StayInRange(redValue1 * redValue2, 0, 1) * 255.0f),
            (byte)(StayInRange(greenValue1 * greenValue2, 0, 1) * 255.0f),
            (byte)(StayInRange(blueValue1 * blueValue2, 0, 1) * 255.0f),
            (byte)(StayInRange(alphaValue1 * alphaValue2, 0, 1) * 255.0f));
      } // MultiplyColors(color1, color2)
}

Related Tutorials