Path: utzoo!mnetor!uunet!husc6!cmcl2!brl-adm!umd5!purdue!decwrl!hplabs!hpl-opus!hpccc!hp-sde!hpfcdc!bayes From: bayes@hpfcdc.HP.COM (Scott Bayes) Newsgroups: comp.sys.mac.programmer Subject: Re: Choosing closest-color-by-blending Message-ID: <11550002@hpfcdc.HP.COM> Date: 26 Apr 88 21:30:53 GMT References: <50841@sun.uucp> Organization: HP Ft. Collins, Co. Lines: 46 >In article <3184@coherent.com> dplatt@coherent.com (Dave Platt) writes: >> >> The Problem >> >>Given a color palette of N colors, each of whose (R,G,B) values are >>known to a reasonable precision, select a group of 4 of those N colors, >>such that the average of these 4 colors' (R,G,B) values falls close to >>a specified (R',G',B') color. > >Much of Dave's posting hacked away ... > >I have been doing some work on an IBM-PC's EGA adaptor that has run me >into this same brick wall, with the slight modification that I have a >choice of up to 16 colors from a palette of 64 ... yes, pretty weak, but >that is the hardware I have to work with; so, if anyone has any >references not only on how to select from the N colors, but also how to >select WHICH n colors, post (or email) them also ... >-- > Greg Limes [limes@sun.com] frames to /dev/fb The solution we use in the Series 300 under Pascal Workstation OS is proprietary, so I can give you the method but not the code. (When I say add x to y, I mean add red component of x to red component of y, green of x to green of y, and blue of x to blue of y, storing result in y. Rx is shorthand for "red component of x".) We work only in RGB, not in HSL. Create two color vectors, RGB and rgb. Initialize RGB to R=0,G=0,B=0. Initialize rgb to r=0,g=0,b=0. "Desired color" means the RGB color you wish to approximate. Now do 4 times: Add desired color to RGB find clut entry x such that (Rx+r-R)^2+(Gx+g-G)^2+(Bx+b-B)^2 is minimal remember index of x add x to rgb. rgb is now the closest approximation to RGB available in the clut. It gets a little slow for large cluts (eg 256 entry), but gives very good results, and maps a desired color that's found in the clut to that entry. Scott Bayes bayes@hpfclw