views:

39

answers:

1

Let's say I have color 'FOO', and it is stored in RGB format. I need to recolor 'FOO' so it matches the closest color in a list of colors. Doing this on the fly, couldn't I view the RGB values from each color as points on a 3D grid (r=x, g=y, b=z) and compute the distance between point 'FOO' vs the points from each color in the list?

The closest point to 'FOO' would be the replacement color?

+1  A: 

In theory, yes. In reality, computing the closest color is non-trivial if you want to do it well. Just for example, people's eyes are much more sensitive to changes in brightness than color shifts, especially toward the ends of the color range (i.e., toward extreme reds or blues).

At least if you don't mind some extra work in the computation, you'll want to use one of the standard "delta E" computations (in your case, you'll want to minimize delta E). Note that these all (all I've worked with anyway) work in the CIE L*a*b* color space. In a typical case, you'll start with RGB, which you'll need to convert to L*a*b* first.

Jerry Coffin
Would this work if I'm targeting a custom palette? E.g, original image contains 8 different colors ( red, green, yellow, peach, etc.) whereas the target palette contains black, white, and 4 shades of blue?
Jeffrey Kern
@Jeffrey: Yes, with the proviso that it's doubtful anything will work very well in that kind of situation -- to the point that it might be a bit of a waste of time. OTOH, with a palette of only 8 colors, you can pre-compute the output for each of the 8 possible inputs, and convert each pixel as a trivial table lookup.
Jerry Coffin
@Jeffrey: also, instead of just picking the *first* equal match as you mentioned in your comment, you might want to keep track of an accumulated error, and pick the one that minimizes the overall error (c.f., Floyd-Steinberg dithering: http://en.wikipedia.org/wiki/Floyd%E2%80%93Steinberg_dithering).
Jerry Coffin
@jerry: the colors in the source image will change with the image loaded but for the most part the target palette won't. I'm just looking for a solution that will allow me to recolor images on the fly when they're rendered wit GDI+. It's a waste making 6 images for 1 object when u can recolor haha. And if I have 100 objects I'd rather not handle 600 images (6 different target palettes including original coloring)
Jeffrey Kern