1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package org.spf4j.perf.impl.chart;
33
34 import java.awt.Color;
35 import java.awt.Paint;
36 import java.io.Serializable;
37 import org.jfree.chart.renderer.PaintScale;
38 import org.jfree.chart.util.PublicCloneable;
39 import org.spf4j.base.CloneFailedException;
40
41
42
43
44
45
46 public final class InverseGrayScale
47 implements PaintScale, PublicCloneable, Serializable {
48
49 private static final long serialVersionUID = 1L;
50
51
52 private final double lowerBound;
53
54
55 private final double upperBound;
56
57
58
59
60
61
62 private final int alpha;
63
64
65
66
67 public InverseGrayScale() {
68 this(0.0, 1.0);
69 }
70
71
72
73
74
75
76
77
78
79
80 public InverseGrayScale(final double lowerBound, final double upperBound) {
81 this(lowerBound, upperBound, 255);
82 }
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97 public InverseGrayScale(final double lowerBound, final double upperBound, final int alpha) {
98 if (lowerBound >= upperBound) {
99 throw new IllegalArgumentException(
100 "Requires " + lowerBound + " < " + upperBound + '.');
101 }
102 if (alpha < 0 || alpha > 255) {
103 throw new IllegalArgumentException(
104 "Requires alpha in the range 0 to 255. and not " + alpha);
105
106 }
107 this.lowerBound = lowerBound;
108 this.upperBound = upperBound;
109 this.alpha = alpha;
110 }
111
112
113
114
115
116
117
118
119 @Override
120 public double getLowerBound() {
121 return this.lowerBound;
122 }
123
124
125
126
127
128
129
130
131 @Override
132 public double getUpperBound() {
133 return this.upperBound;
134 }
135
136
137
138
139
140
141
142
143 public int getAlpha() {
144 return this.alpha;
145 }
146
147
148
149
150
151
152
153
154
155 @Override
156 public Paint getPaint(final double value) {
157 double v = Math.max(value, this.lowerBound);
158 v = Math.min(v, this.upperBound);
159 int g;
160 if (value >= 1) {
161 g = 240 - (int) ((v - this.lowerBound) / (this.upperBound
162 - this.lowerBound) * 240.0);
163 } else {
164 g = 255 - (int) ((v - this.lowerBound) / (this.upperBound
165 - this.lowerBound) * 15.0);
166 }
167
168
169
170 return new Color(g, g, g, this.alpha);
171 }
172
173 @Override
174 public boolean equals(final Object obj) {
175 if (obj == null) {
176 return false;
177 }
178 if (getClass() != obj.getClass()) {
179 return false;
180 }
181 final InverseGrayScale other = (InverseGrayScale) obj;
182 if (Double.doubleToLongBits(this.lowerBound) != Double.doubleToLongBits(other.lowerBound)) {
183 return false;
184 }
185 if (Double.doubleToLongBits(this.upperBound) != Double.doubleToLongBits(other.upperBound)) {
186 return false;
187 }
188 return this.alpha == other.alpha;
189 }
190
191 @Override
192 public int hashCode() {
193 int hash = 3;
194 hash = 97 * hash
195 + (int) (Double.doubleToLongBits(this.lowerBound) ^ (Double.doubleToLongBits(this.lowerBound) >>> 32));
196 hash = 97 * hash
197 + (int) (Double.doubleToLongBits(this.upperBound) ^ (Double.doubleToLongBits(this.upperBound) >>> 32));
198 return 97 * hash + this.alpha;
199 }
200
201
202
203
204
205
206
207
208
209 @Override
210 public InverseGrayScale clone() {
211 try {
212 return (InverseGrayScale) super.clone();
213 } catch (CloneNotSupportedException ex) {
214 throw new CloneFailedException("Failed cloning " + this, ex);
215 }
216 }
217
218 @Override
219 public String toString() {
220 return "InverseGrayScale{" + "lowerBound=" + lowerBound + ", upperBound=" + upperBound
221 + ", alpha=" + alpha + '}';
222 }
223
224 }