> I've started a project where I need to sort a list of colors, by
> color...So far, sorting using HSV values seems to be best,
> but there are odd gaps and jumps...

There will always be the potential for gaps and jumps, because color
is not a one-dimensional entity. Typically it is modelled in three
dimensions. (REALbasic supports three such models directly -- RGB,
CMY, and HSV -- but there are many others.)

Trying to sort colors is like trying to sort geographic locations: is
the location of San Francisco "greater" than the location of Boston?
The question is meaningless until you define a way to compare two
entities that results in a single number (such as taking a location's
latitude, or its longitude, or its distance from a reference
location).

You can treat colors in the same way, by considering the "distance"
from the color in question to a reference color. For example, imagine
the RGB model as representing a color cube, 256 units per side, with
black at one vertex (0,0,0) and white at the opposing vertex
(255,255,255). You can calculate the distance from any color to any
other color within this cube by using the Pythagorean theorem:

  Function RGBColorDistance(Color1 as Color, Color2 as Color) as Double
    return (Color1.Red - Color2.Red) ^ 2 _
           + (Color1.Green - Color2.Green) ^ 2 _
           + (Color1.Blue - Color2.Blue) ^ 2
  End Function

(Of course this actually returns the _square_ of the Cartesian
distance, but since we are only interested in the distance relative to
other distances, we save CPU time by not bothering to take the square
root.)

The task of "sorting" a list of colors then becomes a matter of
calculating the distance from each color to reference color (such as
white), and sorting the color list by this distance. There is no way
to avoid jumps in the result unless you restrict the list of input
colors to say, colors with only red in them. In some cases (such as
color palette optimization), you might need to know only the closest
color. In the situation you describe, it might be best to offer
multiple ways to view and sort the color list.

You can also use other color models when calculating color distance,
including ones better matched to human perception. HSV is better than
RGB in this respect. (For one thing, the human eye gives more weight
to Green than to the others.) But you must take care to scale the
dimensions compatibly so that each has appropriate weight. In RGB,
each dimension is at least uniform and identical to the others.

Here's a paper that goes into some more detail (including describing a
fairly complicated perceptual color model and distance calculation):

  http://mmis.doc.ic.ac.uk/mmir2005/CameraReadyMissaoui.pdf

Googling for "color distance", "color reduction", or "palette
reduction" might also turn up interesting material.

lj
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Reply via email to