1 package org.simantics.utils.ui.color;
3 import java.util.ArrayList;
4 import java.util.Collections;
8 * Tool for creating random and distinct colors.
11 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
14 public class ColorPool {
16 private List<Color> colors;
19 colors = new ArrayList<Color>();
22 public void addDefaultColors() {
23 colors.add(new Color(0, 0, 255)); // blue
24 colors.add(new Color(255, 0, 0)); // red
25 colors.add(new Color(0, 200, 0)); // green
26 colors.add(new Color(0, 0, 0)); // black
29 public List<Color> getColors() {
33 public Color getColor(int index) {
34 if (colors.size() == 0)
36 while (colors.size() <= index) {
39 return colors.get(index);
42 public int getSize() {
46 public void add(Color color) {
50 public void setColor(int index, Color color) {
51 colors.set(index, color);
58 private Color newRandomColor() {
60 ArrayList<Color> sortColors = new ArrayList<Color>();
61 for (Color c : colors) {
62 if (!Float.isNaN(c.getH()))
65 if (sortColors.size() > 0) {
66 Collections.sort(sortColors);
67 Color duplicate = new Color(sortColors.get(0));
68 duplicate.setH(sortColors.get(0).getH()+360.f);
69 sortColors.add(duplicate);
72 int maxDeltaIndex = 0;
73 for (int i = 0; i < sortColors.size()-1; i++) {
74 float delta = sortColors.get(i + 1).getH() - sortColors.get(i).getH();
75 if (delta > maxDelta) {
80 float newHue = sortColors.get(maxDeltaIndex).getH() + 0.5f * maxDelta;
84 Color randomColor = Color.getRandomHSV(0.5f, 0.5f);
85 randomColor.setH(newHue);
86 colors.add(randomColor);
89 Color randomColor = Color.getRandomHSV(0.5f, 0.5f);
90 colors.add(randomColor);