/
dColor.java
319 lines (262 loc) · 9.59 KB
/
dColor.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
package net.aufdemrand.denizen.objects;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.Fetchable;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.objects.properties.Property;
import net.aufdemrand.denizencore.objects.properties.PropertyParser;
import net.aufdemrand.denizencore.tags.Attribute;
import net.aufdemrand.denizencore.tags.TagContext;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import org.bukkit.Color;
import org.bukkit.DyeColor;
import java.lang.reflect.Field;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class dColor implements dObject {
final static Pattern rgbPattern = Pattern.compile("(\\d+)[,:](\\d+)[,:](\\d+)");
//////////////////
// OBJECT FETCHER
////////////////
public static dColor valueOf(String string) {
return valueOf(string, null);
}
/**
* Gets a Color Object from a string form.
*
* @param string the string
* @return a Color, or null if incorrectly formatted
*/
@Fetchable("co")
public static dColor valueOf(String string, TagContext context) {
string = string.toUpperCase().replace("CO@", "");
if (string.matches("RANDOM")) {
// Get a color using random RGB values
return new dColor(CoreUtilities.getRandom().nextInt(256),
CoreUtilities.getRandom().nextInt(256),
CoreUtilities.getRandom().nextInt(256));
}
Matcher m = rgbPattern.matcher(string);
if (m.matches())
return new dColor(aH.getIntegerFrom(m.group(1)),
aH.getIntegerFrom(m.group(2)),
aH.getIntegerFrom(m.group(3)));
Field colorField = null;
try {
colorField = Color.class.getField(string.toUpperCase());
}
catch (SecurityException e1) {
dB.echoError("Security exception getting color field!");
}
catch (NoSuchFieldException e1) {
dB.echoError("No such color field!");
}
if (colorField != null)
return new dColor(colorField);
// No match
return null;
}
/**
* Determines whether a string is a valid color.
*
* @param arg the string
* @return true if matched, otherwise false
*/
public static boolean matches(String arg) {
arg = arg.toUpperCase().replace("CO@", "");
if (arg.toUpperCase().matches("RANDOM"))
return true;
Matcher m = rgbPattern.matcher(arg);
if (m.matches())
return true;
for (Field field : Color.class.getFields())
if (arg.toUpperCase().matches(field.getName())) return true;
return false;
}
///////////////
// Constructors
/////////////
public dColor(int red, int green, int blue) {
color = Color.fromRGB(red, green, blue);
}
public dColor(Field field) {
try {
color = (Color) field.get(null);
}
catch (Exception e) {
dB.echoError("Exception trying to fetch color!");
}
}
public dColor(Color color) {
this.color = color;
}
public dColor(DyeColor dyeColor) {
this.color = dyeColor.getColor();
}
/////////////////////
// INSTANCE FIELDS/METHODS
/////////////////
// Associated with Bukkit Color
private Color color;
public Color getColor() {
return color;
}
String prefix = "color";
@Override
public String getPrefix() {
return prefix;
}
@Override
public String debug() {
return (prefix + "='<A>" + identify() + "<G>' ");
}
@Override
public boolean isUnique() {
return false;
}
@Override
public String getObjectType() {
return "Color";
}
@Override
public String identify() {
for (Field field : Color.class.getFields()) {
try {
if (((Color) field.get(null)).asRGB() == getColor().asRGB())
return "co@" + field.getName();
}
catch (Exception e) {
dB.echoError("Exception trying to fetch color!");
}
}
return "co@" + getColor().getRed() + "," + getColor().getGreen() + "," + getColor().getBlue();
}
@Override
public String identifySimple() {
return identify();
}
@Override
public String toString() {
return identify();
}
@Override
public dObject setPrefix(String prefix) {
if (prefix != null)
this.prefix = prefix;
return this;
}
@Override
public String getAttribute(Attribute attribute) {
// <--[tag]
// @attribute <co@color.red>
// @returns Element(Number)
// @description
// returns the red value of this color.
// -->
if (attribute.startsWith("red"))
return new Element(color.getRed()).getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <co@color.green>
// @returns Element(Number)
// @description
// returns the green value of this color.
// -->
if (attribute.startsWith("green"))
return new Element(color.getGreen()).getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <co@color.blue>
// @returns Element(Number)
// @description
// returns the blue value of this color.
// -->
if (attribute.startsWith("blue"))
return new Element(color.getBlue()).getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <co@color.rgb>
// @returns Element
// @description
// returns the RGB value of this color.
// EG, 255,0,255
// -->
if (attribute.startsWith("rgb"))
return new Element(color.getRed() + "," + color.getGreen() + "," + color.getBlue()).getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <co@color.hue>
// @returns Element(Number)
// @description
// returns the hue value of this color.
// -->
if (attribute.startsWith("hue"))
return new Element(ToHSB()[0]).getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <co@color.saturation>
// @returns Element(Number)
// @description
// returns the saturation value of this color.
// -->
if (attribute.startsWith("saturation"))
return new Element(ToHSB()[1]).getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <co@color.brightness>
// @returns Element(Number)
// @description
// returns the brightness value of this color.
// -->
if (attribute.startsWith("brightness"))
return new Element(ToHSB()[2]).getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <co@color.hsv>
// @returns Element
// @description
// returns the HSV value of this color.
// EG, 100,100,255
// -->
if (attribute.startsWith("hsv")) {
int[] HSV = ToHSB();
return new Element(HSV[1] + "," + HSV[1] + "," + HSV[2]).getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <co@color.name>
// @returns Element
// @description
// returns the name of this color (or red,green,blue if none).
// -->
if (attribute.startsWith("name"))
return new Element(identify().substring(3)).getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <co@color.mix[<color>]>
// @returns dColor
// @description
// returns the color that results if you mix this color with another.
// -->
if (attribute.startsWith("mix")
&& attribute.hasContext(1)) {
dColor mixed_with = dColor.valueOf(attribute.getContext(1));
if (mixed_with != null)
return new dColor(color.mixColors(mixed_with.getColor())).getAttribute(attribute.fulfill(1));
else
dB.echoError("'" + attribute.getContext(1) + "' is not a valid color!");
}
// <--[tag]
// @attribute <co@color.type>
// @returns Element
// @description
// Always returns 'Color' for dColor objects. All objects fetchable by the Object Fetcher will return the
// type of object that is fulfilling this attribute.
// -->
if (attribute.startsWith("type")) {
return new Element("Color").getAttribute(attribute.fulfill(1));
}
// Iterate through this object's properties' attributes
for (Property property : PropertyParser.getProperties(this)) {
String returned = property.getAttribute(attribute);
if (returned != null) return returned;
}
return new Element(identify()).getAttribute(attribute);
}
int[] ToHSB() {
float[] base = java.awt.Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null);
return new int[]{(int) (base[0] * 255), (int) (base[1] * 255), (int) (base[2] * 255)};
}
}