开发者

Sorting items by their Color

I have a basket of flowers and I want them to tag and sort by their Color. Any ideas on how this can be done, will be very helpful.

Thanks m开发者_开发问答uch for your time.


The most obvious solution would seem to order by hue.

This would give you a stable sorting comparison operator that would sort your array in this order:

Sorting items by their Color

Note that some RGB colors are degenerate in the sense that they don't have a single HLS form. White is such an example. It's up to you how you're going to handle them.


You can do this using the concept of Lexicographic ordering.

Basically, given the HSL representations of 2 colors (say C1 = (H1, S1, L1) and C2 = (H2, S2, L2)), we can define C1 < C2 if and only if ((H1 < H2) || (H1 == H2 && S1 < S2) || (H1 == H2 && S1 == S2 && L1 < L2)), and C1 == C2 if and only if H1 == H2 && S1 == S2 && L1 == L2. Similarly for C1>C2.

This will order the colors first by hue; then by saturation to resolve hue conflicts; and finally by lightness to resolve hue and saturation conflicts.

I've used the precedence order H > S > L in this example, but you could just as easily use some other order according to your needs, or maybe even another color respresentation (e.g., RGB).


If you want to sort it by the actual color, not alphabetically by the name of the color, then the solution isn't very obvious. Sorting is normally done on entities where a clear "greater than" and "less than" semantic can be defined. Colors, though, don't have such a semantic because they're represented by 3 numbers generally (either RGB or HSV). You would have to arbitrarily define your own "ordering" and then sort using that.


you can compare colors in java see this


You could try taking the principal components of variance, then projecting on that axis. (So if all the colours are shades of red, red is non-zero, green and blue are both zero, and the algorithm would say the axis is 1.0, 0.0, 0.0. It then sorts on red. If they are all shades of yellow, then that axis is root(0.5), root(0.5), 0.0, it averages the red and green, and sorts on that). It won't work well if the colours form three or more distinct "clouds".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