Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ColorUtils {
- /**
- * Create a minimal indexed colour model that includes the
- * specified colours.
- *
- * @param colors the colours to include
- * @return the computed IndexColorModel
- */
- public static IndexColorModel colorModelFor(Color... colors) {
- int len = 1 + colors.length;
- int bits = 1, twoToBits = 2;
- while (twoToBits < len) {
- bits++;
- twoToBits *= 2;
- }
- byte[] reds = new byte[len];
- byte[] greens = new byte[len];
- byte[] blues = new byte[len];
- byte[] alphas = new byte[len];
- reds[0] = greens[0] = blues[0] = alphas[0] = 0; // transparent "colour"
- for (int i=1; i < len; i++) {
- reds[i] = (byte) colors[i-1].getRed();
- greens[i] = (byte) colors[i-1].getGreen();
- blues[i] = (byte) colors[i-1].getBlue();
- alphas[i] = (byte) colors[i-1].getAlpha();
- }
- return new IndexColorModel(bits, 1 + colors.length, reds, greens, blues, alphas);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement