Package org.openstreetmap.josm.tools
Class ColorScale
- java.lang.Object
-
- org.openstreetmap.josm.tools.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
-
-
Field Summary
Fields Modifier and Type Field Description private java.awt.Color
aboveMaxColor
private java.awt.Color
belowMinColor
private java.lang.String[]
colorBarTitles
private java.awt.Color[]
colors
private static java.lang.String
DAY_TIME_FORMAT
private int
intervalCount
private static java.awt.Color
LEGEND_BACKGROUND
private static java.awt.Color
LEGEND_TEXT_OUTLINE_BRIGHT
private static java.awt.Color
LEGEND_TEXT_OUTLINE_DARK
private static java.awt.Color
LEGEND_TITLE
private double
max
private double
min
private java.awt.Color
noDataColor
private static byte
PADDING
Padding for the legend (from the text to the edge of the rectangle)private static java.lang.String
TIME_FORMAT
private java.lang.String
title
-
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 valueColorScale
addColorBarTitles(java.lang.String[] titles)
Adds titles to the color bar for a fixed scaleColorScale
addTitle(java.lang.String title)
Adds a title to this scaleprivate 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 rectanglevoid
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 graphicsvoid
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 graphicsprivate 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 graphicsprivate 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 graphicsprivate void
drawOutline(java.awt.Graphics2D g, java.lang.String txt, int x, int y, java.awt.Color color)
draws an outline for the legend textsprivate 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 graphicsjava.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 dataColorScale
makeReversed()
Reverses this scaleColorScale
makeTransparent(int alpha)
Make all colors transparentColorScale
setIntervalCount(int intervalCount)
Sets the interval count for this scalevoid
setNoDataColor(java.awt.Color noDataColor)
Sets the color to use if there is no datavoid
setRange(double min, double max)
Sets the hint on the range this scale is forprivate static float
weighted(float x)
transition function: w(0)=1, w(1)=0, 0<=w(x)<=1
-
-
-
Field Detail
-
LEGEND_BACKGROUND
private static final java.awt.Color LEGEND_BACKGROUND
-
LEGEND_TEXT_OUTLINE_DARK
private static final java.awt.Color LEGEND_TEXT_OUTLINE_DARK
-
LEGEND_TEXT_OUTLINE_BRIGHT
private static final java.awt.Color LEGEND_TEXT_OUTLINE_BRIGHT
-
LEGEND_TITLE
private static final java.awt.Color LEGEND_TITLE
-
DAY_TIME_FORMAT
private static final java.lang.String DAY_TIME_FORMAT
- See Also:
- Constant Field Values
-
TIME_FORMAT
private static final java.lang.String TIME_FORMAT
- See Also:
- Constant Field Values
-
PADDING
private static final byte PADDING
Padding for the legend (from the text to the edge of the rectangle)- See Also:
- Constant Field Values
-
min
private double min
-
max
private double max
-
noDataColor
private java.awt.Color noDataColor
-
belowMinColor
private java.awt.Color belowMinColor
-
aboveMaxColor
private java.awt.Color aboveMaxColor
-
colors
private java.awt.Color[] colors
-
colorBarTitles
private java.lang.String[] colorBarTitles
-
title
private java.lang.String title
-
intervalCount
private int intervalCount
-
-
Constructor Detail
-
ColorScale
private ColorScale()
-
-
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 valuemax
- 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 benull
- 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
-
makeReversed
public ColorScale makeReversed()
Reverses this scale- 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 ontxt
- The text to draw the outlinex
- Text xy
- Text ycolor
- 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 onx
- Rect xy
- Rect yw
- Rect widthh
- Rect heightvalueScale
- 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 onx
- Rect xy
- Rect yw
- Color bar widthh
- Color bar heightminVal
- start time of the trackmaxVal
- 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 onx
- Rect xy
- Rect yw
- Color bar widthh
- Color bar heightfw
- The font widthfh
- The font heighttitleWidth
- 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 ony
- Rect yw
- Color bar widthh
- Color bar heightfw
- The font widthfh
- The font heightvalueScale
- The scale factor of the valuesxText
- 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 ony
- Rect yw
- Color bar widthh
- Color bar heightminVal
- start time of the trackmaxVal
- end time of the trackfw
- The font widthfh
- The font heightxText
- 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 ony
- Rect yw
- Color bar widthh
- Color bar heightfw
- The font widthfh
- The font heightxText
- The location to start drawing the text (x-axis)color
- The color of the text to drawtxt
- The text string to drawi
- The index of the legend (so we can calculate the y location)
-
-