我会这样写
int[] values = new int[]{0, 32, 64, 96, 128, 160, 192, 224, 255};
SortedMap<Long, Color> colours = new TreeMap<>(Collections.reverseOrder());
for (int r : values) {
for (int g : values) {
for (int b : values) {
long brightness = (r + g + b) / 3;
long colourScore = (brightness << 24) + (r << 8) + (g << 16) + b;
colours.put(colourScore, new Color(r,g,b));
}
}
}
for (Color color : colours.values()) {
System.out.println(color);
}
这有利于红色和绿色而不是蓝色,因为蓝色与夜晚有关。;) 它打印
java.awt.Color[r=255,g=255,b=255]
java.awt.Color[r=255,g=255,b=224]
java.awt.Color[r=224,g=255,b=255]
java.awt.Color[r=255,g=224,b=255]
java.awt.Color[r=255,g=255,b=192]
java.awt.Color[r=224,g=255,b=224]
java.awt.Color[r=192,g=255,b=255]
... many deleted ...
java.awt.Color[r=32,g=0,b=32]
java.awt.Color[r=0,g=0,b=64]
java.awt.Color[r=0,g=32,b=0]
java.awt.Color[r=32,g=0,b=0]
java.awt.Color[r=0,g=0,b=32]
java.awt.Color[r=0,g=0,b=0]