Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two lists of colors

Tags:

c#

Lets say I have two lists of colors and I need to compare them. I have a function of comparing colors but I'm a little bit confused of types which function gets. How to cast them?

public bool AreColorsSimilar(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;
}

Here is my first list:

public static List<Color> PaletteOfSeasons()
{
    List<Color> springColors = new List<Color>();

    springColors.Add(ColorTranslator.FromHtml("#80a44c"));
    springColors.Add(ColorTranslator.FromHtml("#b4cc3a"));


    return springColors;
}

And in another list I'm pulling pixels from image:

public static IEnumerable<Color> GetPixels(Bitmap bitmap)
{
    for (int x = 0; x < bitmap.Width; x++)
    {
        for (int y = 0; y < bitmap.Height; y++)
        {
            Color pixel = bitmap.GetPixel(x, y);
            yield return pixel;
        }
    }
}

And question is, how can I compare this colors ?

like image 266
cygnus Avatar asked Jan 19 '16 14:01

cygnus


People also ask

How do you compare colors?

The most common method would be a visual color comparison by looking at two physical color samples side by side under a light source. Color is very relative, so you can compare colors in terms of the other color across dimensions such as hue, lightness and saturation (brightness).

How do you find the difference between two colors?

In order to measure the difference between two colors, the difference is assigned to a distance within the color space. In an equidistant-method color space, the color difference ∆E can be determined from the distance between the color places: ΔE = √ (L*₁-L*₂)² + (a*₁-a*₂)² + (b*₁-b*₂)².

How do you compare two RGB values?

If you have two Color objects c1 and c2 , you can just compare each RGB value from c1 with that of c2 . int diffRed = Math. abs(c1. getRed() - c2.


1 Answers

If I understand you right:

var springColors = null;
springColors = PaletteOfSeasons(springColors);

var similarColors = GetPixels(bitmap).Intersect(springColors, new ColorComparer(tolerance));

And you need this class:

public class ColorComparer : IEqualityComparer<Color> 
{
    private _tolerance;

    public ColorComparer(int tolerance)
    {
        _tolerance = tolerance;
    }

    public bool Equals(Color x, Color y)
    {
        return AreColorsSimilar(x, y, _tolerance);
    }

    public int GetHashCode(Foo x)
    {
        return 0;
    }

    private bool AreColorsSimilar(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;
    }
}

P.S. Your method PaletteOfSeasons is a little confusing. Passing list to method foolishly.

P.P.S. Use Bitmap.LockBits() to increase code performance.

P.P.P.S. Such implementation of GetHashCode isn't good. But in our situation it is OK.

like image 147
fryday Avatar answered Oct 24 '22 16:10

fryday