Archived

This topic is now archived and is closed to further replies.

Finding the closest color

This topic is 6676 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Ok, so I''ve got this array of 256 colors (type RGBQUAD). What algorithm should I use to, in this array, find the absolutely closest color to ANY color? Thanks for your time! /mr BiCEPS

Share on other sites
well, if you know the number of the color you''re looking for (0-256) or whatever, you can just look for a matching number. you could do this by creating a look-up table or something.

JoeMont001@aol.com www.polarisoft.n3.net

Share on other sites
  RGBQUAD Colors[256];int FindClosest(RGBQUAD rgb){ int iIndexFound = -1; int iDifferenceFound = 100000; for(int i=0; i<256; i++) { int iDifference = abs(Colors.rgbRed - rgb.rgbRed) + abs(Colors[i].rgbGreen - rgb.rgbGreen) + abs(Colors[i].rgbBlue - rgb.rgbBlue); if (iDifference < iDifferenceFound) { iIndexFound = i; iDifferenceFound = iDifference; } } return iIndexFound;}

Share on other sites
Thanks man!
Worked like a charm!

Share on other sites
Hmm, why does this algorithm looks familiar?

Sludge Software
www.sludgesoft.com
Developing a secret of mana style role-playing-game

Share on other sites
Ah, yes, Baskuenen''s code finds the index of the color with the same *brightness* ,not necessarily the same colour.

(I KNOW, I had this problem too)

FReY

Share on other sites
Baskuenen''s code is calculating the Manhattan distance (it''s really called like that) of two colors. Another way would be to use Euclidian distance ... sqrt((r1-r2)^2 + (g1-g2)^2 +(b1-b2)^2)

The problem is ... there is no ''correct'' way. Both ways of calculating the distance are valid. You might even go further and really use the brightness weighting of the RGB components to get better results:

distance = 0.3 * abs(r1-r2) + 0.6 * abs(g1-g2) + 0.1 * abs(b1-b2)

In the end, whatever works is good ... not a good answer, but so very close to the truth.

MK42

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• Forum Statistics

• Total Topics
633702
• Total Posts
3013450
×