]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/gui/colour/AltitudeColourer.java
5ab79db89377b746861131fe95e91e456bec6389
[GpsPrune.git] / src / tim / prune / gui / colour / AltitudeColourer.java
1 package tim.prune.gui.colour;
2
3 import java.awt.Color;
4
5 import tim.prune.data.DataPoint;
6 import tim.prune.data.Track;
7 import tim.prune.data.TrackInfo;
8
9 /**
10  * Colourer based on altitude values
11  */
12 public class AltitudeColourer extends ContinuousPointColourer
13 {
14         /**
15          * Constructor
16          * @param inStartColour start colour
17          * @param inEndColour end colour
18          */
19         public AltitudeColourer(Color inStartColour, Color inEndColour)
20         {
21                 super(inStartColour, inEndColour);
22         }
23
24         @Override
25         public void calculateColours(TrackInfo inTrackInfo)
26         {
27                 Track track = inTrackInfo == null ? null : inTrackInfo.getTrack();
28                 final int numPoints = track == null ? 0 : track.getNumPoints();
29                 DataPoint point = null;
30
31                 // Figure out altitude range
32                 double minAltitude = 0.0;
33                 double maxAltitude = 0.0;
34                 boolean altFound = false;
35                 for (int i=0; i<numPoints; i++)
36                 {
37                         point = track.getPoint(i);
38                         if (point != null && point.hasAltitude())
39                         {
40                                 double altValue = point.getAltitude().getMetricValue();
41                                 if (altValue < minAltitude || !altFound) {minAltitude = altValue;}
42                                 if (altValue > maxAltitude || !altFound) {maxAltitude = altValue;}
43                                 altFound = true;
44                         }
45                 }
46
47                 if ((maxAltitude - minAltitude) < 1.0)
48                 {
49                         // not enough altitude range, set all to null
50                         init(0);
51                 }
52                 else
53                 {
54                         // initialise the array to the right size
55                         init(numPoints);
56                         // loop over track points to calculate colours
57                         for (int i=0; i<numPoints; i++)
58                         {
59                                 point = track.getPoint(i);
60                                 if (point != null && point.hasAltitude() && !point.isWaypoint())
61                                 {
62                                         double altValue = point.getAltitude().getMetricValue();
63                                         double fraction = (altValue - minAltitude) / (maxAltitude - minAltitude);
64                                         setColour(i, mixColour((float) fraction));
65                                 }
66                                 else setColour(i, null);
67                         }
68                 }
69         }
70 }