Some help through Palette Hell...

Writer: Matthew River Knight

Coders using SCREEN 13 often find it necessary to check within the VGA
palette to see which RGB values will most closely match any given RGB values.
Ideally, we could use a formula to solve this problem for us. Say, for
example, I wanted to find the closest match for 62;10;6, our formula might
find something like 60;10;6.

Fortunately for us, such a formula does exist. To tell how 'far' one color
is from another, just use the distance formula:

Distance = SQR((R2-R1)^2 + (G2-G1)^2 + (B2-B1)^2)

The two colors are R1,G1,B1 and R2,G2,B2. 

Of course you don't necessarily *have to* have a square root if you only
want to find the closest match for a color in an array. Just use its normal.
Here's an example:
Let's say we want to find the closest match for the color (R,G,B) in the
R(),G(),B() array, which contains 16 colors. This is commonly used in
dithering. Here's some example code:
Distance = 32767
FOR A = 0 TO 15
  RD = R(A)-R
  GD = G(A)-G
  BD = B(A)-B
  Normal = RD*RD + GD*GD + BD*BD
  IF Normal