Skip to content

Commit 37f470c

Browse files
author
boiscljo
committed
Allow modifier to be applied to only parts of the text
1 parent 80f316c commit 37f470c

1 file changed

Lines changed: 66 additions & 47 deletions

File tree

src/main/java/com/iridium/iridiumcolorapi/IridiumColorAPI.java

Lines changed: 66 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public class IridiumColorAPI {
2121

2222
/**
2323
* The current version of the server in the a form of a major version.
24-
* If the static initialization for this fails, you know something's wrong with the server software.
24+
* If the static initialization for this fails, you know something's wrong with
25+
* the server software.
2526
*
2627
* @since 1.0.0
2728
*/
@@ -34,7 +35,8 @@ public class IridiumColorAPI {
3435
*/
3536
private static final boolean SUPPORTS_RGB = VERSION >= 16;
3637

37-
private static final List<String> SPECIAL_COLORS = Arrays.asList("&l", "&n", "&o", "&k", "&m","§l", "§n", "§o", "§k", "§m");
38+
private static final List<String> SPECIAL_COLORS = Arrays.asList("&l", "&n", "&o", "&k", "&m", "§l", "§n", "§o",
39+
"§k", "§m");
3840

3941
/**
4042
* Cached result of all legacy colors.
@@ -64,7 +66,8 @@ public class IridiumColorAPI {
6466
*
6567
* @since 1.0.2
6668
*/
67-
private static final List<Pattern> PATTERNS = Arrays.asList(new GradientPattern(), new SolidPattern(), new RainbowPattern());
69+
private static final List<Pattern> PATTERNS = Arrays.asList(new GradientPattern(), new SolidPattern(),
70+
new RainbowPattern());
6871

6972
/**
7073
* Processes a string to add color to it.
@@ -78,7 +81,7 @@ public static String process(@Nonnull String string) {
7881
for (Pattern pattern : PATTERNS) {
7982
string = pattern.process(string);
8083
}
81-
84+
8285
string = ChatColor.translateAlternateColorCodes('&', string);
8386
return string;
8487
}
@@ -93,8 +96,8 @@ public static String process(@Nonnull String string) {
9396
@Nonnull
9497
public static List<String> process(@Nonnull List<String> strings) {
9598
return strings.stream()
96-
.map(IridiumColorAPI::process)
97-
.collect(Collectors.toList());
99+
.map(IridiumColorAPI::process)
100+
.collect(Collectors.toList());
98101
}
99102

100103
/**
@@ -119,22 +122,10 @@ public static String color(@Nonnull String string, @Nonnull Color color) {
119122
*/
120123
@Nonnull
121124
public static String color(@Nonnull String string, @Nonnull Color start, @Nonnull Color end) {
122-
StringBuilder specialColors = new StringBuilder();
123-
for (String color : SPECIAL_COLORS) {
124-
if (string.contains(color)) {
125-
specialColors.append(color);
126-
string = string.replace(color, "");
127-
}
128-
}
129-
130-
StringBuilder stringBuilder = new StringBuilder();
125+
String originalString = string;
126+
string = withoutSpecialChar(string);
131127
ChatColor[] colors = createGradient(start, end, string.length());
132-
String[] characters = string.split("");
133-
for (int i = 0; i < string.length(); i++) {
134-
stringBuilder.append(colors[i]).append(specialColors).append(characters[i]);
135-
}
136-
137-
return stringBuilder.toString();
128+
return apply(originalString, colors);
138129
}
139130

140131
/**
@@ -146,22 +137,11 @@ public static String color(@Nonnull String string, @Nonnull Color start, @Nonnul
146137
*/
147138
@Nonnull
148139
public static String rainbow(@Nonnull String string, float saturation) {
149-
StringBuilder specialColors = new StringBuilder();
150-
for (String color : SPECIAL_COLORS) {
151-
if (string.contains(color)) {
152-
specialColors.append(color);
153-
string = string.replace(color, "");
154-
}
155-
}
156-
157-
StringBuilder stringBuilder = new StringBuilder();
140+
String originalString = string;
141+
string = withoutSpecialChar(string);
142+
158143
ChatColor[] colors = createRainbow(string.length(), saturation);
159-
String[] characters = string.split("");
160-
for (int i = 0; i < string.length(); i++) {
161-
stringBuilder.append(colors[i]).append(specialColors).append(characters[i]);
162-
}
163-
164-
return stringBuilder.toString();
144+
return apply(originalString, colors);
165145
}
166146

167147
/**
@@ -172,21 +152,57 @@ public static String rainbow(@Nonnull String string, float saturation) {
172152
*/
173153
@Nonnull
174154
public static ChatColor getColor(@Nonnull String string) {
175-
return SUPPORTS_RGB ? ChatColor.of(new Color(Integer.parseInt(string, 16))) : getClosestColor(new Color(Integer.parseInt(string, 16)));
155+
return SUPPORTS_RGB ? ChatColor.of(new Color(Integer.parseInt(string, 16)))
156+
: getClosestColor(new Color(Integer.parseInt(string, 16)));
176157
}
177158

178159
/**
179-
* Removes all color codes from the provided String, including IridiumColorAPI patterns.
160+
* Removes all color codes from the provided String, including IridiumColorAPI
161+
* patterns.
180162
*
181-
* @param string The String which should be stripped
182-
* @return The stripped string without color codes
163+
* @param string The String which should be stripped
164+
* @return The stripped string without color codes
183165
* @since 1.0.5
184166
*/
185167
@Nonnull
186168
public static String stripColorFormatting(@Nonnull String string) {
187169
return string.replaceAll("<#[0-9A-F]{6}>|[&§][a-f0-9lnokm]|<[/]?[A-Z]{5,8}(:[0-9A-F]{6})?[0-9]*>", "");
188170
}
189171

172+
@Nonnull
173+
private static String apply(@Nonnull String source, ChatColor[] colors) {
174+
StringBuilder specialColors = new StringBuilder();
175+
StringBuilder stringBuilder = new StringBuilder();
176+
String[] characters = source.split("");
177+
int outIndex = 0;
178+
for (int i = 0; i < characters.length; i++) {
179+
if (characters[i].equals("&") || characters[i].equals("§")) {
180+
if (i + 1 < characters.length) {
181+
if (characters[i + 1].equals("r")) {
182+
specialColors.setLength(0);
183+
} else {
184+
specialColors.append(characters[i]);
185+
specialColors.append(characters[i + 1]);
186+
}
187+
i++;
188+
} else
189+
stringBuilder.append(colors[outIndex++]).append(specialColors).append(characters[i]);
190+
} else
191+
stringBuilder.append(colors[outIndex++]).append(specialColors).append(characters[i]);
192+
}
193+
return stringBuilder.toString();
194+
}
195+
196+
@Nonnull
197+
private static String withoutSpecialChar(@Nonnull String source) {
198+
for (String color : SPECIAL_COLORS) {
199+
if (source.contains(color)) {
200+
source = source.replace(color, "");
201+
}
202+
}
203+
return source;
204+
}
205+
190206
/**
191207
* Returns a rainbow array of chat colors.
192208
*
@@ -199,7 +215,7 @@ public static String stripColorFormatting(@Nonnull String string) {
199215
private static ChatColor[] createRainbow(int step, float saturation) {
200216
ChatColor[] colors = new ChatColor[step];
201217
double colorStep = (1.00 / step);
202-
218+
203219
for (int i = 0; i < step; i++) {
204220
Color color = Color.getHSBColor((float) (colorStep * i), saturation, saturation);
205221
if (SUPPORTS_RGB) {
@@ -208,7 +224,7 @@ private static ChatColor[] createRainbow(int step, float saturation) {
208224
colors[i] = getClosestColor(color);
209225
}
210226
}
211-
227+
212228
return colors;
213229
}
214230

@@ -234,18 +250,18 @@ private static ChatColor[] createGradient(@Nonnull Color start, @Nonnull Color e
234250
};
235251

236252
for (int i = 0; i < step; i++) {
237-
Color color = new Color(start.getRed() + ((stepR * i) * direction[0]), start.getGreen() + ((stepG * i) * direction[1]), start.getBlue() + ((stepB * i) * direction[2]));
253+
Color color = new Color(start.getRed() + ((stepR * i) * direction[0]),
254+
start.getGreen() + ((stepG * i) * direction[1]), start.getBlue() + ((stepB * i) * direction[2]));
238255
if (SUPPORTS_RGB) {
239256
colors[i] = ChatColor.of(color);
240257
} else {
241258
colors[i] = getClosestColor(color);
242259
}
243260
}
244-
261+
245262
return colors;
246263
}
247264

248-
249265
/**
250266
* Returns the closest legacy color from an rgb color
251267
*
@@ -258,7 +274,9 @@ private static ChatColor getClosestColor(Color color) {
258274
double nearestDistance = Integer.MAX_VALUE;
259275

260276
for (Color constantColor : COLORS.keySet()) {
261-
double distance = Math.pow(color.getRed() - constantColor.getRed(), 2) + Math.pow(color.getGreen() - constantColor.getGreen(), 2) + Math.pow(color.getBlue() - constantColor.getBlue(), 2);
277+
double distance = Math.pow(color.getRed() - constantColor.getRed(), 2)
278+
+ Math.pow(color.getGreen() - constantColor.getGreen(), 2)
279+
+ Math.pow(color.getBlue() - constantColor.getBlue(), 2);
262280
if (nearestDistance > distance) {
263281
nearestColor = constantColor;
264282
nearestDistance = distance;
@@ -290,7 +308,8 @@ private static int getVersion() {
290308

291309
// 1.13.2, 1.14.4, etc...
292310
int lastDot = version.lastIndexOf('.');
293-
if (version.indexOf('.') != lastDot) version = version.substring(0, lastDot);
311+
if (version.indexOf('.') != lastDot)
312+
version = version.substring(0, lastDot);
294313

295314
return Integer.parseInt(version.substring(2));
296315
}

0 commit comments

Comments
 (0)