Skip to content

Commit

Permalink
Fix #23926: Extend GPS legend for time information, improve design (p…
Browse files Browse the repository at this point in the history
…atch by Pauline, modified)

Modifications are as follows:
* Reduction of code duplication
* Addition of functions in ColorHelper to calculate contrast ratios

git-svn-id: https://josm.openstreetmap.de/svn/trunk@19236 0c6e7542-c601-0410-84e7-c038aed88b3b
  • Loading branch information
taylor.smock committed Oct 8, 2024
1 parent faed2ac commit 2484e72
Show file tree
Hide file tree
Showing 3 changed files with 358 additions and 36 deletions.
35 changes: 23 additions & 12 deletions src/org/openstreetmap/josm/gui/layer/gpx/GpxDrawHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,24 +236,30 @@ public class GpxDrawHelper implements SoMChangeListener, MapViewPaintable.LayerP
// The heat map was invalidated since the last draw.
private boolean gpxLayerInvalidated;

/** minTime saves the start time of the track as epoch seconds */
private double minTime;
/** maxTime saves the end time of the track as epoch seconds */
private double maxTime;


private void setupColors() {

Check failure on line 245 in src/org/openstreetmap/josm/gui/layer/gpx/GpxDrawHelper.java

View workflow job for this annotation

GitHub Actions / Analyze

com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck

'METHOD_DEF' has more than 1 empty lines before.
hdopAlpha = Config.getPref().getInt("hdop.color.alpha", -1);
velocityScale = ColorScale.createHSBScale(256);
/* Colors (without custom alpha channel, if given) for HDOP painting. */
hdopScale = ColorScale.createHSBScale(256).makeReversed().addTitle(tr("HDOP"));
qualityScale = ColorScale.createFixedScale(rtkLibQualityColors).addTitle(tr("Quality")).addColorBarTitles(rtkLibQualityNames);
fixScale = ColorScale.createFixedScale(gpsFixQualityColors).addTitle(tr("GPS fix")).addColorBarTitles(gpsFixQualityNames);
refScale = ColorScale.createCyclicScale(1).addTitle(tr("GPS ref"));
dateScale = ColorScale.createHSBScale(256).addTitle(tr("Time"));
directionScale = ColorScale.createCyclicScale(256).setIntervalCount(4).addTitle(tr("Direction"));
fixScale = ColorScale.createFixedScale(gpsFixQualityColors).addTitle(tr("GPS fix value")).addColorBarTitles(gpsFixQualityNames);
refScale = ColorScale.createCyclicScale(1).addTitle(tr("GPS Ref-ID"));
dateScale = ColorScale.createHSBScale(256).addTitle(tr("Track date"));
directionScale = ColorScale.createCyclicScale(256).setIntervalCount(4).addTitle(tr("Direction [°]"));

systemOfMeasurementChanged(null, null);
}

@Override
public void systemOfMeasurementChanged(String oldSoM, String newSoM) {
SystemOfMeasurement som = SystemOfMeasurement.getSystemOfMeasurement();
velocityScale.addTitle(tr("Velocity, {0}", som.speedName));
velocityScale.addTitle(tr("Velocity [{0}]", som.speedName));
layer.invalidate();
}

Expand Down Expand Up @@ -622,6 +628,9 @@ public void calculateColors() {
Interval interval = data.getMinMaxTimeForAllTracks().orElse(new Interval(Instant.EPOCH, Instant.now()));
minval = interval.getStart().getEpochSecond();
maxval = interval.getEnd().getEpochSecond();
this.minTime = minval;
this.maxTime = maxval;

dateScale.setRange(minval, maxval);
}

