-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGuildMarkRender.java
More file actions
81 lines (75 loc) · 2.52 KB
/
Copy pathGuildMarkRender.java
File metadata and controls
81 lines (75 loc) · 2.52 KB
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
package provider.service;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class GuildMarkRender {
/**
* Split String using substring, you'll have to tell where to split
* @param src String to split
* @param len where to split
* @return
*/
private static String[] splitMarkerCode(String src, int len) {
String[] result = new String[(int)Math.ceil((double)src.length()/(double)len)];
for (int i=0; i<result.length; i++)
result[i] = src.substring(i*len, Math.min(src.length(), (i+1)*len));
return result;
}
private static void colorMapping(Graphics2D g2d, char charAtPosition){
switch (charAtPosition){
case '2': g2d.setColor(Color.GRAY);
break;
case '3': g2d.setColor(Color.WHITE);
break;
case '4': g2d.setColor(Color.RED);
break;
case '5': g2d.setColor(Color.ORANGE);
break;
case '6': g2d.setColor(Color.YELLOW);
break;
case '7':
case '8':
case '9':
g2d.setColor(Color.GREEN);
break;
case 'A': g2d.setColor(Color.CYAN);
break;
case 'B':
case 'C':
g2d.setColor(Color.BLUE);
break;
case 'D':
case 'E':
g2d.setColor(Color.MAGENTA);
break;
case 'F': g2d.setColor(Color.PINK);
break;
case '1':
case '0':
default: g2d.setColor(Color.BLACK);
}
}
private static byte[] renderMark(String markCode){
int type = BufferedImage.TYPE_INT_ARGB;
BufferedImage image = new BufferedImage(8, 8, type);
Graphics2D g2d = image.createGraphics();
String[] arr = splitMarkerCode(markCode, 8);
for (int a = 0; a < arr.length; a++){
for (int z = 0; z < arr[a].length(); z++){
char charAtPosition = arr[a].charAt(z);
colorMapping(g2d, charAtPosition);
g2d.fillRect(z, a, 1, 1);
}
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ImageIO.write(image, "png", baos);
} catch (IOException e) {
e.printStackTrace();
}
g2d.dispose();
return baos.toByteArray();
}
}