-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRGBChatColor.java
399 lines (366 loc) · 14.4 KB
/
RGBChatColor.java
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import java.awt.Color;
import java.util.*;
import java.util.Map.Entry;
import org.bukkit.ChatColor;
public class RGBChatColor {
public static Map<String, RGBValue> chatValue = new HashMap<>();
private static String BLACK_BAR = Character.toString((char) 0x2588);
private static String bol = (ChatColor.BOLD + "");
static {
chatValue.put(ChatColor.BLACK + bol + "#", new RGBValue(new Color(0, 0,
0)));
addColor("C", new double[] { 155, 53, 128, 56 }, new double[] { 155,
53, 128, 56 }, new double[] { 155, 53, 128, 56 });
addColor("$", new double[] { 108, 94, 121, 94 }, new double[] { 108,
94, 121, 94 }, new double[] { 108, 94, 121, 94 });
addColor("X", new double[] { 105, 86, 93, 76 }, new double[] { 105, 86,
93, 76 }, new double[] { 105, 86, 93, 76 });
addColor("M", new double[] { 151, 125, 93, 80 }, new double[] { 151,
125, 93, 80 }, new double[] { 151, 125, 93, 80 });
addColor("O", new double[] { 138, 107, 104, 93 }, new double[] { 138,
107, 104, 93 }, new double[] { 138, 107, 104, 93 });
addColor("0", new double[] { 159, 131, 127, 102 }, new double[] { 159,
131, 127, 102 }, new double[] { 159, 131, 127, 102 });
addColor("/", new double[] { 36, 84, 82, 10 }, new double[] { 36, 84,
82, 10 }, new double[] { 36, 84, 82, 10 });
addColor("\\", new double[] { 84, 36, 10, 82 }, new double[] { 84, 36,
10, 82 }, new double[] { 84, 36, 10, 82 });
addColor("_", new double[] { 0, 0, 63, 51 }, new double[] { 0, 0, 63,
51 }, new double[] { 0, 0, 63, 51 });
///addColor("~", new double[] { 71, 51, 0, 0 }, new double[] { 71, 51, 0,
// 0 }, new double[] { 71, 51, 0, 0 });
addColor("P", new double[] { 201, 84, 94, 0 }, new double[] { 201, 84,
94, 0 }, new double[] { 201, 84, 94, 0 });
addColor("a", new double[] { 63, 63, 110, 150 }, new double[] { 63, 63,
110, 150 }, new double[] { 63, 63, 110, 150 });
addColor("=", new double[] { 80, 65, 80, 75 }, new double[] { 80, 65,
80, 75 }, new double[] { 80, 65, 80, 75 });
addColor("-", new double[] { 44, 33, 48, 43 }, new double[] { 44, 33,
48, 43 }, new double[] { 44, 33, 48, 43 });
addColor("J", new double[] { 0, 90, 63, 90 }, new double[] { 0, 90, 63,
90 }, new double[] { 0, 90, 63, 90 });
addColor("U", new double[] { 110, 90, 100, 90 }, new double[] { 110,
90, 100, 90 }, new double[] { 110, 90, 100, 90 });
addColor("T", new double[] { 115, 95, 40, 30 }, new double[] { 115, 95,
40, 30 }, new double[] { 115, 95, 40, 30 });
addColor("E", new double[] { 191, 60, 130, 35 }, new double[] { 191,
60, 130, 35 }, new double[] { 191, 60, 130, 35 });
addColor("8", new double[] { 144, 111, 105, 90 }, new double[] { 144,
111, 105, 90 }, new double[] { 144, 111, 105, 90 });
addColor("4", new double[] { 90, 147, 70, 105 }, new double[] { 90,
147, 70, 105 }, new double[] { 90, 147, 70, 105 });
addColor("2", new double[] { 98, 104, 118, 105 }, new double[] { 98,
104, 118, 105 }, new double[] { 98, 104, 118, 105 });
addColor("6", new double[] { 135, 65, 126, 91 }, new double[] { 135,
65, 126, 91 }, new double[] { 135, 65, 126, 91 });
addColor("u", new double[] { 66, 48, 111, 124 }, new double[] { 66, 48,
111, 124 }, new double[] { 66, 48, 111, 124 });
addColor("Y", new double[] { 80, 70, 44, 35 }, new double[] { 80, 70,
44, 35 }, new double[] { 80, 70, 44, 35 });
addColor("I!", new double[] { 136, 123, 113, 78 }, new double[] { 136,
123, 113, 78 }, new double[] { 136, 123, 113, 78 });
addColor("!I", new double[] { 156, 116, 102, 104 }, new double[] { 156,
116, 102, 104 }, new double[] { 156, 116, 102, 104 });
addColor("h", new double[] { 150, 50, 108, 88 }, new double[] { 150,
50, 108, 88 }, new double[] { 150, 50, 108, 88 });
addColor("H", new double[] { 155, 125, 100, 85 }, new double[] { 155,
125, 100, 85 }, new double[] { 155, 125, 100, 85 });
addColor("x", new double[] { 50, 50, 90, 71 }, new double[] { 50, 50,
90, 71 }, new double[] { 50, 50, 90, 71 });
addColor("q", new double[] { 80, 80, 70, 135 }, new double[] { 80, 80,
70, 135 }, new double[] { 80, 80, 70, 135 });
addColor("![", new double[] { 175, 81, 117, 77 }, new double[] { 175,
81, 117, 77 }, new double[] { 175, 81, 117, 77 });
addColor("]!", new double[] { 105, 146, 90, 191 }, new double[] { 105,
146, 90, 191 }, new double[] { 105, 146, 90, 191 });
addColor("S", new double[] { 117, 125, 68, 94 }, new double[] { 117,
125, 68, 94 }, new double[] { 117, 125, 68, 94 });
addColor("N", new double[] { 146, 116, 100, 90 }, new double[] { 146,
116, 100, 90 }, new double[] { 146, 116, 100, 90 });
addColor("n", new double[] { 106, 58, 107, 86 }, new double[] { 106,
58, 107, 86 }, new double[] { 106, 58, 107, 86 });
addColor("b", new double[] { 146, 50, 148, 99 }, new double[] { 146,
50, 148, 99 }, new double[] { 146, 50, 148, 99 });
addColor("B", new double[] { 197, 109, 133, 91 }, new double[] { 197,
109, 133, 91 }, new double[] { 197, 109, 133, 91 });
addColor("w", new double[] { 67, 52, 133, 141 }, new double[] { 67, 52,
133, 141 }, new double[] { 67, 52, 133, 141 });
addColor("W", new double[] { 121, 100, 133, 105 }, new double[] { 121,
100, 133, 105 }, new double[] { 121, 100, 133, 105 });
addColor("#", new double[] { 178, 136, 132, 116 }, new double[] { 178,
136, 132, 116 }, new double[] { 178, 136, 132, 116 });
addColor("f", new double[] { 129, 114, 82, 33 }, new double[] {129, 114, 82, 33}, new double[] { 129, 114, 82, 33 });
addColor("F", new double[] { 194, 68, 92, 0 }, new double[] {194, 68, 92, 0 }, new double[] { 194, 68, 92, 0 });
addColor("d", new double[] { 80, 122, 112, 124 }, new double[] {80, 122, 112, 124 }, new double[] { 80, 122, 112, 124 });
addColor("D", new double[] { 153, 101, 120, 84 }, new double[] {153, 101, 120, 84 }, new double[] {153, 101, 120, 84 });
addColor("A", new double[] { 174, 137, 94, 80 }, new double[] { 174, 137, 94, 80 }, new double[] { 174, 137, 94, 80 });
addColor("k", new double[] { 162, 32, 146, 55 }, new double[] { 162, 32, 146, 55}, new double[] {162, 32, 146, 55});
addColor("K", new double[] { 157, 75, 100, 70 }, new double[] { 157, 75, 100, 70}, new double[] {157, 75, 100, 70});
addColor("v", new double[] { 62, 50, 80, 71 }, new double[] { 62, 50, 80, 71 }, new double[] { 62, 50, 80, 71 });
addColor("V", new double[] { 119, 98, 79, 70 }, new double[] {119, 98, 79, 70 }, new double[] { 119, 98, 79, 70 });
addColor("L", new double[] { 113, 0, 133, 54 }, new double[] {113, 0, 133, 54 }, new double[] { 113, 0, 133, 54 });
addColor("ll", new double[] { 144, 82, 101, 75 }, new double[] {144, 82, 101, 75 }, new double[] { 144, 82, 101, 75 });
addColor("1", new double[] { 73, 42, 99, 83 }, new double[] {73, 42, 99, 83 }, new double[] { 73, 42, 99, 83 });
}
private static void addColor(String ch, double[] r, double[] g, double[] b) {
double[] c = new double[4 * 3];
for (int k = 0; k < 4; k++) {
c[(k * 3) + 0] = ((double) r[k]) / 255;
c[(k * 3) + 1] = ((double) g[k]) / 255;
c[(k * 3) + 2] = ((double) b[k]) / 255;
}
int[][] mods = { { 0, 0, 0 }, // black 0
{ 0, 0, 170 },// darkblue 1
{ 0, 170, 0 }, // dark green 2
{ 0, 170, 170 }, // cyan 3
{ 170, 0, 0 }, // dark red 4
{ 170, 0, 170 }, // purple 5
{ 255, 170, 0 }, // gold 6
{ 170, 170, 170 }, // light gray 7
{ 85, 85, 85 }, // dark gray 8
{ 85, 85, 255 }, // blue 9
{ 85, 255, 85 },// green a
{ 85, 255, 255 }, // light blue b
{ 255, 85, 85 }, // red c
{ 255, 85, 255 }, // light pink d
{ 255, 255, 85 }, // yellow e
{ 255, 255, 255 } // white f
};
int cm = 0;
for (ChatColor cc : ChatColor.values()) {
if (cc == ChatColor.BOLD || cc == ChatColor.STRIKETHROUGH
|| cc == ChatColor.RESET || cc == ChatColor.ITALIC
|| cc == ChatColor.MAGIC || cc == ChatColor.UNDERLINE)
continue;
int x = 0;
chatValue.put(cc + bol + ch, new RGBValue(new Color(
(int) (c[x] * mods[cm][x++ % 3]),
(int) (c[x] * mods[cm][x++ % 3]),
(int) (c[x] * mods[cm][x++ % 3])), new Color(
(int) (c[x] * mods[cm][x++ % 3]),
(int) (c[x] * mods[cm][x++ % 3]),
(int) (c[x] * mods[cm][x++ % 3])), new Color(
(int) (c[x] * mods[cm][x++ % 3]),
(int) (c[x] * mods[cm][x++ % 3]),
(int) (c[x] * mods[cm][x++ % 3])), new Color(
(int) (c[x] * mods[cm][x++ % 3]),
(int) (c[x] * mods[cm][x++ % 3]),
(int) (c[x] * mods[cm][x++ % 3]))));
cm++;
}
}
/**
* The color value of the four closest colors. Use this if you want to
* preserve hard edges in images. For the array, you need four color values.
* Use the following chart to understand which pixel should be at which
* index:
*
* | 0 | 1 |
*
* |---|---|
*
* | 2 | 3 |
*
* @param c
* - The color value
* @return The closest material and durability.
*/
public static String getClosestBlockValue(Color c[]) {
int[] r = new int[4];
int[] b = new int[4];
int[] g = new int[4];
for (int i = 0; i < c.length; i++) {
r[i] = c[i].getRed();
b[i] = c[i].getBlue();
g[i] = c[i].getGreen();
}
double cR = Integer.MAX_VALUE;
double cG = Integer.MAX_VALUE;
double cB = Integer.MAX_VALUE;
String closest = null;
double[] tR = new double[4];
double[] tG = new double[4];
double[] tB = new double[4];
for (Entry<String, RGBValue> entry : chatValue.entrySet()) {
for (int i = 0; i < 4; i++) {
tR[i] = entry.getValue().r[i] - r[i];
tG[i] = entry.getValue().g[i] - g[i];
tB[i] = entry.getValue().b[i] - b[i];
if (tR[i] < 0)
tR[i] = -tR[i];
if (tG[i] < 0)
tG[i] = -tG[i];
if (tB[i] < 0)
tB[i] = -tB[i];
}
if ((tR[0] * tR[0]) + (tG[0] * tG[0]) + (tB[0] * tB[0])
+ (tR[1] * tR[1]) + (tG[1] * tG[1]) + (tB[1] * tB[1])
+ (tR[2] * tR[2]) + (tG[2] * tG[2]) + (tB[2] * tB[2])
+ (tR[3] * tR[3]) + (tG[3] * tG[3]) + (tB[3] * tB[3]) < (cR
+ cG + cB)) {
cR = 0;
cB = 0;
cG = 0;
for (int i = 0; i < 4; i++) {
cR += (tR[i] * tR[i]);
cG += (tG[i] * tG[i]);
cB += (tB[i] * tB[i]);
}
closest = entry.getKey();
}
}
return closest;
}
/**
* This gets all the pixel values for an image. Use this to get all the
* pixels for an image.
*
* The first array stores the Row value (e.g. MC's "Y" value), and the
* second array stores the Columb value (MC's X or Z)
*
* For example: If you want to get the pixel at the top left of an image,
* use convertTo2DWithoutUsingGetRGB(Image)[HEIGHT][0]
*
* For example: If you want to get the pixel at the bottom right of an
* image, use convertTo2DWithoutUsingGetRGB(Image)[0][WIDTH]
*
* @param image
* @return
*/
public static Pixel[][] convertTo2DWithoutUsingGetRGB(BufferedImage image) {
if (image.getRaster().getDataBuffer() instanceof DataBufferByte) {
final byte[] pixels = ((DataBufferByte) image.getRaster()
.getDataBuffer()).getData();
final int width = image.getWidth();
final int height = image.getHeight();
final boolean hasAlphaChannel = image.getAlphaRaster() != null;
Pixel[][] result = new Pixel[height][width];
if (hasAlphaChannel) {
final int pixelLength = 4;
for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) {
int r = 0;
int b = 0;
int g = 0;
// argb += (((int) pixels[pixel] & 0xff) << 24); // alpha
b += ((int) pixels[pixel + 1] & 0xff); // blue
g += (((int) pixels[pixel + 2] & 0xff) /* << 8 */); // green
r += (((int) pixels[pixel + 3] & 0xff) /* << 16 */); // red
result[row][col] = new Pixel(r, g, b);
col++;
if (col == width) {
col = 0;
row++;
}
}
} else {
final int pixelLength = 3;
for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) {
int r = 0;
int b = 0;
int g = 0;
// argb += -16777216; // 255 alpha
b += ((int) pixels[pixel] & 0xff); // blue
g += (((int) pixels[pixel + 1] & 0xff) /* <<8 */); // green
r += (((int) pixels[pixel + 2] & 0xff) /* <<16 */); // red
result[row][col] = new Pixel(r, g, b);
col++;
if (col == width) {
col = 0;
row++;
}
}
}
return result;
} else if (image.getRaster().getDataBuffer() instanceof DataBufferInt) {
final int[] pixels = ((DataBufferInt) image.getRaster()
.getDataBuffer()).getData();
final int width = image.getWidth();
final int height = image.getHeight();
final boolean hasAlphaChannel = image.getAlphaRaster() != null;
Pixel[][] result = new Pixel[height][width];
if (hasAlphaChannel) {
final int pixelLength = 4;
for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) {
int r = 0;
int b = 0;
int g = 0;
// argb += (((int) pixels[pixel] & 0xff) << 24); // alpha
b += ((int) pixels[pixel + 1] & 0xff); // blue
g += (((int) pixels[pixel + 2] & 0xff) /* << 8 */); // green
r += (((int) pixels[pixel + 3] & 0xff) /* << 16 */); // red
result[row][col] = new Pixel(r, g, b);
col++;
if (col == width) {
col = 0;
row++;
}
}
} else {
final int pixelLength = 1;
for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) {
int r = 0;
int b = 0;
int g = 0;
int rgb = pixels[pixel];
r = (rgb >> 16) & 0xFF;
g = (rgb >> 8) & 0xFF;
b = rgb & 0xFF;
result[row][col] = new Pixel(r, g, b);
col++;
if (col == width) {
col = 0;
row++;
}
}
}
return result;
}
return null;
}
}
class RGBValue {
int[] r = new int[4];
int[] b = new int[4];
int[] g = new int[4];
public double[] rRat = new double[4];
public double[] gRat = new double[4];
public double[] bRat = new double[4];
public RGBValue(Color c) {
init(c, c, c, c);
}
public RGBValue(Color c, Color c2, Color c3, Color c4) {
init(c, c2, c3, c4);
}
private void init(Color c, Color c2, Color c3, Color c4) {
this.r[0] = c.getRed();
this.g[0] = c.getGreen();
this.b[0] = c.getBlue();
// /
this.r[1] = c2.getRed();
this.g[1] = c2.getGreen();
this.b[1] = c2.getBlue();
// /
this.r[2] = c3.getRed();
this.g[2] = c3.getGreen();
this.b[2] = c3.getBlue();
//
this.r[3] = c4.getRed();
this.g[3] = c4.getGreen();
this.b[3] = c4.getBlue();
for (int i = 0; i < 4; i++) {
rRat[i] = r[i];
gRat[i] = g[i];
bRat[i] = b[i];
}
}
}
class Pixel {
public int r;
public int b;
public int g;
public Pixel(int r, int g, int b) {
this.r = r;
this.b = b;
this.g = g;
}
}