ColorSequenceGenerator.java 735 B

1234567891011121314151617181920212223242526
  1. package de.tudarmstadt.informatik.hostage.ui2.helper;
  2. import android.graphics.Color;
  3. /**
  4. * Idea from http://ridiculousfish.com/blog/posts/colors.html
  5. * Created by Fabio Arnold on 25.02.14.
  6. */
  7. public class ColorSequenceGenerator {
  8. private static final int BIT_COUNT = 30; // sadly there is no unsigned type in java
  9. public static int getColorForIndex(int index) {
  10. int reverseIndex = 0;
  11. for (int i = 0; i < BIT_COUNT; i++) {
  12. reverseIndex = (reverseIndex << 1) | (index & 1);
  13. index >>= 1;
  14. }
  15. float hue = ((float)reverseIndex / (float)(1 << BIT_COUNT) + 0.0f) % 1.0f;
  16. float[] hsv = new float[3];
  17. hsv[0] = 360.0f * hue;
  18. hsv[1] = 0.7f; // not fully saturated
  19. hsv[2] = 0.9f;
  20. return Color.HSVToColor(hsv);
  21. }
  22. }