Java – how to dynamically calculate the color list?
In order to represent the object list with different colors in GWT widget, we need to dynamically obtain the color list with different colors from the object Since the size of the list may be different, we need to be able to calculate such a color list
Solution
Another version of my solution has a scope:
List<int> getUniqueColors(int amount) { final int lowerLimit = 0x10; final int upperLimit = 0xE0; final int colorStep = (upperLimit-lowerLimit)/Math.pow(amount,1f/3); final List<int> colors = new ArrayList<int>(amount); for (int R = lowerLimit;R < upperLimit; R+=colorStep) for (int G = lowerLimit;G < upperLimit; G+=colorStep) for (int B = lowerLimit;B < upperLimit; B+=colorStep) { if (colors.size() >= amount) { //The calculated step is not very precise,so this safeguard is appropriate return colors; } else { int color = (R<<16)+(G<<8)+(B); colors.add(color); } } return colors; }
This is more advanced because it produces colors as different from each other as possible (like @aiiobe)
Generally, we divide the range into three sub ranges of red, green and blue, calculate the number of steps we need to iterate them (by applying pow (range, 1F / 3)) and iterate them
For example, given the number 3, it will generate 0x0000b1,0x00b100,0x00b1b1 For the number 10, it will be: 0x000076,0x0000ec, 0x007600,0x007676,0x0076ec, 0x00ec00,0x00ec76,0x00ecec, 0x760000,0x760076