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

Last change on this file since 10809 was 10378, checked in by Don-vip, 8 years ago

Checkstyle 6.19: enable SingleSpaceSeparator and fix violations

  • Property svn:eol-style set to native
File size: 5.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.awt.Color;
5import java.awt.FontMetrics;
6import java.awt.Graphics2D;
7
8/**
9 * Utility class that helps to work with color scale for coloring GPX tracks etc.
10 * @since 7319
11 */
12public final class ColorScale {
13 private double min, max;
14 private Color noDataColor;
15 private Color belowMinColor;
16 private Color aboveMaxColor;
17
18 private Color[] colors;
19 private String title = "";
20 private int intervalCount = 5;
21
22 private ColorScale() {
23
24 }
25
26 public static ColorScale createHSBScale(int count) {
27 ColorScale sc = new ColorScale();
28 sc.colors = new Color[count];
29 for (int i = 0; i < count; i++) {
30 sc.colors[i] = Color.getHSBColor(i / 300.0f, 1, 1);
31 }
32 sc.setRange(0, 255);
33 sc.addBounds();
34 return sc;
35 }
36
37 public static ColorScale createCyclicScale(int count) {
38 ColorScale sc = new ColorScale();
39 // CHECKSTYLE.OFF: SingleSpaceSeparator
40 // red yellow green blue red
41 int[] h = new int[] {0, 59, 127, 244, 360};
42 int[] s = new int[] {100, 84, 99, 100};
43 int[] b = new int[] {90, 93, 74, 83};
44 // CHECKSTYLE.ON: SingleSpaceSeparator
45
46 sc.colors = new Color[count];
47 for (int i = 0; i < sc.colors.length; i++) {
48
49 float angle = 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 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 }
103
104 public Color getColor(Number value) {
105 return (value == null) ? noDataColor : getColor(value.doubleValue());
106 }
107
108 public Color getNoDataColor() {
109 return noDataColor;
110 }
111
112 public void setNoDataColor(Color noDataColor) {
113 this.noDataColor = noDataColor;
114 }
115
116 public ColorScale makeTransparent(int alpha) {
117 for (int i = 0; i < colors.length; i++) {
118 colors[i] = new Color((colors[i].getRGB() & 0xFFFFFF) | ((alpha & 0xFF) << 24), true);
119 }
120 return this;
121 }
122
123 public ColorScale addTitle(String title) {
124 this.title = title;
125 return this;
126 }
127
128 public ColorScale setIntervalCount(int intervalCount) {
129 this.intervalCount = intervalCount;
130 return this;
131 }
132
133 public ColorScale makeReversed() {
134 int n = colors.length;
135 Color tmp;
136 for (int i = 0; i < n/2; i++) {
137 tmp = colors[i];
138 colors[i] = colors[n-1-i];
139 colors[n-1-i] = tmp;
140 }
141 tmp = belowMinColor;
142 belowMinColor = aboveMaxColor;
143 aboveMaxColor = tmp;
144 return this;
145 }
146
147 public void drawColorBar(Graphics2D g, int x, int y, int w, int h, double valueScale) {
148 int n = colors.length;
149
150 for (int i = 0; i < n; i++) {
151 g.setColor(colors[i]);
152 if (w < h) {
153 g.fillRect(x, y+i*h/n, w, h/n+1);
154 } else {
155 g.fillRect(x+i*w/n, y, w/n+1, h);
156 }
157 }
158
159 int fw, fh;
160 FontMetrics fm = g.getFontMetrics();
161 fh = fm.getHeight()/2;
162 fw = fm.stringWidth(String.valueOf(Math.max((int) Math.abs(max*valueScale),
163 (int) Math.abs(min*valueScale)))) + fm.stringWidth("0.123");
164 g.setColor(noDataColor);
165 if (title != null) {
166 g.drawString(title, x-fw-3, y-fh*3/2);
167 }
168 for (int i = 0; i <= intervalCount; i++) {
169 g.setColor(colors[(int) (1.0*i*n/intervalCount-1e-10)]);
170 final double val = min+i*(max-min)/intervalCount;
171 final String txt = String.format("%.3f", val*valueScale);
172 if (w < h) {
173 g.drawString(txt, x-fw-3, y+i*h/intervalCount+fh/2);
174 } else {
175 g.drawString(txt, x+i*w/intervalCount-fw/2, y+fh-3);
176 }
177 }
178 }
179}
Note: See TracBrowser for help on using the repository browser.