-package org.simantics.utils.ui.color;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collections;\r
-import java.util.List;\r
-\r
-/**\r
- * Tool for creating random and distinct colors.\r
- * \r
- * \r
- * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
- *\r
- */\r
-public class ColorPool {\r
- \r
- private List<Color> colors;\r
- \r
- public ColorPool() {\r
- colors = new ArrayList<Color>();\r
- }\r
- \r
- public void addDefaultColors() {\r
- colors.add(new Color(0, 0, 255)); // blue\r
- colors.add(new Color(255, 0, 0)); // red\r
- colors.add(new Color(0, 200, 0)); // green\r
- colors.add(new Color(0, 0, 0)); // black\r
- }\r
- \r
- public List<Color> getColors() {\r
- return colors;\r
- }\r
- \r
- public Color getColor(int index) {\r
- if (colors.size() == 0)\r
- addDefaultColors();\r
- while (colors.size() <= index) {\r
- newRandomColor();\r
- }\r
- return colors.get(index);\r
- }\r
- \r
- public int getSize() {\r
- return colors.size();\r
- }\r
- \r
- public void add(Color color) {\r
- colors.add(color);\r
- }\r
- \r
- public void setColor(int index, Color color) {\r
- colors.set(index, color);\r
- }\r
- \r
- public void clear() {\r
- colors.clear();\r
- }\r
- \r
- private Color newRandomColor() {\r
- \r
- ArrayList<Color> sortColors = new ArrayList<Color>();\r
- for (Color c : colors) {\r
- if (!Float.isNaN(c.getH()))\r
- sortColors.add(c);\r
- }\r
- if (sortColors.size() > 0) {\r
- Collections.sort(sortColors);\r
- Color duplicate = new Color(sortColors.get(0));\r
- duplicate.setH(sortColors.get(0).getH()+360.f);\r
- sortColors.add(duplicate);\r
- \r
- float maxDelta = 0;\r
- int maxDeltaIndex = 0;\r
- for (int i = 0; i < sortColors.size()-1; i++) {\r
- float delta = sortColors.get(i + 1).getH() - sortColors.get(i).getH();\r
- if (delta > maxDelta) {\r
- maxDelta = delta;\r
- maxDeltaIndex = i;\r
- }\r
- }\r
- float newHue = sortColors.get(maxDeltaIndex).getH() + 0.5f * maxDelta;\r
- if (newHue > 360.f)\r
- newHue -= 360.f;\r
- \r
- Color randomColor = Color.getRandomHSV(0.5f, 0.5f);\r
- randomColor.setH(newHue);\r
- colors.add(randomColor);\r
- return randomColor;\r
- } else {\r
- Color randomColor = Color.getRandomHSV(0.5f, 0.5f);\r
- colors.add(randomColor);\r
- return randomColor;\r
- }\r
- \r
- }\r
- \r
- \r
-}\r
+package org.simantics.utils.ui.color;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Tool for creating random and distinct colors.
+ *
+ *
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
+ *
+ */
+public class ColorPool {
+
+ private List<Color> colors;
+
+ public ColorPool() {
+ colors = new ArrayList<Color>();
+ }
+
+ public void addDefaultColors() {
+ colors.add(new Color(0, 0, 255)); // blue
+ colors.add(new Color(255, 0, 0)); // red
+ colors.add(new Color(0, 200, 0)); // green
+ colors.add(new Color(0, 0, 0)); // black
+ }
+
+ public List<Color> getColors() {
+ return colors;
+ }
+
+ public Color getColor(int index) {
+ if (colors.size() == 0)
+ addDefaultColors();
+ while (colors.size() <= index) {
+ newRandomColor();
+ }
+ return colors.get(index);
+ }
+
+ public int getSize() {
+ return colors.size();
+ }
+
+ public void add(Color color) {
+ colors.add(color);
+ }
+
+ public void setColor(int index, Color color) {
+ colors.set(index, color);
+ }
+
+ public void clear() {
+ colors.clear();
+ }
+
+ private Color newRandomColor() {
+
+ ArrayList<Color> sortColors = new ArrayList<Color>();
+ for (Color c : colors) {
+ if (!Float.isNaN(c.getH()))
+ sortColors.add(c);
+ }
+ if (sortColors.size() > 0) {
+ Collections.sort(sortColors);
+ Color duplicate = new Color(sortColors.get(0));
+ duplicate.setH(sortColors.get(0).getH()+360.f);
+ sortColors.add(duplicate);
+
+ float maxDelta = 0;
+ int maxDeltaIndex = 0;
+ for (int i = 0; i < sortColors.size()-1; i++) {
+ float delta = sortColors.get(i + 1).getH() - sortColors.get(i).getH();
+ if (delta > maxDelta) {
+ maxDelta = delta;
+ maxDeltaIndex = i;
+ }
+ }
+ float newHue = sortColors.get(maxDeltaIndex).getH() + 0.5f * maxDelta;
+ if (newHue > 360.f)
+ newHue -= 360.f;
+
+ Color randomColor = Color.getRandomHSV(0.5f, 0.5f);
+ randomColor.setH(newHue);
+ colors.add(randomColor);
+ return randomColor;
+ } else {
+ Color randomColor = Color.getRandomHSV(0.5f, 0.5f);
+ colors.add(randomColor);
+ return randomColor;
+ }
+
+ }
+
+
+}