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

Last change on this file since 8468 was 8443, checked in by Don-vip, 9 years ago

remove extra whitespaces

  • Property svn:eol-style set to native
File size: 5.2 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 // red yellow green blue red
40 int[] h = new int[] {0, 59, 127, 244, 360};
41 int[] s = new int[] {100, 84, 99, 100};
42 int[] b = new int[] {90, 93, 74, 83};
43
44 sc.colors = new Color[count];
45 for (int i = 0; i < sc.colors.length; i++) {
46
47 float angle = 4 - i / 256f * 4;
48 int quadrant = (int) angle;
49 angle -= quadrant;
50 quadrant = Utils.mod(quadrant+1, 4);
51
52 float vh = h[quadrant] * w(angle) + h[quadrant+1] * (1 - w(angle));
53 float vs = s[quadrant] * w(angle) + s[Utils.mod(quadrant+1, 4)] * (1 - w(angle));
54 float vb = b[quadrant] * w(angle) + b[Utils.mod(quadrant+1, 4)] * (1 - w(angle));
55
56 sc.colors[i] = Color.getHSBColor(vh/360f, vs/100f, vb/100f);
57 }
58 sc.setRange(0, 2*Math.PI);
59 sc.addBounds();
60 return sc;
61 }
62
63 /**
64 * transition function:
65 * w(0)=1, w(1)=0, 0&lt;=w(x)&lt;=1
66 * @param x number: 0&lt;=x&lt;=1
67 * @return the weighted value
68 */
69 private static float w(float x) {
70 if (x < 0.5)
71 return 1 - 2*x*x;
72 else
73 return 2*(1-x)*(1-x);
74 }
75
76 public void setRange(double min, double max) {
77 this.min = min;
78 this.max = max;
79 }
80
81 /**
82 * Add standard colors for values below min or above max value
83 */
84 public void addBounds() {
85 aboveMaxColor = colors[colors.length-1];
86 belowMinColor = colors[0];
87 }
88
89 public final Color getColor(double value) {
90 if (value<min) return belowMinColor;
91 if (value>max) return aboveMaxColor;
92 if (Double.isNaN(value)) return noDataColor;
93 final int n = colors.length;
94 int idx = (int) ((value-min)*colors.length / (max-min));
95 if (idx<colors.length) {
96 return colors[idx];
97 } else {
98 return colors[n-1]; // this happens when value==max
99 }
100 }
101
102 public final Color getColor(Number value) {
103 return (value==null)? noDataColor : getColor(value.doubleValue());
104 }
105
106 public Color getNoDataColor() {
107 return noDataColor;
108 }
109
110 public void setNoDataColor(Color noDataColor) {
111 this.noDataColor = noDataColor;
112 }
113
114 public ColorScale makeTransparent(int alpha) {
115 for (int i = 0; i < colors.length; i++) {
116 colors[i] = new Color((colors[i].getRGB() & 0xFFFFFF) | ((alpha & 0xFF) << 24), true);
117 }
118 return this;
119 }
120
121 public ColorScale addTitle(String title) {
122 this.title = title;
123 return this;
124 }
125
126 public ColorScale setIntervalCount(int intervalCount) {
127 this.intervalCount = intervalCount;
128 return this;
129 }
130
131 public ColorScale makeReversed() {
132 int n = colors.length;
133 Color tmp;
134 for (int i=0; i<n/2; i++) {
135 tmp = colors[i];
136 colors[i] = colors[n-1-i];
137 colors[n-1-i] = tmp;
138 }
139 tmp = belowMinColor;
140 belowMinColor = aboveMaxColor;
141 aboveMaxColor = tmp;
142 return this;
143 }
144
145 public void drawColorBar(Graphics2D g, int x, int y, int w, int h, double valueScale) {
146 int n=colors.length;
147
148 for (int i=0; i<n; i++) {
149 g.setColor(colors[i]);
150 if (w<h) {
151 g.fillRect(x, y+i*h/n, w, h/n+1);
152 } else {
153 g.fillRect(x+i*w/n, y, w/n+1, h);
154 }
155 }
156
157 int fw, fh;
158 FontMetrics fm = g.getFontMetrics();
159 fh = fm.getHeight()/2;
160 fw = fm.stringWidth(String.valueOf(Math.max((int)Math.abs(max*valueScale), (int)Math.abs(min*valueScale)))) + fm.stringWidth("0.123");
161 g.setColor(noDataColor);
162 if (title != null) {
163 g.drawString(title, x-fw-3, y-fh*3/2);
164 }
165 for (int i=0; i<=intervalCount; i++) {
166 g.setColor(colors[(int)(1.0*i*n/intervalCount-1e-10)]);
167 final double val = min+i*(max-min)/intervalCount;
168 final String txt = String.format("%.3f", val*valueScale);
169 if (w<h) {
170 g.drawString(txt, x-fw-3, y+i*h/intervalCount+fh/2);
171 } else {
172 g.drawString(txt, x+i*w/intervalCount-fw/2, y+fh-3);
173 }
174 }
175 }
176}
Note: See TracBrowser for help on using the repository browser.