how do I sort dictionaries by value then by key?

What is the correct way to sort colors when calculating a median value?

  • Unlike with grayscale values there are many ways how to sort a set of colors: luminance, hue, saturation etc. The common way is probably to sort the rgb, yuv or lab components separately and recombine them, but then the resulting color is very likely not in the original set. And sorting them numerically (24 bit rgb values for example) somehow doesn't feel correct either.

  • Answer:

    If you are after the central tendency of a set of colors and want to pick one of the set as its representative, I'd compute all pairwise distances in the set and pick the color whose sum of pairwise distances is the smallest (this is the same as picking the color with the lowest mean distance to all other colors). To make the distances more visually meaningful I would use a color space like CIE LAB (or, even better, CIE CAM02).

Ján Morovič at Quora Visit the source

Was this solution helpful to you?

Other answers

to continue Jan's answer: ... and if you insist on finding the median rather than the mean, you can approximate it by raising Jan's pairwise distances to the power of a number between 0 and 1, call it Gamma, for example Gamma=0.1 or 0.01. Distance_for_color(a) =     Power(         Sum[ over all colors b](               Power(Distance_between_two_colors(a,b), Gamma)                                             ),        (1/Gamma)                ).

Yoav Bressler

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.