1 package tim.prune.gui.colour;
6 * Abstract class to do the discrete colouring of points,
7 * using start and end colours and a wrapping index
9 public abstract class DiscretePointColourer extends PointColourer
11 /** array of discrete colours to use */
12 private Color[] _discreteColours = null;
13 /** array of colour indexes */
14 private int[] _colourIndexes = null;
19 * @param inStartColour start colour of scale
20 * @param inEndColour end colour of scale
21 * @param inMaxColours number of unique colours before wrap
23 public DiscretePointColourer(Color inStartColour, Color inEndColour, int inMaxColours)
25 super(inStartColour, inEndColour, inMaxColours);
28 /** max number of colours is required here */
29 public static boolean isMaxColoursRequired() {
34 * Initialise the array to the right size
35 * @param inNumPoints number of points in the track
37 protected void init(int inNumPoints)
39 if (_colourIndexes == null || _colourIndexes.length != inNumPoints)
41 // Array needs to be created or resized
42 if (inNumPoints > 0) {
43 _colourIndexes = new int[inNumPoints];
46 _colourIndexes = null;
52 * Set the colour at the given index
53 * @param inPointIndex point index
54 * @param inColourIndex index of colour to use
56 protected void setColour(int inPointIndex, int inColourIndex)
58 if (_colourIndexes != null && _colourIndexes.length > inPointIndex && inPointIndex >= 0)
60 _colourIndexes[inPointIndex] = inColourIndex;
65 * Get the colour for the given point index
66 * @param inPointIndex index of point in track
67 * @return colour object
69 public Color getColour(int inPointIndex)
71 if (_colourIndexes != null && _colourIndexes.length > inPointIndex && inPointIndex >= 0 && getMaxColours() > 0)
73 int colourIndex = _colourIndexes[inPointIndex] % getMaxColours();
74 if (colourIndex >= 0 && _discreteColours != null && colourIndex < _discreteColours.length) {
75 return _discreteColours[colourIndex];
78 // not found, use default
79 return super.getDefaultColour();
83 * Generate the set of discrete colours to use
84 * @param inNumCategories number of different categories found in the data
86 protected void generateDiscreteColours(int inNumCategories)
88 int maxColours = getMaxColours();
89 if (maxColours <= 1) {maxColours = 2;}
90 if (inNumCategories < 1) {inNumCategories = 1;}
91 else if (inNumCategories > maxColours) {inNumCategories = maxColours;}
93 // Use this number of categories to generate the colours
94 _discreteColours = new Color[inNumCategories];
95 for (int i=0; i<inNumCategories; i++) {
96 _discreteColours[i] = mixColour(i, inNumCategories);
101 * Mix the given colours together by interpolating H,S,B values
102 * @param inIndex index from 0 to inWrap-1
103 * @param inWrap wrap length
104 * @return mixed colour
106 private Color mixColour(int inIndex, int inWrap)
108 float fraction = inWrap < 2 ? 0.0f : (float) inIndex / (float) (inWrap - 1);
109 return mixColour(fraction);
113 * @param inIndex specified colour index
114 * @return precalculated colour at the given index
116 protected Color getDiscreteColour(int inIndex)
118 if (_discreteColours == null || inIndex < 0 || getMaxColours() <= 1) {
119 return getDefaultColour();
121 return _discreteColours[inIndex % getMaxColours()];