user1008784
user1008784

Reputation:

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?

Upvotes: 5

Views: 2158

Answers (2)

Joe Zack
Joe Zack

Reputation: 3308

ColorMine is open source C# library that has methods for converting between color spaces and comparing via a couple delta-e methods

For example, this will give you a similarity score based on the most common delta-E method (Cie76)

var a = new Rgb { R = 23, G = 117, B = 114 }
var b = new Rgb { R = 113, G = 27, B = 11 }
var deltaE = a.Compare(b,new Cie1976Comparison());

Upvotes: 1

Nicholas Carey
Nicholas Carey

Reputation: 74267

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).

Upvotes: 3

Related Questions