Apply alpha to color - CSharp System.Drawing

CSharp examples for System.Drawing:Color

Description

Apply alpha to color

Demo Code

// Copyright (C) Microsoft Corporation. All rights reserved.
using System.Text;
using System.Collections.Generic;
using System;/*from  w ww .  j av  a 2s.c o  m*/
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

public class Main{
        /// <summary>
        /// Apply alpha to color
        /// </summary>
        /// <param name="col">Color</param>
        /// <param name="newAlpha">New alpha</param>
        /// <returns>Color</returns>
        public static Color ApplyAlphaToColor(Color col, float newAlpha)
        {
            if (newAlpha < 0)
                newAlpha = 0;
            if (newAlpha > 1)
                newAlpha = 1;
            return new Color(
                (byte)(col.R),
                (byte)(col.G),
                (byte)(col.B),
                (byte)(newAlpha * 255.0f));
        }
}

Related Tutorials