#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmers Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use it's exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# Finding the closest match...

Guest
I nedd help on finding the closest match.... I have a 8 bit normal VGA-screen, and I need to find the closest match for a color, in the current palette. I have prepared already a basic one, but it doesn't seem to work right... It goes wuite close, but I there would even be closer ones... Could this be easier done with HSV-colorspace?

· ·

• Guest
Umm... maybe I'm just a bit dense, but I don't quite understand

what your asking... Are you trying to convert an 8-bit color to a

15,16, or 24-bit Direct Color value? I've got some source for that

if you want it. Just post back...

URL:http://acheronx.ml.org/home/

· ·
• Guest
Hi,

For what I understand from your message, I think you want to be able to take a generic RGB color, and find the closest match for it in your current 256 color palette. I haven't tested this yet, but you could try the following:

`FindClosestColor(int r, int g, int b){   int p, d, BestCandidate=0, LeastDifference=999;   for(p=0; p<256; p++)<br>   {      d =  abs(MyPalette[p][0] - r);      d += abs(MyPalette[p][1] - g);      d += abs(MyPalette[p][2] - b);      if(d < LeastDifference)      {         LeastDifference = d;         BestCandidate = p;      }   }   return BestCandidate;}`

This might not work very well in some cases, because two palette entries with the same difference from the desired RGB color are not handled properly, so you can still get a color which might not be the most alike. The above code is just something I thought of right now, so I don't know what the results would be, or if there might be other (better) methods to find the closest match (probably yes). I hope to have been of some help.

Greets,

Mark

· ·
• Guest
: I nedd help on finding the closest match.... I have a 8 bit normal VGA-screen, and I need to find the closest match for a color, in the current palette. I[D

Simple take the square of the differences and it

will work (i think)

· ·
• Guest
: d = abs(MyPalette[p][0] - r);

: d += abs(MyPalette[p][1] - g);

: d += abs(MyPalette[p][2] - b);

: This might not work very well in some cases, because two palette entries with the same difference from the desired RGB color are not handled properly, so you can still get a color which might not be the most alike. The above code is just something I thought of right now, so I don't know what the results would be, or if there might be other (better) methods to find the closest match (probably yes). I hope to have been of some help.

What should improve things a lot is to take the

square of those values. Thus:

d = abs(MyPalette[p][0]-r)^2;

d += .... ^2;

etc.

This way all three of the values are close and

peek-values are reduced.

DaCoTa

· ·
• Guest
I have the same problem at the moment and

I also think the best would be to convert

it into HSB or HSV format .

Here's a bit better method :

-Do it like Mark but declare seperate ints

(rdiff,gdiff and bdiff) instead of adding everything to d

-Find out the highest color(r,g or b)

-Divide the color difference of the highest

color by 15 (change this number and look

what happens)

-Set d = 0 and add all differences to d

This method is better because the brightness

of the color isn't so important(a dark red is more

equal to a bright red than a bright green)

But it doesn't work with gray !

· ·