1 package tim.prune.gui.colour;
5 import tim.prune.data.TrackInfo;
8 * Abstract class to do the colouring of points,
9 * that is holding a colour for each track point
10 * in the current track
12 public abstract class PointColourer
15 private Color _defaultColour = Color.BLUE;
16 /** start and end colours */
17 private Color _startColour = null, _endColour = null;
18 /** max number of unique colours before wrapping */
19 private int _maxColours = 1;
24 * @param inStartColour start colour
25 * @param inEndColour end colour
26 * @param inMaxColours max number of colours
28 public PointColourer(Color inStartColour, Color inEndColour, int inMaxColours)
30 _startColour = inStartColour;
31 _endColour = inEndColour;
32 _maxColours = inMaxColours;
37 * @param inStartColour start colour
38 * @param inEndColour end colour
40 public PointColourer(Color inStartColour, Color inEndColour)
42 this(inStartColour, inEndColour, -1);
46 * Calculate the colours for each of the points in the given track
47 * @param inTrackInfo track info object
49 public abstract void calculateColours(TrackInfo inTrackInfo);
52 * Get the colour for the given point index
53 * @param inPointIndex index of point in track
54 * @return colour object
56 public Color getColour(int inPointIndex)
58 return _defaultColour;
62 * @param inColor default colour to use
64 protected void setDefaultColour(Color inColour)
66 if (inColour != null) {
67 _defaultColour = inColour;
72 * @return default colour
74 protected Color getDefaultColour() {
75 return _defaultColour;
79 * @return start colour
81 protected Color getStartColour() {
88 protected Color getEndColour() {
93 * @return maximum number of colours, or -1
95 protected int getMaxColours() {
100 * Mix the given colours together using HSB values instead of interpolating RGB
101 * @param inFraction between 0.0 (start) and 1.0 (end)
102 * @return mixed colour
104 protected Color mixColour(float inFraction)
106 if (_startColour == null && _endColour == null) return getDefaultColour();
107 if (_startColour == null) return _endColour;
108 if (_endColour == null || inFraction < 0.0 || inFraction > 1.0) return _startColour;
110 // Convert both colours to hsb, and interpolate
111 float[] startHSB = Color.RGBtoHSB(_startColour.getRed(), _startColour.getGreen(), _startColour.getBlue(), null);
112 float[] endHSB = Color.RGBtoHSB(_endColour.getRed(), _endColour.getGreen(), _endColour.getBlue(), null);
113 // Note that if end hue is less than start hue, hue will go backwards rather than forwards with wrap around 0
115 return Color.getHSBColor(startHSB[0] + (endHSB[0]-startHSB[0]) * inFraction,
116 startHSB[1] + (endHSB[1]-startHSB[1]) * inFraction,
117 startHSB[2] + (endHSB[2]-startHSB[2]) * inFraction);