]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/colour/DiscretePointColourer.java
Version 17, September 2014
[GpsPrune.git] / tim / prune / gui / colour / DiscretePointColourer.java
1 package tim.prune.gui.colour;
2
3 import java.awt.Color;
4
5 /**
6  * Abstract class to do the discrete colouring of points,
7  * using start and end colours and a wrapping index
8  */
9 public abstract class DiscretePointColourer extends PointColourer
10 {
11         /** array of discrete colours to use */
12         private Color[] _discreteColours = null;
13         /** array of colour indexes */
14         private int[] _colourIndexes = null;
15
16
17         /**
18          * Constructor
19          * @param inStartColour start colour of scale
20          * @param inEndColour end colour of scale
21          * @param inMaxColours number of unique colours before wrap
22          */
23         public DiscretePointColourer(Color inStartColour, Color inEndColour, int inMaxColours)
24         {
25                 super(inStartColour, inEndColour, inMaxColours);
26         }
27
28         /** max number of colours is required here */
29         public static boolean isMaxColoursRequired() {
30                 return true;
31         }
32
33         /**
34          * Initialise the array to the right size
35          * @param inNumPoints number of points in the track
36          */
37         protected void init(int inNumPoints)
38         {
39                 if (_colourIndexes == null || _colourIndexes.length != inNumPoints)
40                 {
41                         // Array needs to be created or resized
42                         if (inNumPoints > 0) {
43                                 _colourIndexes = new int[inNumPoints];
44                         }
45                         else {
46                                 _colourIndexes = null;
47                         }
48                 }
49         }
50
51         /**
52          * Set the colour at the given index
53          * @param inPointIndex point index
54          * @param inColourIndex index of colour to use
55          */
56         protected void setColour(int inPointIndex, int inColourIndex)
57         {
58                 if (_colourIndexes != null && _colourIndexes.length > inPointIndex && inPointIndex >= 0)
59                 {
60                         _colourIndexes[inPointIndex] = inColourIndex;
61                 }
62         }
63
64         /**
65          * Get the colour for the given point index
66          * @param inPointIndex index of point in track
67          * @return colour object
68          */
69         public Color getColour(int inPointIndex)
70         {
71                 if (_colourIndexes != null && _colourIndexes.length > inPointIndex && inPointIndex >= 0 && getMaxColours() > 0)
72                 {
73                         int colourIndex = _colourIndexes[inPointIndex] % getMaxColours();
74                         if (colourIndex >= 0 && _discreteColours != null && colourIndex < _discreteColours.length) {
75                                 return _discreteColours[colourIndex];
76                         }
77                 }
78                 // not found, use default
79                 return super.getDefaultColour();
80         }
81
82         /**
83          * Generate the set of discrete colours to use
84          * @param inNumCategories number of different categories found in the data
85          */
86         protected void generateDiscreteColours(int inNumCategories)
87         {
88                 int maxColours = getMaxColours();
89                 if (maxColours <= 1) {maxColours = 2;}
90                 if (inNumCategories < 1) {inNumCategories = 1;}
91                 else if (inNumCategories > maxColours) {inNumCategories = maxColours;}
92
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);
97                 }
98         }
99
100         /**
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
105          */
106         private Color mixColour(int inIndex, int inWrap)
107         {
108                 float fraction = inWrap < 2 ? 0.0f : (float) inIndex / (float) (inWrap - 1);
109                 return mixColour(fraction);
110         }
111
112         /**
113          * @param inIndex specified colour index
114          * @return precalculated colour at the given index
115          */
116         protected Color getDiscreteColour(int inIndex)
117         {
118                 if (_discreteColours == null || inIndex < 0 || getMaxColours() <= 1) {
119                         return getDefaultColour();
120                 }
121                 return _discreteColours[inIndex % getMaxColours()];
122         }
123
124 }