1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui.components.ledder;
21 import java.awt.Color;
22
23
24
25
26
27
28
29
30
31
32 public class LedRendererOld {
33
34 private int centerX;
35 private int centerY;
36
37 private double radiusOffset;
38
39 private int radius;
40
41 private int red;
42 private int green;
43 private int blue;
44
45 private double highlightFactor = 0.85;
46 private double highlightFocus = 3;
47
48 private double shadowIntensity = 1.5;
49
50 private final float[] colorHSB = new float[3];
51
52 private double[] light = getLight(-0.2, -0.5, new double[3]);
53
54
55
56
57
58
59
60 public LedRendererOld(int x, int y, int r, LedPaintParameters lpp) {
61 super();
62
63 centerX = x;
64 centerY = y;
65 radius = r;
66
67 light = getLight(lpp.lightX, lpp.lightY, new double[3]);
68
69 highlightFactor = lpp.highlightFactor;
70 highlightFocus = lpp.highlightFocus;
71
72 shadowIntensity = lpp.shadowIntensity;
73
74 radiusOffset = (1 - lpp.borderSize) * radius;
75
76 red = lpp.color.getRed();
77 green = lpp.color.getGreen();
78 blue = lpp.color.getBlue();
79 }
80
81
82
83
84
85 private static double vLen(final double[] v) {
86 return Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
87 }
88
89
90
91
92
93
94
95 private static final double getZ(
96 final double x,
97 final double y,
98 final double r) {
99
100 return Math.sqrt(r * r - x * x - y * y);
101 }
102
103
104
105
106
107 private static final double[] norm(final double[] v) {
108 double l = vLen(v);
109 v[0] /= l;
110 v[1] /= l;
111 v[2] /= l;
112
113 return v;
114 }
115
116
117
118
119
120
121 private final float getHighlight(double x, final double p) {
122
123 x = (x - highlightFactor) / (1 - highlightFactor);
124
125 return (float) ((x < 0) ? p : p * (1 - Math.pow(x, highlightFocus)));
126 }
127
128
129
130
131
132
133 private final float getIntensity(final double x, final double p) {
134 return (float) (p * Math.pow(x, shadowIntensity));
135
136 }
137
138
139
140
141
142 private final int getColor(final double angle) {
143 Color.RGBtoHSB(red, green, blue, colorHSB);
144
145 return Color.HSBtoRGB(
146 colorHSB[0],
147 getHighlight(angle, colorHSB[1]),
148 getIntensity(angle, colorHSB[2]));
149 }
150
151
152
153
154
155
156
157 public double[] getLight(double x, double y, double[] v) {
158 v[0] = x;
159 v[1] = y;
160 v[2] = getZ(x, y, 1);
161
162 return norm(v);
163 }
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211 public final int[] renderSection(
212 final int x,
213 final int y,
214 final int w,
215 final int h) {
216
217 int[] data = new int[w * h * 4];
218
219 int col = 0;
220 int base = 0;
221
222 int ro1 = (int) radiusOffset * (int) radiusOffset;
223 int ro2 = radius * radius;
224
225 for (int py = 0; py < h; py++) {
226
227 base = 4 * (py * w);
228 int dy = y + py - centerY;
229
230 for (int px = 0; px < w; px++) {
231 int dx = x + px - centerX;
232
233
234 int l = dx * dx + dy * dy;
235
236 if (l < ro1) {
237
238 int dz = (int) Math.sqrt(ro2 - l);
239
240 double dot = dx * light[0] + dy * light[1] + dz * light[2];
241
242
243
244
245
246 double angle = (1 + dot / radius) / 2;
247
248 col = getColor(angle);
249
250 } else {
251 col = (l <= ro2) ? 0xFF000000 : 0x0;
252 }
253
254 data[base + 0] = (col >> 0x10) & 0xFF;
255 data[base + 1] = (col >> 0x08) & 0xFF;
256 data[base + 2] = (col >> 0x00) & 0xFF;
257 data[base + 3] = (col >> 0x18) & 0xFF;
258
259 base += 4;
260 }
261 }
262 return data;
263 }
264 }
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297