Expand All @@ -641,7 +650,7 @@ public void calculateColors() {
if (!refs.isEmpty()) {
Collections.sort(refs);
String[] a = {};
refScale = ColorScale.createCyclicScale(refs.size()).addTitle(tr("GPS ref")).addColorBarTitles(refs.toArray(a));
refScale = ColorScale.createCyclicScale(refs.size()).addTitle(tr("GPS ref ID")).addColorBarTitles(refs.toArray(a));
refScale.setRange(0, refs.size());
}
}
Expand Down Expand Up @@ -1618,18 +1627,20 @@ public void drawColorBar(Graphics2D g, MapView mv) {
g.setComposite(AlphaComposite.SrcOver.derive(1.00f));

if (colored == ColorMode.HDOP) {
hdopScale.drawColorBar(g, w-30, 50, 20, 100, 1.0);
hdopScale.drawColorBar(g, w-10, 50, 20, 100, 1.0);
} else if (colored == ColorMode.QUALITY) {
qualityScale.drawColorBar(g, w-30, 50, 20, 100, 1.0);
qualityScale.drawColorBar(g, w-10, 50, 20, 100, 1.0);
} else if (colored == ColorMode.FIX) {
fixScale.drawColorBar(g, w-30, 50, 20, 175, 1.0);
fixScale.drawColorBar(g, w-10, 50, 20, 175, 1.0);
} else if (colored == ColorMode.REF) {
refScale.drawColorBar(g, w-30, 50, 20, 175, 1.0);
refScale.drawColorBar(g, w-10, 50, 20, 175, 1.0);
} else if (colored == ColorMode.VELOCITY) {
SystemOfMeasurement som = SystemOfMeasurement.getSystemOfMeasurement();
velocityScale.drawColorBar(g, w-30, 50, 20, 100, som.speedValue);
velocityScale.drawColorBar(g, w-10, 50, 20, 100, som.speedValue);
} else if (colored == ColorMode.DIRECTION) {
directionScale.drawColorBar(g, w-30, 50, 20, 100, 180.0/Math.PI);
directionScale.drawColorBar(g, w-10, 50, 20, 100, 180.0/Math.PI);
} else if (colored == ColorMode.TIME) {
dateScale.drawColorBarTime(g, w-10, 50, 20, 100, this.minTime, this.maxTime);
}
}

Expand Down
50 changes: 47 additions & 3 deletions src/org/openstreetmap/josm/tools/ColorHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,13 @@ public static String color2html(Color color, boolean withAlpha) {
*/
public static Color getForegroundColor(Color bg) {
// http://stackoverflow.com/a/3943023/2257172
return bg == null ? null :
(bg.getRed()*0.299 + bg.getGreen()*0.587 + bg.getBlue()*0.114) > 186 ?
Color.BLACK : Color.WHITE;
if (bg == null) {
return null;
}
if (calculateContrastRatio(Color.WHITE, bg) > calculateContrastRatio(Color.BLACK, bg)) {
return Color.WHITE;
}
return Color.BLACK;
}

/**
Expand Down Expand Up @@ -128,4 +132,44 @@ public static Color alphaMultiply(Color color, float alphaFactor) {
public static Color complement(Color clr) {
return new Color(255 - clr.getRed(), 255 - clr.getGreen(), 255 - clr.getBlue(), clr.getAlpha());
}

/**
* Calculate the relative "luminance" of a color. This is mostly useful for choosing background/foreground colours
* @see <a href="https://stackoverflow.com/questions/9733288/how-to-programmatically-calculate-the-contrast-ratio-between-two-colors">
* constrast ratio</a>
*/
private static double calculateLuminance(Color color) {
final double rs = color.getRed() / 255.0;
final double gs = color.getGreen() / 255.0;
final double bs = color.getBlue() / 255.0;
final double r = calculateLuminanceStepFunction(rs);
final double g = calculateLuminanceStepFunction(gs);
final double b = calculateLuminanceStepFunction(bs);
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
}

/**
* This is a step function for {@link #calculateLuminance(Color)}
* @param color The color to get the values for
* @return The value to use when calculating relative luminance
*/
private static double calculateLuminanceStepFunction(double color) {
if (color <= 0.03928) {
return color / 12.92;
}
return Math.pow((color + 0.055) / 1.055, 2.4);
}

/**
* Calculate the contrast between two colors (e.g. {@link Color#black} and {@link Color#white}).
* @param first The first color to use
* @param second The second color to use
* @return The contrast ratio ((L1 + 0.05)/(L2 + 0.05))
* @since 19236
*/
public static double calculateContrastRatio(Color first, Color second) {
final double fL = calculateLuminance(first);
final double sL = calculateLuminance(second);
return (Math.max(fL, sL) + 0.05) / (Math.min(fL, sL) + 0.05);
}
}
Loading

0 comments on commit 2484e72

Please sign in to comment.