将ARGB转换为RGB而不会丢失信息

时间:2012-03-30 16:47:58

标签: c# windows-phone-7 colors

我尝试将argb值转换为rgb值而不会丢失它从背景中获取的信息。 所以例如:背景是黑色的,argb是(150,255,0,0),结果我不会有一种棕色。

有没有机会处理这个问题?

2 个答案:

答案 0 :(得分:7)

你可以计算

foreground * alpha + background * (1-alpha)

作为红色,绿色和蓝色通道的新颜色。请注意,我在表达式中使用了alpha缩放到01

答案 1 :(得分:6)

    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));
    }

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