1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui.components.util;
21
22 import java.awt.Color;
23 import java.util.ArrayList;
24
25
26
27
28
29
30
31
32 public class ColorManager {
33
34 public static final Color[] DEFAULT_COLORS= new Color[]{Color.BLACK,Color.RED,Color.BLUE,Color.GREEN.darker(),Color.ORANGE,Color.DARK_GRAY,Color.RED.darker(),Color.BLUE.darker(),Color.GREEN.darker().darker(),Color.ORANGE.darker()};
35 private Color[] availableColors = DEFAULT_COLORS;
36 private ArrayList<Color> unusedColors = new ArrayList<Color>(3);
37 private int count = 0;
38
39
40
41
42
43 public void loadDefaultColors() {
44 loadColors(DEFAULT_COLORS);
45 }
46
47
48
49
50
51
52 public void loadColors(Color[] colors) {
53 if (colors == null) throw new IllegalArgumentException("Colors cannot be null.");
54 else if (colors.length == 0) throw new IllegalArgumentException("There should be at least one Color available.");
55 synchronized (unusedColors) {
56 availableColors = new Color[colors.length];
57 System.arraycopy(colors, 0, availableColors, 0, colors.length);
58 if (count >= availableColors.length) {
59 count %= availableColors.length;
60 }
61 }
62 }
63
64
65
66
67
68
69 public Color pickColor() {
70 synchronized (unusedColors) {
71 if (unusedColors.size() > 0) {
72 Color c = unusedColors.remove(0);
73 if (c != null) {
74 count++;
75 return c;
76 }
77 }
78 return availableColors[count++%availableColors.length];
79 }
80 }
81
82
83
84
85
86
87
88
89
90 public void dropColor(Color color) {
91 if (color == null) throw new IllegalArgumentException("Dropped color cannot be null.");
92 synchronized (unusedColors) {
93 unusedColors.add(color);
94 count--;
95 if (count <=0 ) count = 0;
96 }
97 }
98
99
100
101
102
103 public int getColorIndex() {
104 return count;
105 }
106
107
108
109
110
111 public void setColorIndex(int index) {
112 this.count = index % availableColors.length;
113 }
114
115
116
117
118
119
120
121 public void setIndexByColor(Color color) {
122 for (int i = 0; i < availableColors.length; i++) {
123 if (availableColors[i].equals(color)) {
124 setColorIndex(i);
125 return;
126 }
127 }
128 }
129
130
131
132
133
134 public void reset() {
135 synchronized (unusedColors) {
136 unusedColors.clear();
137 count = 0;
138 }
139 }
140
141
142
143
144
145
146 public Color previewNextColor() {
147 synchronized (unusedColors) {
148 if (unusedColors.size() > 0) {
149 return unusedColors.get(0);
150 }
151 return availableColors[count%availableColors.length];
152 }
153 }
154
155 }