Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. public class ColorUtils {
  2.  
  3. /**
  4. * Create a minimal indexed colour model that includes the
  5. * specified colours.
  6. *
  7. * @param colors the colours to include
  8. * @return the computed IndexColorModel
  9. */
  10. public static IndexColorModel colorModelFor(Color... colors) {
  11. int len = 1 + colors.length;
  12. int bits = 1, twoToBits = 2;
  13. while (twoToBits < len) {
  14. bits++;
  15. twoToBits *= 2;
  16. }
  17.  
  18. byte[] reds = new byte[len];
  19. byte[] greens = new byte[len];
  20. byte[] blues = new byte[len];
  21. byte[] alphas = new byte[len];
  22.  
  23. reds[0] = greens[0] = blues[0] = alphas[0] = 0; // transparent "colour"
  24. for (int i=1; i < len; i++) {
  25. reds[i] = (byte) colors[i-1].getRed();
  26. greens[i] = (byte) colors[i-1].getGreen();
  27. blues[i] = (byte) colors[i-1].getBlue();
  28. alphas[i] = (byte) colors[i-1].getAlpha();
  29. }
  30.  
  31. return new IndexColorModel(bits, 1 + colors.length, reds, greens, blues, alphas);
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement