Search code examples
c#imagebitmappixel

Setting every pixel in image to closest match from a list of Colors


How can I set the colour of every pixel in a image to it's closest colour match from a list of colours in RGB format (no alpha), that can be of any length, in C#?

It's basically creating a custom BitmapPalette, but since you can't do that (Trust me, I've tried everything possible for that), I need an alternative.

Does anyone know a way to do this?


Solution

  • Boy...I hope you loves your maths...

    This is a tough question. To determine the "closeness of fit" between two colors, you first must understand the color space/color model in which your are working. The RGB color model (not counting the alpha channel) is essentially Euclidean in nature: each color maps to a point in 3D space. Ergo, the putative distance between two colors, C1 and C2 is

    Distance = SQRT( (C1red - C2red)2 +  (C1green - C2green)2 +  (C1blue - C2blue)2 )

    WRT "normal" human visual perception, this is not necessarily correct. To take that into account gets much more complicated.

    Try these two papers as jumping-off points:

    The Color FAQ also provide many links to other colorspace resources.

    Some more links at http://www.golden-gryphon.com/software/misc/color-links.html

    Here's a paper on color differences that might help also: http://www.axiphos.com/Reports/ColorDifferences.pdf

    Bruce Lindbloom's web site has lots of stuff as well, including a color difference calculator, that works in the CIE color space (which has provision for distance computations).