source: josm/trunk/src/org/openstreetmap/josm/tools/ColorScale.java@ 7342

Last change on this file since 7342 was 7319, checked in by akks, 10 years ago

Add colorbar for active GPX layer, big GpxLayer class refactoring, see #5662
new classes GpxDrawHelper and ColorScale responsible for GPX drawing and coloring
move data-related methods to GpxData
remove WayPoint.customColoringTransparent from memory, calculate it at paint-time

File size: 5.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2
3package org.openstreetmap.josm.tools;
4
5import java.awt.Color;
6import java.awt.FontMetrics;
7import java.awt.Graphics2D;
8
9/**
10 * Utility class that helps to work with color scale
11 * for coloring GPX tracks etc.
12 * @since 7319
13 */
14public class ColorScale {
15 private double min, max;
16 private Color noDataColor;
17 private Color belowMinColor;
18 private Color aboveMaxColor;
19
20 private Color[] colors;
21 private String title = "";
22 private int intervalCount = 5;
23
24 private ColorScale() {
25
26 }
27
28 public static ColorScale createHSBScale(int count) {
29 ColorScale sc = new ColorScale();
30 sc.colors = new Color[count];
31 for (int i = 0; i < count; i++) {
32 sc.colors[i] = Color.getHSBColor(i / 300.0f, 1, 1);
33 }
34 sc.setRange(0, 255);
35 sc.addBounds();
36 return sc;
37 }
38
39 public static ColorScale createCyclicScale(int count) {
40 ColorScale sc = new ColorScale();
41 // red yellow green blue red
42 int[] h = new int[] { 0, 59, 127, 244, 360};
43 int[] s = new int[] { 100, 84, 99, 100 };
44 int[] b = new int[] { 90, 93, 74, 83 };
45
46 sc.colors = new Color[count];
47 for (int i = 0; i < sc.colors.length; i++) {
48
49 float angle = 4 - i / 256f * 4;
50 int quadrant = (int) angle;
51 angle -= quadrant;
52 quadrant = Utils.mod(quadrant+1, 4);
53
54 float vh = h[quadrant] * w(angle) + h[quadrant+1] * (1 - w(angle));
55 float vs = s[quadrant] * w(angle) + s[Utils.mod(quadrant+1, 4)] * (1 - w(angle));
56 float vb = b[quadrant] * w(angle) + b[Utils.mod(quadrant+1, 4)] * (1 - w(angle));
57
58 sc.colors[i] = Color.getHSBColor(vh/360f, vs/100f, vb/100f);
59 }
60 sc.setRange(0, 2*Math.PI);
61 sc.addBounds();
62 return sc;
63 }
64
65 /**
66 * transition function:
67 * w(0)=1, w(1)=0, 0&lt;=w(x)&lt;=1
68 * @param x number: 0&lt;=x&lt;=1
69 * @return the weighted value
70 */
71 private static float w(float x) {
72 if (x < 0.5)
73 return 1 - 2*x*x;
74 else
75 return 2*(1-x)*(1-x);
76 }
77
78 public void setRange(double min, double max) {
79 this.min = min;
80 this.max = max;
81 }
82
83 /**
84 * Add standard colors for values below min or above max value
85 */
86 public void addBounds() {
87 aboveMaxColor = colors[colors.length-1];
88 belowMinColor = colors[0];
89 }
90
91 public final Color getColor(double value) {
92 if (value<min) return belowMinColor;
93 if (value>max) return aboveMaxColor;
94 if (Double.isNaN(value)) return noDataColor;
95 final int n = colors.length;
96 int idx = (int) ((value-min)*colors.length / (max-min));
97 if (idx<colors.length) {
98 return colors[idx];
99 } else {
100 return colors[n-1]; // this happens when value==max
101 }
102 // int hdoplvl =(int) Math.round(colorModeDynamic ? ((hdop-minval)*255/(maxval-minval))
103 // : (hdop <= 0 ? 0 : hdop * hdopfactor));
104 // int hdopcolor = 255 - (hdoplvl > 255 ? 255 : hdoplvl);
105 }
106
107 public final Color getColor(Number value) {
108 return (value==null)? noDataColor : getColor(value.doubleValue());
109 }
110
111 public Color getNoDataColor() {
112
113 return noDataColor;
114 }
115
116 public void setNoDataColor(Color noDataColor) {
117 this.noDataColor = noDataColor;
118 }
119
120 public ColorScale makeTransparent(int alpha) {
121 for (int i = 0; i < colors.length; i++) {
122 colors[i] = new Color((colors[i].getRGB() & 0xFFFFFF) | ((alpha & 0xFF) << 24), true);
123 }
124 return this;
125 }
126
127 public ColorScale addTitle(String title) {
128 this.title = title;
129 return this;
130 }
131
132 public ColorScale setIntervalCount(int intervalCount) {
133 this.intervalCount = intervalCount;
134 return this;
135 }
136
137 public ColorScale makeReversed() {
138 int n = colors.length;
139 Color tmp;
140 for (int i=0; i<n/2; i++) {
141 tmp = colors[i];
142 colors[i] = colors[n-1-i];
143 colors[n-1-i] = tmp;
144 }
145 tmp = belowMinColor;
146 belowMinColor = aboveMaxColor;
147 aboveMaxColor = tmp;
148 return this;
149 }
150
151 public void drawColorBar(Graphics2D g, int x, int y, int w, int h, double valueScale) {
152 int n=colors.length;
153
154 for (int i=0; i<n; i++) {
155 g.setColor(colors[i]);
156 if (w<h) {
157 g.fillRect(x, y+i*h/n, w, h/n+1);
158 } else {
159 g.fillRect(x+i*w/n, y, w/n+1, h);
160 }
161 }
162
163 int fw, fh;
164 FontMetrics fm = g.getFontMetrics();
165 fh = fm.getHeight()/2;
166 fw = fm.stringWidth(String.valueOf(Math.max((int)Math.abs(max*valueScale), (int)Math.abs(min*valueScale)))) + fm.stringWidth("0.123");
167 g.setColor(noDataColor);
168 if (title != null) {
169 g.drawString(title, x-fw-3, y-fh*3/2);
170 }
171 for (int i=0; i<=intervalCount; i++) {
172 g.setColor(colors[(int)(1.0*i*n/intervalCount-1e-10)]);
173 final double val = min+i*(max-min)/intervalCount;
174 final String txt = String.format("%.3f", val*valueScale);
175 if (w<h) {
176 g.drawString(txt, x-fw-3, y+i*h/intervalCount+fh/2);
177 } else {
178 g.drawString(txt, x+i*w/intervalCount-fw/2, y+fh-3);
179 }
180 }
181 }
182}
Note: See TracBrowser for help on using the repository browser.