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



我尝试将argb值转换为rgb值,而不会丢失从他的背景中获得的信息。例如:背景是黑色,argb是(150255,0,0),因此我不想有一种棕色。

有机会解决这个问题吗?

    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

您可以计算

foreground * alpha + background * (1-alpha)

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

最新更新