Created
February 20, 2015 09:20
-
-
Save warting/ca2df34946966e51caf5 to your computer and use it in GitHub Desktop.
How to sort Integer representation of colors in Android as the rainbow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.color.activities; | |
import android.graphics.Color; | |
import android.os.Bundle; | |
import android.support.v7.app.ActionBarActivity; | |
import android.util.Log; | |
import java.util.Comparator; | |
import java.util.Map; | |
import java.util.TreeMap; | |
import com.color.R; | |
public class AssetsActivity extends ActionBarActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.assets_activity); | |
Map<Integer, String> colors = new TreeMap<>(new Comparator<Integer>() { | |
@Override | |
public int compare(Integer lhs, Integer rhs) { | |
float[] lhsv = new float[3]; | |
float[] rhsv = new float[3]; | |
Color.colorToHSV(lhs, lhsv); | |
Color.colorToHSV(rhs, rhsv); | |
for (int i = 0; i < 3; i++) { | |
if (lhsv[i] != rhsv[i]) { | |
return (lhsv[i] < rhsv[i]) ? -1 : 1; | |
} | |
} | |
return 0; | |
} | |
}); | |
colors.put(-16095289, "0A67C7"); | |
colors.put(-15298149, "16919B"); | |
colors.put(-12605458, "3FA7EE"); | |
colors.put(-16167274, "094E96"); | |
for (Map.Entry<Integer, String> color : colors.entrySet()) { | |
Log.e("Color", color.getKey() + " - " + color.getValue()); | |
} | |
// Gives: | |
// Color﹕ -15298149 - 16919B | |
// Color﹕ -12605458 - 3FA7EE | |
// Color﹕ -16095289 - 0A67C7 | |
// Color﹕ -16167274 - 094E96 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment