Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert ARGB to RGB without losing information

i try to convert a argb value to a rgb-value without losing the information it got from his background. so for example: the background is black, the argb is (150,255,0,0) and as the result i wan't to have a kind of brown.

is there any chance to deal with that?

like image 706
roqstr Avatar asked Mar 30 '12 16:03

roqstr


2 Answers

    public static Color RemoveAlpha(Color foreground, Color background)
    {
        if (foreground.A == 255)
            return foreground;

        var alpha = foreground.A / 255.0;
        var diff = 1.0 - alpha;
        return Color.FromArgb(255,
            (byte)(foreground.R * alpha + background.R * diff),
            (byte)(foreground.G * alpha + background.G * diff),
            (byte)(foreground.B * alpha + background.B * diff));
    }

from http://mytoolkit.codeplex.com/SourceControl/latest#Shared/Utilities/ColorUtility.cs

like image 196
Rico Suter Avatar answered Nov 06 '22 11:11

Rico Suter


You may calculate

foreground * alpha + background * (1-alpha)

as your new color for channels red, green and blue. Note that I used an alpha scaled to 0 to 1 in the expression.

like image 27
Howard Avatar answered Nov 06 '22 10:11

Howard