Class ColorScale


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

      Constructors 
      Modifier Constructor Description
      private ColorScale()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addBounds()
      Add standard colors for values below min or above max value
      ColorScale addColorBarTitles​(java.lang.String[] titles)
      Adds titles to the color bar for a fixed scale
      ColorScale addTitle​(java.lang.String title)
      Adds a title to this scale
      private static java.awt.FontMetrics calculateFontMetrics​(java.awt.Graphics2D g)  
      static ColorScale createCyclicScale​(int count)
      Creates a cyclic color scale (red yellow green blue red)
      static ColorScale createFixedScale​(java.awt.Color[] colors)
      Gets a fixed color range.
      static ColorScale createHSBScale​(int count)
      Gets a HSB color range.
      private int[] drawBackgroundRectangle​(java.awt.Graphics2D g, int x, int y, int w, int h, int fw, int fh, int titleWidth)
      Draw the background rectangle
      void drawColorBar​(java.awt.Graphics2D g, int x, int y, int w, int h, double valueScale)
      Draws a color bar representing this scale on the given graphics
      void drawColorBarTime​(java.awt.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
      private void drawLegend​(java.awt.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
      private void drawLegendText​(java.awt.Graphics2D g, int y, int w, int h, int fh, int fw, int xText, int i, java.awt.Color color, java.lang.String txt)
      Draws the legend for the color bar representing the time scale on the given graphics
      private void drawOutline​(java.awt.Graphics2D g, java.lang.String txt, int x, int y, java.awt.Color color)
      draws an outline for the legend texts
      private void drawTimeLegend​(java.awt.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
      java.awt.Color getColor​(double value)
      Gets a color for the given value.
      java.awt.Color getColor​(java.lang.Number value)
      Gets a color for the given value.
      java.awt.Color getNoDataColor()
      Get the color to use if there is no data
      ColorScale makeReversed()
      Reverses this scale
      ColorScale makeTransparent​(int alpha)
      Make all colors transparent
      ColorScale setIntervalCount​(int intervalCount)
      Sets the interval count for this scale
      void setNoDataColor​(java.awt.Color noDataColor)
      Sets the color to use if there is no data
      void setRange​(double min, double max)
      Sets the hint on the range this scale is for
      private static float weighted​(float x)
      transition function: w(0)=1, w(1)=0, 0<=w(x)<=1
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • createFixedScale

        public static ColorScale createFixedScale​(java.awt.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 java.awt.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 java.awt.Color getColor​(java.lang.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

        public java.awt.Color getNoDataColor()
        Get the color to use if there is no data
        Returns:
        The color
      • setNoDataColor

        public void setNoDataColor​(java.awt.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​(java.lang.String title)
        Adds a title to this scale
        Parameters:
        title - The new title
        Returns:
        This scale, for chaining
      • addColorBarTitles

        public ColorScale addColorBarTitles​(java.lang.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
      • drawOutline

        private void drawOutline​(java.awt.Graphics2D g,
                                 java.lang.String txt,
                                 int x,
                                 int y,
                                 java.awt.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​(java.awt.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​(java.awt.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

        private static java.awt.FontMetrics calculateFontMetrics​(java.awt.Graphics2D g)
      • drawBackgroundRectangle

        private int[] drawBackgroundRectangle​(java.awt.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​(java.awt.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​(java.awt.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​(java.awt.Graphics2D g,
                                    int y,
                                    int w,
                                    int h,
                                    int fh,
                                    int fw,
                                    int xText,
                                    int i,
                                    java.awt.Color color,
                                    java.lang.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)