]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/color/ColorPool.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / color / ColorPool.java
1 package org.simantics.utils.ui.color;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6
7 /**
8  * Tool for creating random and distinct colors.
9  * 
10  * 
11  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
12  *
13  */
14 public class ColorPool {
15         
16         private List<Color> colors;
17         
18         public ColorPool() {
19                 colors = new ArrayList<Color>();
20         }
21         
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
27         }
28         
29         public List<Color> getColors() {
30                 return colors;
31         }
32         
33         public Color getColor(int index) {
34                 if (colors.size() == 0)
35                         addDefaultColors();
36                 while (colors.size() <= index) {
37                         newRandomColor();
38                 }
39                 return colors.get(index);
40         }
41         
42         public int getSize() {
43                 return colors.size();
44         }
45         
46         public void add(Color color) {
47                 colors.add(color);
48         }
49         
50         public void setColor(int index, Color color) {
51                 colors.set(index, color);
52         }
53         
54         public void clear() {
55                 colors.clear();
56         }
57         
58         private Color newRandomColor() {
59                 
60                 ArrayList<Color> sortColors = new ArrayList<Color>();
61                 for (Color c : colors) {
62                         if (!Float.isNaN(c.getH()))
63                                 sortColors.add(c);
64                 }
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);
70                         
71                 float maxDelta = 0;
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) {
76                         maxDelta = delta;
77                         maxDeltaIndex = i;
78                     }
79                 }
80                 float newHue = sortColors.get(maxDeltaIndex).getH() + 0.5f * maxDelta;
81                 if (newHue > 360.f)
82                     newHue -= 360.f;
83                 
84                 Color randomColor = Color.getRandomHSV(0.5f, 0.5f);
85                 randomColor.setH(newHue);
86                 colors.add(randomColor);
87                 return randomColor;
88                 } else {
89                         Color randomColor = Color.getRandomHSV(0.5f, 0.5f);
90                         colors.add(randomColor);
91                         return randomColor;
92                 }
93         
94         }
95         
96         
97 }