RGB and 256-color palette matching

Quote:

>Hi! i am in need of an algorithm to match a given R, G, B bytes values

>into one of the entries in a 256-color palette. Say the 256-color

>palette is already set with all the range covered. What I want to do

>here is to match a given byte values, R, G, B, into one of the colors

>of the palette. In short, determine the nearest possible RGB color out

>of the 256 possible colors available for the system. anyway, thanks!!!

IIRC this can be done by taking the sum of the squares of the differences

and finding the smallest. That is to compare two rgb values like x and

y, you calculate sqr(x.r-y.r)+sqr(x.g-y.g)+sqr(x.b-y.b). Then find the

color for which the above is smallest. There are better alternatives but

that should be good enough for most cases. Remember to use a signed type

like integer or even longint.

Also you should really use dithering with it.{*filter*}choose the color then

disperse the error and move to the next pixel.

Osmo