0

Imagine I have random colors with their hue, saturation and value. How would I make an algorithm to order those colors from red to purple so that similar colors are next to each other?

Sorting them by hue would not be enough because I could have this case :

Color 1 : Hue = 1°, Saturation = 100%, Value = 100% : Red

Color 2 : Hue = 2°, Saturation = 10%, Value = 10% : Basically black with a hint of red

Color 3 : Hue = 3°, Saturation = 100%, Value = 100% : Red but a little more orange

For this setup, I would like Color 1 and Color 3 to be next to each other

I thought about using the distance between colors with the Pythagorean theorem in 3D but I'm not sure how to make an actual algorithm with it.

Would it work to start with the lowest hue, and then finding the closest color, and then the closest to the next one ? Or would it sometimes lead to a non ideal sort for some cases ? Is it analogous to the traveling salesman problem?

  • 2
    How would you sort points in 3-dimensional space? Imagine a cloud of points? Do you see a way to map a 3D set to a 1D collection? You'll always have cases that are not "nice". – trincot Jun 16 '22 at 07:03
  • Look into space-filling curves. You'll be able to map N-dimensional space to 1 dimension, often optimizing for locality. – Dillon Davis Jun 16 '22 at 07:17
  • Red to purple may be viewed as a linear function? Then for every color you have find the closest point on the line? – Jean-Baptiste Yunès Jun 16 '22 at 07:48
  • 1
    I would recommend trying something like https://developers.google.com/optimization/routing/tsp. I would add points `[0, 100, 100]` and `[90, 100, 100]` to your points as `start` and `end`, then add another fake point `tunnel` which is close to both start and end but very far from everything else. This will force the path to go `end` through `tunnel` to `start, then the rest will be your desired order. All other distances are Euclidean. – btilly Jun 16 '22 at 21:48
  • Without the "red to purple" constraint you end up with the TSP (traveling salesman problem). Just add a dummy "point" that has distance 0 to all other points, solve the TSP and then the dummy point marks the start/end. How to measure the distance between two colors also has to be defined. – maraca Jun 17 '22 at 15:08

0 Answers0