nevermind that nearly anything involving actual code (C or C++ or Java
or whatever) will probably be considerably faster than doing it using
SQL queries.
typically, when I do stuff based on colors, I tend to make the
simplifying assumption that color-distance==euclidean-distance, so, it
is finding the color with the smallest value for
(R1-R0)^2+(G1-G0)^2+(B1-B0)^2.
as for nearest neighbor:
a lot will depend on the number of colors in question (to match against);
for a small number of colors, a linear search is likely to be fairly
effective;
tree-based strategies make more sense for larger numbers of colors, but
may themselves be fairly costly if the number of colors is fairly small
(say, due to having larger constant factors).
an example of a tree-building strategy would be:
if the pool is less than N items, build a leaf holding these items;
find the estimated center-point (say, the average of the colors);
find a good dividing-plane normal-axis (several strategies exist, 1);
split the collection in half, based on which side of the plane the item
is on;
build a node holding the plane and the results of repeating this process
for each sub-collection.
1: a few strategies:
add the squared distances from the origin, for each axis, and pick the
axis with the greatest value;
like the above, but simply treat this value as a vector (normalize and
use as axis);
produce a 3x3 matrix, [[XX,XY,XZ], [YX,YY,YZ], [ZX,ZY,ZZ]], and pick the
row with the greatest length;
...
finding the nearest point with such a tree is basically done by walking
the tree (going down the side which the point is on), where one can note
that the closest point on the opposite side of a plane will be the
distance from the plane, meaning that when walking back up the tree, the
opposite side of the tree can be ignored if the distance to the plane is
>= that of the currently nearest point.
note: this is probably overkill for color-matching tasks.
or such...