Jump to content
  • Advertisement


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


Finding best color match

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Since we are looking for the closest "looking" color, I would expect that the RGB description of a color is all but useless. What we really need is a HSB description of the colors (Hue, Saturation, Brightness). THEN, and only then, do we do a weighted (as in assigning three constants, k, l, and m, to multiply the H, S, and B values by respectively) 3D vector length from desired color to each indexed color in turn.

This should produce much more desireable output. Now, I can't help you with RGB -> HSB conversion, but the find someone who does or even some code that does is trivial.

Share this post

Link to post
Share on other sites
Here is a code from Crystal Space sources, it works good:

int TxtCmapPrivate::find_rgb (int r, int g, int b)
if (r>255) r=255; else if (r<0) r=0;
if (g>255) g=255; else if (g<0) g=0;
if (b>255) b=255; else if (b<0) b=0;

int i, min, mindist;
mindist = 1000L*256*256;
min = -1;
register int red, green, blue, dist;
for (i = 1 ; i < 256 ; i++) // Color 0 is not used, it is reserved for transparency
if (alloc[i])
red = r - rgb_values[(i<<2)+0];
green = g - rgb_values[(i<<2)+1];
blue = b - rgb_values[(i<<2)+2];
dist = (299*red*red) + (587*green*green) + (114*blue*blue);
if (dist == 0) return i;
if (dist < mindist) { mindist = dist; min = i; }
return min;

The trick is to make difference between colors so distance calculations will be more accurate.


Share this post

Link to post
Share on other sites
HSB space does seem more appropriate. I'll have to look for conversion routines, but in that space I don't think you can use simple 3D vectors, because that color space is not a nice cube like RGB is; it is a crazy double ended cone, which seems pretty scary at first glance anyway.

The Crystal Space example looks about the same as Allegro, but does anyone know where the 'magic numbers' come from? CS uses 299, 587, 114, but allegro uses 30*30, 59*59, and 11*11. I guess it makes some sense in that humans discern green more than other colors, so its weighted more, but why are the numbers different? Maybe I'll run across the answer looking at the color space conversion routines.

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
This maybe out of topic here...have a look.
This is from a book called 'Programmer's Guide to the EGA and VGA Cards'....
gray scale = (0.30 x red) +
(0.59 x green) +
(0.11 x blue)
In creating the gray scale, 30 percent of the red intensity is added to 59 percent of the green intensity and added to 11 percent of the blue intensity. Because the resultant intensity is equal to 100 percent of the intensity of the three colors, the intensity of a gray scale will result in a gray scale of the same value. For example, assume that the three color values are 40, 40, 40 for red, green, blue. This produces a gray scale of intensity 40. The resultant gray scale would be (.30 x 40) + (.59 x 40) + (.11 x 40)
= 1.0 x 40 = 40..............

What i found out from CS example above is that 299 almost = 30% x 1000L, 587 almost = 59% x 1000L, 114 almost = 11% x 1000L.......

that's all i can help here.

Share this post

Link to post
Share on other sites
Now that we've really beaten this to death ...
__I believe, Rock2000, that you may also want to make the images look good. The next logical step would be to implement a dithering routine.
__Preferably it would be error diffusion; unfortunately I know zilch about it. Perhaps there's a thread in the message board. I couldn't find one, though. Maybe someone could start a new thread on dithering ! [hint, hint]

Share this post

Link to post
Share on other sites
The day I find a good article, source, tutorial about COLOUR dithering will be the happiest day of my life. Dithering from RGB24 to PALETTE8 is a pain! You have to do the colour palette conversions AND the dithering sort of at the same time, or else you'll remove colours that are essential to the dithering process. If you dither first, you have too many colours and have to do palette matching, which might cause your dithering to sharpen up or soften up, ruining your image.

Do check out:

A Description of Using Error Diffusion for Graphics


Sloppy's palette selection

Sloppy's palette selection concept is pretty amazing, and works quite well (given the sample images) as far as I can implement it.. but dithering is still needed - I wish I knew how photoshop does it because Adobe has made a really nice weighted dithering scheme..

Share this post

Link to post
Share on other sites
I need a routine to search a palette and find the color that best matches the input RGB value. I would normally have thought that the best color would be the one that has the smallest distance in the color cube (3D cube, 1 unit around, with x,y,z representing red,green,blue, in whatever order).

However, I've looked at the source code for Allegro because it has lookup tables, which I figured I'd use, but it seems to use some sort of weighted squares approach that I don't understand (and there is pretty much no comments). What is the best approach, or where are any good explanations/algorithms on this?


Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!