X-Git-Url: http://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=src%2Ftim%2Fprune%2Fgui%2Fcolour%2FPointColourer.java;fp=src%2Ftim%2Fprune%2Fgui%2Fcolour%2FPointColourer.java;h=27e658c8a9839b385a041f8020608d04750fa1a0;hp=0000000000000000000000000000000000000000;hb=ce6f2161b8596f7018d6a76bff79bc9e571f35fd;hpb=2d8cb72e84d5cc1089ce77baf1e34ea3ea2f8465 diff --git a/src/tim/prune/gui/colour/PointColourer.java b/src/tim/prune/gui/colour/PointColourer.java new file mode 100644 index 0000000..27e658c --- /dev/null +++ b/src/tim/prune/gui/colour/PointColourer.java @@ -0,0 +1,119 @@ +package tim.prune.gui.colour; + +import java.awt.Color; + +import tim.prune.data.TrackInfo; + +/** + * Abstract class to do the colouring of points, + * that is holding a colour for each track point + * in the current track + */ +public abstract class PointColourer +{ + /** default colour */ + private Color _defaultColour = Color.BLUE; + /** start and end colours */ + private Color _startColour = null, _endColour = null; + /** max number of unique colours before wrapping */ + private int _maxColours = 1; + + + /** + * Constructor + * @param inStartColour start colour + * @param inEndColour end colour + * @param inMaxColours max number of colours + */ + public PointColourer(Color inStartColour, Color inEndColour, int inMaxColours) + { + _startColour = inStartColour; + _endColour = inEndColour; + _maxColours = inMaxColours; + } + + /** + * Constructor + * @param inStartColour start colour + * @param inEndColour end colour + */ + public PointColourer(Color inStartColour, Color inEndColour) + { + this(inStartColour, inEndColour, -1); + } + + /** + * Calculate the colours for each of the points in the given track + * @param inTrackInfo track info object + */ + public abstract void calculateColours(TrackInfo inTrackInfo); + + /** + * Get the colour for the given point index + * @param inPointIndex index of point in track + * @return colour object + */ + public Color getColour(int inPointIndex) + { + return _defaultColour; + } + + /** + * @param inColor default colour to use + */ + protected void setDefaultColour(Color inColour) + { + if (inColour != null) { + _defaultColour = inColour; + } + } + + /** + * @return default colour + */ + protected Color getDefaultColour() { + return _defaultColour; + } + + /** + * @return start colour + */ + protected Color getStartColour() { + return _startColour; + } + + /** + * @return end colour + */ + protected Color getEndColour() { + return _endColour; + } + + /** + * @return maximum number of colours, or -1 + */ + protected int getMaxColours() { + return _maxColours; + } + + /** + * Mix the given colours together using HSB values instead of interpolating RGB + * @param inFraction between 0.0 (start) and 1.0 (end) + * @return mixed colour + */ + protected Color mixColour(float inFraction) + { + if (_startColour == null && _endColour == null) return getDefaultColour(); + if (_startColour == null) return _endColour; + if (_endColour == null || inFraction < 0.0 || inFraction > 1.0) return _startColour; + + // Convert both colours to hsb, and interpolate + float[] startHSB = Color.RGBtoHSB(_startColour.getRed(), _startColour.getGreen(), _startColour.getBlue(), null); + float[] endHSB = Color.RGBtoHSB(_endColour.getRed(), _endColour.getGreen(), _endColour.getBlue(), null); + // Note that if end hue is less than start hue, hue will go backwards rather than forwards with wrap around 0 + + return Color.getHSBColor(startHSB[0] + (endHSB[0]-startHSB[0]) * inFraction, + startHSB[1] + (endHSB[1]-startHSB[1]) * inFraction, + startHSB[2] + (endHSB[2]-startHSB[2]) * inFraction); + } +}