]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/colour/PointColourer.java
Version 17, September 2014
[GpsPrune.git] / tim / prune / gui / colour / PointColourer.java
1 package tim.prune.gui.colour;
2
3 import java.awt.Color;
4
5 import tim.prune.data.TrackInfo;
6
7 /**
8  * Abstract class to do the colouring of points,
9  * that is holding a colour for each track point
10  * in the current track
11  */
12 public abstract class PointColourer
13 {
14         /** default colour */
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;
20
21
22         /**
23          * Constructor
24          * @param inStartColour start colour
25          * @param inEndColour end colour
26          * @param inMaxColours max number of colours
27          */
28         public PointColourer(Color inStartColour, Color inEndColour, int inMaxColours)
29         {
30                 _startColour = inStartColour;
31                 _endColour   = inEndColour;
32                 _maxColours  = inMaxColours;
33         }
34
35         /**
36          * Constructor
37          * @param inStartColour start colour
38          * @param inEndColour end colour
39          */
40         public PointColourer(Color inStartColour, Color inEndColour)
41         {
42                 this(inStartColour, inEndColour, -1);
43         }
44
45         /**
46          * Calculate the colours for each of the points in the given track
47          * @param inTrackInfo track info object
48          */
49         public abstract void calculateColours(TrackInfo inTrackInfo);
50
51         /**
52          * Get the colour for the given point index
53          * @param inPointIndex index of point in track
54          * @return colour object
55          */
56         public Color getColour(int inPointIndex)
57         {
58                 return _defaultColour;
59         }
60
61         /**
62          * @param inColor default colour to use
63          */
64         protected void setDefaultColour(Color inColour)
65         {
66                 if (inColour != null) {
67                         _defaultColour = inColour;
68                 }
69         }
70
71         /**
72          * @return default colour
73          */
74         protected Color getDefaultColour() {
75                 return _defaultColour;
76         }
77
78         /**
79          * @return start colour
80          */
81         protected Color getStartColour() {
82                 return _startColour;
83         }
84
85         /**
86          * @return end colour
87          */
88         protected Color getEndColour() {
89                 return _endColour;
90         }
91
92         /**
93          * @return maximum number of colours, or -1
94          */
95         protected int getMaxColours() {
96                 return _maxColours;
97         }
98
99         /**
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
103          */
104         protected Color mixColour(float inFraction)
105         {
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;
109
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
114
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);
118         }
119 }