-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorPalette.java
126 lines (103 loc) · 2.69 KB
/
ColorPalette.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
public class ColorPalette implements java.io.Serializable
{
int length;
int maxLength;
/* colors are stored as an array of ints */
int[] colors;
private static final int EMPTY_COLOR = Integer.MIN_VALUE;
/* random generator */
CategoricalDistribution distribution;
/* ctors */
public ColorPalette(int numColors)
{
maxLength = numColors;
initColors(length);
distribution = new CategoricalDistribution(numColors);
}
public ColorPalette(int numColors,
CategoricalDistribution probabilityDistribution)
{
maxLength = numColors;
initColors(length);
distribution = probabilityDistribution;
}
private void initColors(int length)
{
colors = new int [maxLength];
/* -1 represents no color */
for(int i = 0; i < maxLength; ++i)
{
colors[i] = EMPTY_COLOR;
}
length = 0;
}
public void setColor(int index, int color)
{
if(index > maxLength - 1)
index = maxLength - 1; //def write @ last positionr
colors[index] = color; //FIXED: index always in boundaries
if (index == length) {
length++; // FIXED: increment currently set colors of palette
System.out.println("Aggiunti finora colori: " + length);
} else if (index >= length && index < maxLength) {
length = index + 1;
}
}
/* DEPRECATED use setColor instead */
// public boolean addColor(int color)
// {
// boolean added = false;
// if(length < maxLength)
// {
// colors[length] = color;
// length++;
// added = true;
// }
// return added;
// }
/* review in the future */
public int getColor(int index)
{
if (index >= maxLength)
index = maxLength - 1; //def read @ last position
int color = colors[index]; //FIXED
/* if the chosen index leads to a non color, try the last one */
if(color == EMPTY_COLOR)
{
/* if zero colors are present, raise exception */
color = colors[length - 1];
}
return color;
}
public void setColorProbability(int index, double prob)
{
distribution.setOutcomeProbability(index, prob);
}
public void setColorProbability(double[] dist)
{
distribution.setOutcomeProbabilities(dist);
}
public double getColorProbability(int index)
{
return distribution.getOutcomeProbability(index);
}
public int getRandomColor()
{
/* generate a random value between */
int randomIndex = distribution.nextValue();
return colors[randomIndex];
}
/* default color is the first one */
public int getDefaultColor()
{
return colors[0];
}
public void setDefaultColor(int color)
{
colors[0] = color;
}
public int getPaletteLength()
{
return length;
}
}