C#颜色匹配

时间:2016-06-19 07:58:19

标签: c# colors

你能帮我配色吗

我试图搜索一些没有效果的代码

我的逻辑如下所示,可调节公差,例如最接近颜色的5%或10%:

Red and Light Red = True    
Red and Dark Red = True
Red and black = False

这是我的代码,但它并没有很好地运作

public static bool MatchArgb(int Argb1, int Argb2, int tolerance)
{
    Color c1 = Color.FromArgb(Argb1);
    Color c2 = Color.FromArgb(Argb2);

    return Math.Abs(c1.R - c2.R) <= tolerance ^
           Math.Abs(c1.G - c2.G) <= tolerance ^
           Math.Abs(c1.B - c2.B) <= tolerance;
}

public static bool MatchColor(Color c1, Color c2, int tolerance)
{
    return Math.Abs(c1.R - c2.R) <= tolerance ^
           Math.Abs(c1.G - c2.G) <= tolerance ^
           Math.Abs(c1.B - c2.B) <= tolerance;
}

1 个答案:

答案 0 :(得分:2)

在Paint.NET中检查这是如何完成的,也许是个好主意。我在这里找到了它的克隆和相应的源代码:Pinta/Flood Tool

    private static bool CheckColor (ColorBgra a, ColorBgra b, int tolerance)
    {
        int sum = 0;
        int diff;

        diff = a.R - b.R;
        sum += (1 + diff * diff) * a.A / 256;

        diff = a.G - b.G;
        sum += (1 + diff * diff) * a.A / 256;

        diff = a.B - b.B;
        sum += (1 + diff * diff) * a.A / 256;

        diff = a.A - b.A;
        sum += diff * diff;

        return (sum <= tolerance * tolerance * 4);
    }