Class ColorScale

java.lang.Object
org.openstreetmap.josm.tools.ColorScale

public final class ColorScale extends Object
Utility class that helps to work with color scale for coloring GPX tracks etc.
Since:
7319
  • Field Details

  • Constructor Details

  • Method Details

    • createFixedScale

      public static ColorScale createFixedScale(Color[] colors)
      Gets a fixed color range.
      Parameters:
      colors - the fixed colors list
      Returns:
      The scale
      Since:
      15247
    • createHSBScale

      public static ColorScale createHSBScale(int count)
      Gets a HSB color range.
      Parameters:
      count - The number of colors the scale should have
      Returns:
      The scale
    • createCyclicScale

      public static ColorScale createCyclicScale(int count)
      Creates a cyclic color scale (red yellow green blue red)
      Parameters:
      count - The number of colors the scale should have
      Returns:
      The scale
    • weighted

      private static float weighted(float x)
      transition function: w(0)=1, w(1)=0, 0<=w(x)<=1
      Parameters:
      x - number: 0<=x<=1
      Returns:
      the weighted value
    • setRange

      public void setRange(double min, double max)
      Sets the hint on the range this scale is for
      Parameters:
      min - The minimum value
      max - The maximum value
    • addBounds

      public void addBounds()
      Add standard colors for values below min or above max value
    • getColor

      public Color getColor(double value)
      Gets a color for the given value.
      Parameters:
      value - The value
      Returns:
      The color for this value, this may be a special color if the value is outside the range but never null.
    • getColor

      public Color getColor(Number value)
      Gets a color for the given value.
      Parameters:
      value - The value, may be null
      Returns:
      The color for this value, this may be a special color if the value is outside the range or the value is null but never null.
    • getNoDataColor

      Get the color to use if there is no data
      Returns:
      The color
    • setNoDataColor

      public void setNoDataColor(Color noDataColor)
      Sets the color to use if there is no data
      Parameters:
      noDataColor - The color
    • makeTransparent

      public ColorScale makeTransparent(int alpha)
      Make all colors transparent
      Parameters:
      alpha - The alpha value all colors in the range should have, range 0..255
      Returns:
      This scale, for chaining
    • addTitle

      public ColorScale addTitle(String title)
      Adds a title to this scale
      Parameters:
      title - The new title
      Returns:
      This scale, for chaining
    • addColorBarTitles

      public ColorScale addColorBarTitles(String[] titles)
      Adds titles to the color bar for a fixed scale
      Parameters:
      titles - Array of String, same length as the colors array
      Returns:
      This scale, for chaining
      Since:
      18396
    • setIntervalCount

      public ColorScale setIntervalCount(int intervalCount)
      Sets the interval count for this scale
      Parameters:
      intervalCount - The interval count hint
      Returns:
      This scale, for chaining
    • makeReversed

      Reverses this scale
      Returns:
      This scale, for chaining
    • drawOutline

      private void drawOutline(Graphics2D g, String txt, int x, int y, Color color)
      draws an outline for the legend texts
      Parameters:
      g - The graphics to draw on
      txt - The text to draw the outline
      x - Text x
      y - Text y
      color - The color of the text
    • drawColorBar

      public void drawColorBar(Graphics2D g, int x, int y, int w, int h, double valueScale)
      Draws a color bar representing this scale on the given graphics
      Parameters:
      g - The graphics to draw on
      x - Rect x
      y - Rect y
      w - Rect width
      h - Rect height
      valueScale - The scale factor of the values
    • drawColorBarTime

      public void drawColorBarTime(Graphics2D g, int x, int y, int w, int h, double minVal, double maxVal)
      Draws a color bar representing the time scale on the given graphics
      Parameters:
      g - The graphics to draw on
      x - Rect x
      y - Rect y
      w - Color bar width
      h - Color bar height
      minVal - start time of the track
      maxVal - end time of the track
    • calculateFontMetrics

    • drawBackgroundRectangle

      private int[] drawBackgroundRectangle(Graphics2D g, int x, int y, int w, int h, int fw, int fh, int titleWidth)
      Draw the background rectangle
      Parameters:
      g - The graphics to draw on
      x - Rect x
      y - Rect y
      w - Color bar width
      h - Color bar height
      fw - The font width
      fh - The font height
      titleWidth - The width of the title
      Returns:
      an @{code int[]} of [xRect, rectWidth, xText, titleWidth] TODO investigate using records in Java 17
    • drawLegend

      private void drawLegend(Graphics2D g, int y, int w, int h, double valueScale, int fh, int fw, int xText)
      Draws the legend for the color bar representing the time scale on the given graphics
      Parameters:
      g - The graphics to draw on
      y - Rect y
      w - Color bar width
      h - Color bar height
      fw - The font width
      fh - The font height
      valueScale - The scale factor of the values
      xText - The location to start drawing the text (x-axis)
    • drawTimeLegend

      private void drawTimeLegend(Graphics2D g, int y, int w, int h, double minVal, double maxVal, int fh, int fw, int xText)
      Draws the legend for the color bar representing the time scale on the given graphics
      Parameters:
      g - The graphics to draw on
      y - Rect y
      w - Color bar width
      h - Color bar height
      minVal - start time of the track
      maxVal - end time of the track
      fw - The font width
      fh - The font height
      xText - The location to start drawing the text (x-axis)
    • drawLegendText

      private void drawLegendText(Graphics2D g, int y, int w, int h, int fh, int fw, int xText, int i, Color color, String txt)
      Draws the legend for the color bar representing the time scale on the given graphics
      Parameters:
      g - The graphics to draw on
      y - Rect y
      w - Color bar width
      h - Color bar height
      fw - The font width
      fh - The font height
      xText - The location to start drawing the text (x-axis)
      color - The color of the text to draw
      txt - The text string to draw
      i - The index of the legend (so we can calculate the y location)